130 likes | 374 Views
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.
E N D
IPC mechanisms Pipes Sockets System V IPC Message Queues Semaphores Shared Memory COP5570 – 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 COP5570 – 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 COP5570 – 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: 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
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
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
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
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
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
Deleting Shared Memory int shmctl(int shmid, int cmd, struct shmid_ds *buf); shmctl(shmid, IPC_RMID, NULL); Example COP5570 – Advanced Unix Programming
Command-line IPC control • ipcs • Lists all IPC objects owned by the user • ipcrm • Removes specific IPC object COP5570 – 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 COP5570 – Advanced Unix Programming