Chapter 6 Interprocess Communications 1 Contents u Introduction






![SVR 4 Pipes u Bidirectional: u status = pipe(int fildes[2]); write to fildes[1] & SVR 4 Pipes u Bidirectional: u status = pipe(int fildes[2]); write to fildes[1] &](https://slidetodoc.com/presentation_image_h/b2bf2fadf0509d5b92bb6ed79d97b66e/image-7.jpg)
























- Slides: 31

Chapter 6 Interprocess Communications 1

Contents u Introduction u Universal IPC Facilities u System V IPC 2

Introduction u The purposes of IPC: u Data transfer u Sharing data u Event notification u Resource sharing u Process control 3

6. 2. Universal IPC Facilities u Signals u Kill u Sigpause u ^C u Expensive u Limited: only 31 signals. u Signals are not enough. 4

Pipes u. A unidirectional, FIFO, unstructured data stream of fixed maximum size. u int pipe (int * filedes) u filedes[0] for read, filedes[1] for write P P P Data P P Fig 6 -1 Data flow through a pipe. 5

Pipes u u u 6 Applications: in shell – passing output of one program to another program – e. g. cat fil 1 file 2 | sort Limitations: cannot be used for broadcastng; Data in pipe is a byte stream – has no structure No way to distinguish between several readers or writers Implementation – by using file system mechanisms, sockets or STREAMS, Alternative: FIFO – named pipe – may be accessed by unrelated processes, is persistent, has a name; - but must be explicitly deleted when not used, less secure than pipe,
![SVR 4 Pipes u Bidirectional u status pipeint fildes2 write to fildes1 SVR 4 Pipes u Bidirectional: u status = pipe(int fildes[2]); write to fildes[1] &](https://slidetodoc.com/presentation_image_h/b2bf2fadf0509d5b92bb6ed79d97b66e/image-7.jpg)
SVR 4 Pipes u Bidirectional: u status = pipe(int fildes[2]); write to fildes[1] & read from filde[0] u Or vice versa u u u 7 status = fattach(int fildes, char *path) fdetach()

Process Tracing u ptrace(cmd, pid, addr, data) pid is the process ID u addr refers to a location u cmd: r/w, intercept, set or delete watchpoints, resume the execution u Data: interpreted by cmd u Used by debuggers sdb or dbx u 8

Process Tracing u drawbacks and limitations - child must allow tracing explicitly by invoking ptrace; – parent can control the execution of its direct children only; extremely inefficient (requires several context switches); - cannot control a process already running; - security problems when tracing a setuid programs (user can obtain superuser privileges) – to avoid this UNIX disables tracing such programs; u current debuggers use different approach. 9

6. 3. System V IPC u Common u Key: Elements resource ID u Creator: IDs u Owner: IDs u Permissions: r/w/x for g/owner/others 10

6. 3. System V IPC u Common u u u 11 Elements General name: ipc resource Similar system calls: shmget, semget, msgget Similar control mechanisms Each ipc resource must be explicitly deallocated and removed Each type of ipc resource has its own fixed-size resource table

Semaphores u Special variable called a semaphore is used for signaling u If a process is waiting for a signal, it is suspended until that signal is sent u Wait and signal operations cannot be interrupted u Queue is used to hold processes waiting on the semaphore 12

P/V Operations u P(wait): u s=s-1; u if (s<0) block(); u V(signal): u s= s+1; u if (s>=0) wake(); 13

Producer/Consumer Problem u One or more producers are generating data and placing these in a buffer u A single consumer is taking items out of the buffer one at time u Only one producer or consumer may access the buffer at any one time u Two semaphores are used u u 14 one to represent the number of items in the buffer one to signal that it is all right to use the buffer

Producer Function #define SIZE 100 semaphore s=1 semaphore n=0 semaphore e= SIZE void producer(void) {while (TRUE){ produce_item(); wait(e); wait(s); enter_item(); signal(s); signal(n); } } 15

Consumer Function void consumer(void) {while (TRUE){ wait(n); wait(s); remove_item(); signal(s); signal(e); } } 16

Semaphore u u semid = semget(key, count, flag) status = semop(semid, sops, nsops) Add sem_op to the value, wake up struct sembuf{ Block until the value u unsigned short sem_num; becomes zero u short sem_op; /*>0; =0, <0*/ Block until the value u short sem_flg; becomes greater than or equal to the absolute u} u value of sem_op, then subtract sem_op from that value 17

Semaphore implementation struct semid_ds{ struct ipc_perm sem_perm; struct sem* sem_base; struct sem{ ushort sem_nsems; time_t sem_otime; ushort semval; tiem_t sem_ctime; pid_t sempid; } ushort semncnt; ushort semzcnt; } 18

An example of semaphore Semid_ds sem_perm sem_base sem_nsems sem_otime sem_ctime 19 sem_val sem_pid sem_ncnt sem_zcnt sem_val Sem_pid sem_ncnt sem_zcnt [0] [0] [1] [1]

Deadlock of semaphores lock Proc A S 1 20 S 2 Proc B lock

Problems with semaphores u Deadlock detection and avoidance left to the applications. u Sem allocation and initialization is not atomic – may lead to race conditions, u Sem must be explicitly removed garbage collection problem 21

Message Queue u u 22 msgqid = msgget(key, flag) msgsnd(msgqid, msgp, count, flag) struct msqid_ds { struct ipc_perm msg_perm; struct msg* msg_first; struct msg* msg_last; unshort msg_cbytes; unshort msg_qnum; } count =msgrcv(msgqid, msgp, maxcnt, msgtype, flag)

An example of a msgq msqid_ds msg_perm; msg_first; msg_last; msg_cbytes; msg_qnum; 23 Link Type=100 Length=1 data Link Type=200 Length=2 NULL Type=300 Length=3 Data

Using a message queue receivers struct Msgqid_ds P msg P 24 senders msg P

Discussion of message queues u u u u 25 Msgq are more versatile than pipes and address some of their limitations, Can transmit msgs as structured entities Msg type can be used to specify e. g. priorities, urgency, designate recipient, Can be used for small amount of data – expensive for large amounts Kernel does not help with recipient specification, Cannot broadcast msgs, Allow selective retrieval of msgs by type, STREAMS are now more popular for msg transfer

Shared memory u. A portion of physical memory that is shared by multiple processes. Process B Process A 0 x 30000 0 x 50000 Shared memory region 26 0 x 70000

Using the shared memory shmid = shmget(key, size, flag) u addr = shmat(shmid, shmaddr, shmflag) u shmdt(shmaddr) u 27

Client/server with shared memory client Shared memory server kernel Output file 28 Input file

Implementation of shared memory struct shmid_ds { struct ipc_perm shm_perm; int shm_segsz; struct anon_map *shm_amp; ushort shm_nattch; … }; 29

Shared memory u u u 30 Advantages – good for sharing large amount of data - very fast, Limitation – no synchronization provided – applications must create their own Alternative - mmap system call, which maps file into the address space of the caller,

Discussion u IPC is similar to file system u Distinct name space u Unsuitable for distributed environment u difficult to agree on a common key = ftok(char *pathname, int ndx) u Security: u If you can guess the key, you can r/w the resource. 31