Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 6 Interprocess Communications. 2 Contents u Introduction u Universal IPC Facilities u System V IPC.

Similar presentations


Presentation on theme: "1 Chapter 6 Interprocess Communications. 2 Contents u Introduction u Universal IPC Facilities u System V IPC."— Presentation transcript:

1 1 Chapter 6 Interprocess Communications

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

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

4 4 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.

5 5 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 Data P P P P P Fig 6-1 Data flow through a pipe.

6 6 Pipes u Applications: in shell – passing output of one program to another program – e.g. cat fil1 file2 | sort u Limitations:cannot be used for broadcastng; u Data in pipe is a byte stream – has no structure u No way to distinguish between several readers or writers u Implementation – by using file system mechanisms, sockets or STREAMS, u 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,

7 7 SVR4 Pipes u Bidirectional: u status = pipe(int fildes[2]); u write to fildes[1] & read from filde[0] u Or vice versa u status = fattach(int fildes, char *path) u fdetach()

8 8 Process Tracing u ptrace(cmd, pid, addr, data) u 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

9 9 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.

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

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

12 12 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

13 13 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();

14 14 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 one to represent the number of items in the buffer u one to signal that it is all right to use the buffer

15 15 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); }

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

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

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

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

20 20 Deadlock of semaphores S2 Proc B S1 Proc A lock

21 21 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

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

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

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

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

26 26 Shared memory u A portion of physical memory that is shared by multiple processes. Process A Process B 0x30000 0x50000 0x70000 Shared memory region

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

28 28 Client/server with shared memory clientserver kernel Shared memory Input fileOutput file

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

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

31 31 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 key = ftok(char *pathname, int ndx) u Security: u If you can guess the key, you can r/w the resource.


Download ppt "1 Chapter 6 Interprocess Communications. 2 Contents u Introduction u Universal IPC Facilities u System V IPC."

Similar presentations


Ads by Google