Interprocess Communication 2002 2 Hyun-Ju Park
Introduction (interprocess communication; IPC) IPC data transfer sharing data event notification resource sharing process control Interprocess Communication 1
Universal IPC facilities signal, pipe, process tracing IPC Interprocess Communication 2
Signals Limitation signals are expensive sender must make a system call limited bandwidth limited amount of information Interprocess Communication 3
Pipes (1) A unidirectional, FIFO, unstructured data stream of fixed maximum size Simple flow-control mechanism a process attempting to read from an empty pipe blocks until data is written to the pipe a process trying to write to a full pipe blocks until another process reads data from the pipe pipe() : read(), write() : Interprocess Communication 4
Pipes (2) Limitation data in a pipe is a byte-stream multiple reader, writer reader Implementation (in SVR2) inode BSD-based variants SVR4 Interprocess Communication 5
Pipes (3) FIFO file, named pipe mknod(filename, S_IFIFO) to create a FIFO file (persistent) pipes are easier to setup and consume fewer resources Interprocess Communication 6
SVR4 Pipes SVR4 STREAMS, FIFO SVR4 pipes are bidirectional status = pipe(int fildes[2]); STREAMS file descriptor status = fattach(int fildes, char *path); path :,, Interprocess Communication 7
Process Tracing (1) Use ptrace to control the execution of a child process dbx ptrace(cmd, pid, addr, data); pid : PID of the target process addr : location in the target s address space cmd : u area Interprocess Communication 8
Process Tracing (2) Limitation of ptrace if the traced process forks, the debugger cannot control the new process ptrace is extremely inefficient ptrace tracing a setuid program raises a security problem SVR4 and Solaris provide much more efficient debugging facilities using the /proc file system Interprocess Communication 9
Process Tracing (3) /proc file system /proc, /proc PID /proc, lseek, read, write ioctl /proc file system is not a physical file Interprocess Communication 10
System V IPC System V IPC - semaphores, message queues, and shared memory BSD UNIX Interprocess Communication 11
(1) IPC,, IPC (key) (creator) (owner) (permissions) UID & GID UID & GID,, / / IPC_STAT / IPC_SET : IPC_RMID : Interprocess Communication 12
(2) IPC ipc_perm Interprocess Communication 13
(1) semget semid = semget(key, count, flag) key : 32bit value count : an array of count semaphores flag : IPC_CREAT, IPC_EXCL Interprocess Communication 14
(2) semop status = semop(semid, sops, nsops); sops: a pointer to an nsops-element array of sembuf structures struct sembuf { unsigned short sem_num; short sem_op; short sem_flg; } semop > 0 : V() operation semop = 0 : 0 sem_op <0 : semop Interprocess Communication 15
(3) struct semid_ds { } struct ipc_perm sem_perm; struct sem* sem_base; // pointer to array of semaphores in set ushort struct sem { } ushort pid_t ushort ushort semval; sempid; semncnt; semzcnt; sem_nsems; // number of semaphores in set Interprocess Communication 16
(4) Major problems with semaphores race conditions & deadlock avoidance Interprocess Communication 17
(1) msgget / msgqid=msgget(key, flag); msgsnd msgsnd(msgqid, msgp, count, flag); msgrcv count = msgrcv(msgqid, msgp, maxcnt, msgtype, flag); Interprocess Communication 18
(2) message queue resource table entry struct msqid_ds { } struct ipc_perm msg_perm struct msg* struct msg* msg_first; msg_last; Interprocess Communication 19
(3) receivers P P msg Struct msgqid_ds msg msg senders P P Interprocess Communication 20
(4) message queue transmit data as discrete messages rather than as an unformatted byte-stream(pipe),, 2 do not supply a broadcast mechanism Interprocess Communication 21
(1) shmid=shmget(key,size,flag) addr = shmat(shmid,shmaddr,shmflag); shmdt(shmaddr); shared memory resource table entry struct shmid_ds { } struct ipc_perm shm_perm; int shm_segsz; Interprocess Communication 22
(2) 0x30000 0x50000 Memory map of process A 0 Shared memory region Memory map of process B 0 0x50000 0x70000 Interprocess Communication 23