1 36
전공핵심실습1:운영체제론 Chapter 6. Inter-process Communication (IPC)
Sungkyunkwan University Embedded Software Lab. Dongkun Shin
Embedded Software Lab. 2 Contents 36
• Linux Kernel IPC – Pipe – FIFO – System V IPC • Semaphore • Message Queue • Shared Memory • Tizen Platform IPC – D-Bus – vconf
Embedded Software Lab. 3 Linux Kernel IPC 36
• Pipe • FIFO • System V IPC – Semaphore – Message Queue – Shared Memory
Embedded Software Lab. 4 IPC System Call 36
• User uses IPC mechanism via system calls provided by the kernel
Tizen IPC System Calls (# syscall number) PIPE FIFO Semaphore Message Queue Shared Memory MSGGET (303) SHMGET (307) PIPE (42) OPEN (5) SEMGET (299) MSGSND (301) SHMAT (305) PIPE2 (359) READ (3) SEMOP (298) WRITE (4) MSGRCV (302) SHMDT (306) IPC System Call
Linux Kernel
Embedded Software Lab. 5 PIPE 36
• The oldest form of UNIX IPC and provided by all unix systems • Two limitations – Half-duplex : data flows only in one direction – Can be used only between processes that have a common ancestor • Usually used between the parent and child processes • int pipe (int fd[2]); – Two file descriptors are returned through the fd argument • fd[0] : open for reading • fd[1] : open for writing – The output of fd[1] is the input for fd[0]
Embedded Software Lab. 6 PIPE (cont.) 36
Embedded Software Lab. 7 PIPE (cont.) 36
• Kernel Implementation (linux/fs/pipe.c)
SYSCALL_DEFINE2(pipe2, int __user *, fildes, int, flags) { struct file *files[2]; int fd[2]; int error; create 2 pipe files (later in fifo) and open with O_RDONLY and O_WRONLY flags error = __do_pipe_flags(fd, files, flags); if (!error) { if (unlikely(copy_to_user(fildes, fd, sizeof(fd)))) { fput(files[0]); Copy file descriptors to userspace as an output of the system call fput(files[1]); put_unused_fd(fd[0]); put_unused_fd(fd[1]); error = -EFAULT; } else { fd_install(fd[0], files[0]); fd_install(fd[1], files[1]); } Install file *s in the file descriptor table of the process } return error; } Embedded Software Lab. 8 FIFO (a.k.a named pipe) 36
• Unrelated processes can exchange data, whereas pipes can be used only between related processes • FIFO is a type of file. (S_ISFIFO macro against st_mode) – FIFO is handled by file operations such as open(), close(), read(), and write(), once a FIFO created. – Multiple readers/writers are allowed • int mkfifo (const char *path, mode_t mode) – Generates FIFO with specific file name, path. – Accesses the generated FIFO with open system calls between 2 processes • open(“fifo1”, O_RDONLY) FIFO must have Reader / Writer both. • open(“fifo1”, O_WRONLY)
Embedded Software Lab. 9 Create Pipe Files 36
• Create a directory entry • Create an inode • Plug pipefifo_ops to file_operations of the generated pipe or fifo files.
const struct file_operations pipefifo_fops = { .open = fifo_open, .llseek = no_llseek, .read = do_sync_read, .aio_read = pipe_read, .write = do_sync_write, .aio_write = pipe_write, .poll = pipe_poll, .unlocked_ioctl = pipe_ioctl, .release = pipe_release, once. fasyncpipefifo_fops= pipe_fasyncis registered ,as file_operations, }; the normal file I/O functions all work with FIFO
Embedded Software Lab. 10 FIFO Example 36
• In Shell $ cat /proc/meminfo | grep –I active | tail –n4 > memory.txt
Embedded Software Lab. 11 System V IPC 36
• Semaphore, Message Queue, and Shared Memory • Semaphore – Synchronize itself with other processes • Message Queue (Not equal to POSIX Message queue) – Send or receive messages each other • Shared Memory – Share a memory area • System V IPCs shares same kernel data structures and operations (linux/ipc/util.h) – All IPC objects are stored in kernel memory – They are referred to by IPC object identifier – Kernel translate a given key to corresponding ID, whereas processes only can access the key Embedded Software Lab. 12 IPC Identifier 36
• Key of IPC Resources – semget() – Get Semaphores IPC Resources – msgget() – Get Messages IPC Resources – shmget() – Get Share Memory IPC Resources
semget() msgget() + IPC Identifier shmget() (XXX)
IPC resource Process1 Process2 XXX
Some data for share
Embedded Software Lab. 13 IPC Resource (include/linux/ipc_namespace.h) 36
ipc_ids
represent The data structure similar with radix tree IPC Resources Type to map a id to the corresponding pointer
struct idr ipcs_idr
kern_ipc_prem kern_ipc_prem kern_ipc_prem
represent represent represent IPC Resource IPC Resource IPC Resource
Embedded Software Lab. 14 Semaphore 36
• Similar to the kernel semaphores (lock) – include/linux/semaphore.h, kernel/locking/semaphore.c • Counters used to controlled access to shared data structures for multiple processes semget() : get IPC Identifier semop() : Decrease or Increase semaphore count
semop( struct sembuf ) struct sembuf Process IPC resource sem_num : Semaphore number sem_op : Add this to semval (increase / decrease semaphore count) sem_flg : Operation flags Some data for share
Embedded Software Lab. 15 Semaphore (ipc/sem.c) 36
• Kernel Implementation
SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg) { struct ipc_namespace *ns; struct ipc_ops sem_ops; struct ipc_params sem_params;
ns = current->nsproxy->ipc_ns;
if (nsems < 0 || nsems > ns->sc_semmsl) return -EINVAL; newary() function allocates new ipc semaphore to ids as invoked via ipcget() sem_ops.getnew = newary; sem_ops.associate = sem_security; sem_ops.more_checks = sem_more_checks;
sem_params.key = key; sem_params.flg = semflg; sem_params.u.nsems = nsems;
return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params); } Embedded Software Lab. 16 newary() (ipc/sem.c) 36
ipc_ids
represent IPC Resources Type
struct idr ipcs_idr
sem_array kern_ipc_prem sem_array kern_ipc_prem
represent represent represent represent Semephore IPC Resources Semephore IPC Resources
Embedded Software Lab. 17 Message Queue 36
• IPC Messages – msgget() : Create IPC Messages resources or get IPC identifier – msgsnd() : Send messages – msgrcv() : Receive messages – POSIX Message Queue is implemented independently Buffer Data
Buffer Data Some data for share msgrcv()
msgsnd()
IPC resource Process1 Process2 XXX Embedded Software Lab. 18 Messages IPC Resources 36
• Kernel Implementation (ipc/msg.c)
SYSCALL_DEFINE2(msgget, key_t, key, int, msgflg) { struct ipc_namespace *ns; struct ipc_ops msg_ops; struct ipc_params msg_params;
newqueuens = current() function->nsproxy allocates-> ipc_nsnew ipc; message to ids as invoked via ipcget()
msg_ops.getnew = newque; msg_ops.associate = msg_security; msg_ops.more_checks = NULL;
msg_params.key = key; msg_params.flg = msgflg;
return ipcget(ns, &msg_ids(ns), &msg_ops, &msg_params); }
Embedded Software Lab. 19 newque() (ipc/msg.c) 36
ipc_ids
represent IPC Resources Type
struct idr ipcs_idr
msg_queue kern_ipc_prem msg_queue kern_ipc_prem
State represent State represent Messages IPC Resources Messages IPC Resources
msg_queue msg_queue Messages Messages
Embedded Software Lab. 20 Shared Memory 36
• shmget() : Create IPC Share Memory resources or get IPC identifier • shmat() : Map IPC Share Memory space for user process memory space • shmdt() : Unmap IPC Share Memory space for user process memory space
Process1 Process2 IPC Resource
0 shmat() 0 shmdt() 0 0 Default : 32MB Kernel Kernel Max : 4096 Data Data 0 0 Embedded Software Lab. 21 Shared Memory IPC Resources 36
• Kernel Implementation (ipc/shm.c)
SYSCALL_DEFINE3(shmget, key_t, key, size_t, size, int, shmflg) { struct ipc_namespace *ns; struct ipc_ops shm_ops; struct ipc_params shm_params;
newsegns ()= functioncurrent -allocates>nsproxy new->ipc_ns shared; memory to ids as invoked via ipcget()
shm_ops.getnew = newseg; shm_ops.associate = shm_security; shm_ops.more_checks = shm_more_checks;
shm_params.key = key; shm_params.flg = shmflg; shm_params.u.size = size;
return ipcget(ns, &shm_ids(ns), &shm_ops, &shm_params); }
Embedded Software Lab. 22 newseg() (ipc/shm.c) 36
ipc_ids
represent IPC Resources Type
struct idr ipcs_idr
shmid_kernel kern_ipc_prem shmid_kernel kern_ipc_prem
State represent State represent Share Memory IPC Resources Share Memory IPC Resources
File Object File Object
Embedded Software Lab. 23 Other IPCs in Linux 36
• POSIX IPC Mechanism – Same interface as System V IPC – Implemented in independent pool from System V IPC – Implemented not through key-identifier design but through file-based design – Event-driven support (epoll event) • Unix Domain Socket – General purpose IPC • Socket supports not only IPC but also diverse network protocols. – Simple code style, but high overhead compared to other IPC mechanisms.
Embedded Software Lab. 24 Tizen Platform IPC 36
• Various IPC methods are used in Tizen. – D-Bus – vconf – socket – pipe • Old Tizen (2.0, 2.1) used socket and pipe for IPC. • Recent Tizen (2.3~, 3.x) is adopting dbus and vconf for IPC. – Socket D-Bus: system server, AUL, alarm manager, connectivity manager, BlueZ, NFC manager, …
Embedded Software Lab. 25 D-Bus 36
• Inter-process communication (IPC) mechanism using socket – Simplify the IPC requirements with single shared channel – Access control using SMACK Fig. 1 IPC without D-Bus
Fig. 3 D-Bus and SMACK Fig. 2 IPC with D-Bus Embedded Software Lab. 26 D-Bus Access Control 36
• Access control using SMACK (Simplified Mandatory Access Control Kernel) – dbus daemon allow setting the policy to apply using configuration files explicitly – Set allow or deny interface/destination
Embedded Software Lab. 27 D-Bus in Tizen 36
• User-level APIs and wrappers to use D-Bus – Low-level API • DBusConnection – Connection to another application • DBusMessage – Message to be sent or received over a DBusConnection
– D-Bus Wrapper • EDBus – D-Bus wrapper for EFL applications • GDBus – Glib based D-Bus wrapper
Embedded Software Lab. 28 D-Bus Low-level API 36
Process 1 Process 2
dbus_bus_get () dbus_bus_get () Init dbus_bus_request_name ()
dbus_message_new_method_call() dbus_message_append_args() reply=dbus_connection_ Method send_with_reply_and_block() dbus_message_is_method_call() Call
dbus_message_get_args()
Signal dbus_message_new_signal() dbus_message_is_signal()
Embedded Software Lab. 29 EDBus Usage in Tizen 36
• Used in system framework and UI framework. • Example: deviced (in system framework) 1. Initialize • deviced/src/core/edbus-handler.c
void edbus_init(void *data) { ... edbus_request_name = e_dbus_request_name(edbus_conn, DEVICED_BUS_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING, request_name_cb, NULL); ... }
2. Register methods (Display device management in deviced) • deviced/src/display/display-dbus.c
int init_pm_dbus(void) static const struct edbus_method edbus_methods[] = { { { "start", NULL, NULL, edbus_start }, ret = { "stop", NULL, NULL, edbus_stop }, register_edbus_method(DEVICED_PATH_DISPLAY, { "lockstate", "sssi", "i", edbus_lockstate }, { "unlockstate", "ss", "i", edbus_unlockstate }, edbus_methods, ARRAY_SIZE(edbus_methods));
Embedded Software Lab. 30 EDBus Usage in Tizen 36
• Example: deviced (in system framework) (Cont’d) 3. Other process send message • appfw/alarm-manager/alarm-manager.c
int __display_lock_state(char *state, char *flag, unsigned int timeout) { msg = g_dbus_message_new_method_call(DEVICED_BUS_NAME, DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY, DEVICED_LOCK_STATE);
4. Receive and handle the message • deviced/src/core/edbus-handler.c
static DBusHandlerResult message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
ret = dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg, DBUS_TYPE_INVALID);
DD_LIST_FOREACH(edbus_watch_list, n, watch) { if (strcmp(arg, watch->name)) continue; if (watch->func) watch->func(watch->name, watch->id); • Compare method name } • Perform function call Embedded Software Lab. 31 EDBus Usage in Tizen 36
• Example: deviced (in system framework) (Cont’d) 5. Perform specific function
static DBusMessage *edbus_lockstate(E_DBus_Object *obj, DBusMessage *msg) { if (!dbus_message_get_args(msg, &err, DBUS_TYPE_STRING, &state_str, DBUS_TYPE_STRING, &option1_str, ① Get arguments from message DBUS_TYPE_STRING, &option2_str, DBUS_TYPE_INT32, &timeout, DBUS_TYPE_INVALID)) { _E("there is no message"); ret = -EINVAL; goto out; }
if (!strcmp(state_str, PM_LCDON_STR)) state = LCD_NORMAL; else if (!strcmp(state_str, PM_LCDDIM_STR)) ② Check LCD state state = LCD_DIM; else if (!strcmp(state_str, PM_LCDOFF_STR)) state = LCD_OFF;
Embedded Software Lab. 32 vconf 36
• Key-value fair + inotify – Store system configuration using SQLite (libsqlfs) – Able to communicate between inter-process using inotify • Inotify (inode notify) – Linux kernel subsystem to notify filesystem’s event • used to notify an event to application
Embedded Software Lab. 33 vconf 36
• vconftool – A tool to get or set the configuration values managed by vconf – Example: Initialize Tizen device configuration values in deviced • set -t
Embedded Software Lab. 34 vconf Access Control 36
• Vconf access is controlled by SMACK – Use one of predefined smack labels – All modules(including applications) will have read permission to those files and only restricted modules have write permission
– Label list • setting : only setting application can write • inhouse : preloaded applications and platform modules can write • privacy : only privacy related modules can write • system : app framework, security, system, base • multimedia : graphics & UI, multimedia • network : web, connectivity • misc : other keys
Embedded Software Lab. 35 vconf Usage 36
• Get the values of Tizen device configuration 1. Connect your Tizen Z3 phone to your PC using micro-USB cable.
2. $ sdb root on … 3. $ sdb shell 4. # vconftool get memory/
… Embedded Software Lab. 36 vconf Usage 36
• Set the values of Tizen device configuration – Notify power-off button event • # vconftool set -t int memory/sysman/power_off 1 -i -s system::vconf_system -f – Notify earphone jack event • # vconftool set -t int memory/sysman/earjack 1 -i -s system::vconf_system -f – Control battery capacity as 1% • # vconftool set -t int memory/sysman/battery_capacity 5 -i -s system::vconf_system -f
Embedded Software Lab.