Shared Memory • Tizen Platform IPC – D-Bus – Vconf

Shared Memory • Tizen Platform IPC – D-Bus – Vconf

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 <busconfig> <policy smack="bluez"> <allow own="org.bluez"/> </policy> <policy smack="bt_agent::public"> <allow send_destination="org.bluez" send_interface="org.bluez.ProfileManager1" send_member="RegisterProfile1"/> </policy>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    36 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us