1 / 19

Advanced UNIX IPC Facilities

After Haviland, et al.’s book. Advanced UNIX IPC Facilities. Introduction and Basic Concepts. UNIX offers a variety of advanced IPC mechanisms This makes UNIX an extremely rich system in the IPC area

sandra_john
Download Presentation

Advanced UNIX IPC Facilities

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. After Haviland, et al.’s book Advanced UNIX IPC Facilities

  2. Introduction and Basic Concepts • UNIX offers a variety of advanced IPC mechanisms • This makes UNIX an extremely rich system in the IPC area • Allows developers to use a variety of approaches when programming a system made up of cooperating tasks

  3. Categories of advanced IPC • System IV: • sockets • System V: (our emphasis) • Message passing • Semaphores • Shared memory

  4. IPC facility keys • UNIX System V has made the 3 categories of IPC as similar as possible for programming convenience • The most (?) important similarity is the IPC key • Keys are numbers used to identify an IPC object on a UNIX system • Much like a file name being used to identify files • Key allows IPC objects to be shared by several processes

  5. Difference between keys and file names • Keys are not file names and carry less meaning • Actual key type is determined by implementation, and defined by key_t in <sys/types.h> • UNIX uses a simple library function that maps a file’s pathname to a key • Routine is called ftok( ) • Thus it is possible to start with a pathname and get a key generated automatically from the pathname

  6. IPC get operation • With the get operation, a program uses a key to • Create an IPC, or, • Gain access to an existing one Mqid = msgget((key_t)0100, 0644|IPC_CREAT|IPC_EXCL); For semaphores: semget For shared mem: shmget Msg queue key Non-neg id returned if successful

  7. Other IPC Operations (other than get) • Control operations • Get status information • Set control values • Actual calls are msgctl, semctl, shmctl • More specific operations to perform various functions • Do interesting work • Called “IPC operations” • For example, for message queues we have: • Msgsend • msgrcv

  8. Status data structures • When an IPC object is created, the system also creates an IPC facility status structure • This contains any administrative info assoc with object • There is one type of status structure for messages, semaphores and shared mem • However all 3 contain a common permission structure, ipc_perm

  9. Message passing • A message is basically a sequence of characters or bytes, not necessarily null-terminated • One process creates a message queue using msgget • Once a queue is established, a process with the right permissions can put messages into it with msgsnd • Another process can then read this message with msgrcv

  10. Form of msgget #include <sys/msg.h> int msgget(key_t key, int permflags);

  11. Permflags • Determine the exact action perform by msgget • Two constants are relevant here, defined in <sys/ipc.h> • Can be used alone or ORed together • IPC_CREAT • Tells msgget to create a message queue for the value key if one doesn't exist • Message queue will not be overwritten if it already exists • If not set, then msgget returns id of existing msgqueue, if exists • IPC_EXCL • If this and IPC_CREAT are both set, then only intend to create new msgqueue • Return -1 if already exists

  12. Semaphores (info from Robbins and Robbins book) • Semaphore can be created or opened (gotten) with semget() call • Key can be specified in 3 ways • Use IPC_PRIVATE and have system come up with a new key • Pick your own key (type key_t) • Use ftok() to generate a key from a pathname (C string) • See man page for more details • See example next slide.

  13. Semaphore creation example /* creates a private semaphore set containing 3 semaphore elements. from Kay and Steven Robbins, p. 311 */ #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #define PERMS S_IRUSR | S_IWUSR #define SET_SIZE 3 int semid; if ((semid = semget(IPC_PRIVATE, SET_SIZE, PERMS)) < 0) perror ("Could not create new private semaphore"); /* PERMS value says semaphores can only be r/w by user. IPC_PRIVATE key guarantees that semget creates new sems */

  14. Semaphore operations • A semaphore is initialized by the semctl() call. • A process can increment, decrement, or test individual semaphore elements for a 0 value by using the semop() call #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> int semop(int semid, struct sembuf *sops, int nsops);

  15. Explanation of semop() • semid is handle returned by semget • sops points to an array of element operations • nsops specified number of element operations -All the operations specified in the array of struct sembuf are performed atomically on a single semaphore set -All operations must be performable simultaneously for any for any operation to succeed.

  16. Struct sembuf details • short sem_num: number of the semaphore element (index into semaphore set array) • short sem_op: the particular operation to be performed on that semaphore (not to be confused with semop!) • Operations to be detailed on the next slide • short sem_flg: flags to specify options for the operation (see man page)

  17. Details of sem_op member of struct sembuf • If sem_op > 0: semop() adds the value to the corresponding semaphore element, and awakens all processes that are waiting for the element to increase. • If sem_op = 0: if semaphore element != 0, semop() blocks calling process (waiting for 0) and increments the count of processes waiting for a 0 value of that element. If element = 0 do nothing. • Usefulness: a semaphore counts some resource. These processes waiting for semaphore to become 0 will be awakened to do some job when the resource disappears. • If sem_op < 0: semop() adds the (negative) value to the semaphore provided result isn't negative. But if result would be negative, don't add, and instead block process until semaphore increases enough. If result of addition is 0 then semop() awakens processes waiting for that semaphore element to be 0.

  18. Initializing sembuf • The fields of struct sembuf need not be in any particular order • So don't initialize at declaration like this: • Struct sembuf myopbuf = {1, 1, 0}; • Instead, use a program, as shown on next slide.

  19. Program for initializing struct sembuf #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> void set_sembuf_struct(struct sembuf *s, int num, int op, int flg){ s->sem_num = (short) num; s->sem_op = op; s->sem_flg = flg; return; }

More Related