1 / 12

IPC mechanisms

IPC mechanisms. Pipes Sockets System V IPC Message Queues Semaphores Shared Memory. 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.

adolph
Download Presentation

IPC mechanisms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IPC mechanisms Pipes Sockets System V IPC Message Queues Semaphores Shared Memory COP5570 – Advanced Unix Programming

  2. 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 COP5570 – Advanced Unix Programming

  3. 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 COP5570 – Advanced Unix Programming

  4. 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: can’t use select and poll. • Sometimes busy-wait is the only choice • Advantages: reliable, flow controlled, record oriented, beyond first-in first-out COP5570 – Advanced Unix Programming

  5. 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. COP5570 – Advanced Unix Programming

  6. 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) COP5570 – Advanced Unix Programming

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

  8. Creating Shared Memory int shmget(key_t key, size_t size, int shmflg); Example: key_t key; int shmid; key = ftok(“<somefile>", ‘A'); shmid = shmget(key, 1024, 0644 | IPC_CREAT); Here’s an example. COP5570 – Advanced Unix Programming

  9. Attach and DetachShared Memory void *shmat(int shmid, void *shmaddr, int shmflg); int shmdt(void *shmaddr); Example: key_t key; int shmid; char *data; key = ftok("<somefile>", ‘A'); shmid = shmget(key, 1024, 0644); data = shmat(shmid, (void *)0, 0); shmdt(data); Here’s an example. COP5570 – Advanced Unix Programming

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

  11. Command-line IPC control • ipcs • Lists all IPC objects owned by the user • ipcrm • Removes specific IPC object COP5570 – Advanced Unix Programming

  12. 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 COP5570 – Advanced Unix Programming

More Related