Presentation is loading. Please wait.

Presentation is loading. Please wait.

Florida State UniversityCOP5570 – Advanced Unix Programming IPC mechanisms Pipes Sockets System V IPC –Message Queues –Semaphores –Shared Memory.

Similar presentations


Presentation on theme: "Florida State UniversityCOP5570 – Advanced Unix Programming IPC mechanisms Pipes Sockets System V IPC –Message Queues –Semaphores –Shared Memory."— Presentation transcript:

1 Florida State UniversityCOP5570 – Advanced Unix Programming IPC mechanisms Pipes Sockets System V IPC –Message Queues –Semaphores –Shared Memory

2 Florida State UniversityCOP5570 – Advanced Unix Programming Identifiers and Keys –Each IPC structure (message queue, semaphore, or share memory segment) is identified by a nonnegative integer identifier. Similar to a file descriptor, but can be a large value. –A key must be specified when creating an IPC structure. Kernel convert the key to the identifier. All process access to an IPC structure must have a way to specify the same key? –One way to do it ftok

3 Florida State UniversityCOP5570 – Advanced Unix Programming key_t ftok(const char *path, int id); –Path point to a file that the process can stat –Id: project ID, only the last 8 bits are used

4 Florida State UniversityCOP5570 – Advanced Unix Programming Advantages and disadvantages of system V IPC mechanisms: –All system wide and do not have a reference count. Must be deleted explicitly (ipcs, ipcrm). –The IPC structures are not identified by names in the filesystem. –IPC descriptor is different from file descriptors: cant use select and poll. Sometimes busy-wait is the only choice –Advantages: reliable, flow controlled, record oriented, beyond first-in first-out

5 Florida State UniversityCOP5570 – Advanced Unix Programming Message queue. –A linked list of messages stored within the kernel and identified by a message queue identifier. Every message has a type field, and a nonnegative length, and the actual data bytes. Msgsnd puts a message at the end of the queue Msgrcv gets a message, may not follow FIFO order (can be based on type) Has resource limits: MSGMAX, MSGMNB, MSGMNI, MSGTQL.

6 Florida State UniversityCOP5570 – Advanced Unix Programming Message queue operations Int msgget(key_t, int flag) Int msgctl(int msgid, int cmd, struct msgid_ds *buf) Int msgsnd(int msgid, const void *ptr, size nbytes, int flag); Int msgrcv(int msgid, void *ptr, size_t nbytes, long type, int flag); Message queue has a lot of problems: –Removal is handled in strange way –Limited by global resources: programs that use it can be affected by other processes using message queues Performance advantage is no longer there in newer systems (compared with pipe)

7 Florida State UniversityCOP5570 – Advanced Unix Programming Shared Memory Common chunk of read/write memory among processes Proc. 1 Proc. 2 ptr Attach Proc. 3 Proc. 4Proc. 5 ptr Attach Create Shared Memory (unique key) 0 MAX

8 Florida State UniversityCOP5570 – Advanced Unix Programming Creating Shared Memory int shmget(key_t key, size_t size, int shmflg); Example: key_t key; int shmid; key = ftok( ", A'); shmid = shmget(key, 1024, 0644 | IPC_CREAT); Heres an example.example.

9 Florida State UniversityCOP5570 – Advanced Unix Programming Attach and Detach Shared Memory void *shmat(int shmid, void *shmaddr, int shmflg); int shmdt(void *shmaddr); Example: key_t key; int shmid; char *data; key = ftok(" ", A'); shmid = shmget(key, 1024, 0644); data = shmat(shmid, (void *)0, 0); shmdt(data); Heres an example.example.

10 Florida State UniversityCOP5570 – Advanced Unix Programming Deleting Shared Memory int shmctl(int shmid, int cmd, struct shmid_ds *buf); shmctl(shmid, IPC_RMID, NULL); Example

11 Florida State UniversityCOP5570 – Advanced Unix Programming Command-line IPC control ipcs –Lists all IPC objects owned by the user ipcrm –Removes specific IPC object

12 Florida State UniversityCOP5570 – Advanced Unix Programming Semaphores Managing concurrent access to shared memory segment. Using Semaphores –Creation: semget( … ) –Incr/Decr/Test-and-set : semop(…) –Deletion: semctl(semid, 0, IPC_RMID, 0); Online tutorial http://www.ecst.csuchico.edu/~beej/guide/ipc/semaphores.html


Download ppt "Florida State UniversityCOP5570 – Advanced Unix Programming IPC mechanisms Pipes Sockets System V IPC –Message Queues –Semaphores –Shared Memory."

Similar presentations


Ads by Google