1 / 18

Message Queues

Message Queues. Linked list of msgs stored within K Create Q / q open msgget() Msg added at end of Q msgsnd(). Message Queues. struct msqid_ds { struct ipc_perm msg_perm; msgqnum_t msg_qnum; /* # of messages on queue */

tracey
Download Presentation

Message Queues

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. Message Queues • Linked list of msgs stored within K • Create Q / q open msgget() • Msg added at end of Q msgsnd() www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  2. Message Queues struct msqid_ds { struct ipc_perm msg_perm; msgqnum_t msg_qnum; /* # of messages on queue */ msglen_t msg_qbytes; /* max # of bytes on queue */ pid_t msg_lspid; /* pid of last msgsnd() */ pid_t msg_lrpid; /* pid of last msgrcv() */ time_t msg_stime; /* last-msgsnd() time */ time_t msg_rtime; /* last-msgrcv() time */ time_t msg_ctime; /* last-change time */ . . . }; www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  3. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  4. #include <sys/msg.h> int msgget(key_t key, int flag); Returns: message queue ID if OK, 1 on error Ipc_ perm=0 msgqnum, msg_lspid, msg_lrpid ,msg_stime, msg_rtime msg_ ctime=current time #include <sys/msg.h> int msgctl(int msqid, int cmd, struct msqid_ds *buf ); Returns: 0 if OK, -1 on error www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  5. Messages are retrieved from q by #include<sys/msg.h> Ssizet msgrcv(int msqid,void *ptr,size bytes,long type,int flag); Type==0 first msg returned Type>0 first msg on q whose message type equals type is returned Type<0 first msg on q whose message type is lowest less than or equal to absolute value type is returned www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  6. Shared memory • Allows two or more processes to share a given region • of memory • Fastest form of IPC • Struct shmid_ds{ • Ipc_perm shm_perm; • Size_t shm_size; • Pid_t shm_pid; • Time_t shm_dtime; • Time_t shm_ctime; • Time_t shm_atime; • . • . • }; www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  7. #include<sys.shm.h> • Int shmget(keyt key,size_t size, int flag); • Return is if ok -1 on error • #include<sys.shm.h>//catch all • Int shmctl(int shmid,int cmd,struct shmids_ds *buf); • #include<sys.shm.h> • Void *shmat(int shmid,const void *addr,int flag); • #include<sys.shm.h> • Int shmdt(void *addr); www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  8. program that prints info on where one particular system places various types of datachar array[size];int main(void){int shmid;if(ptrmalloc(MALLOC_SIZE)==NULL err_sys(“malloc error”);if (shmid=shmget(ipc_private,size,shm mode)<0) err_sys(“ error”); if (shmptr=shmat(shmid,0,0)) err_sys(“ error”); if (shmctl(shmid,ipc_id,0) err_sys(“ error”); exit(0);} www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  9. semaphores • A semaphore is a counter used to provide access • to a shared data object for • multiple processes. • Example :operation of railroads • Only one rail is allowed. • Guarding this track is semaphore. • Semaphore is simple integer. • Test semaphore that controls the resource. • Semaphore=positive process can use resource • Semaphore=semaphore-1 (since it has used one unit of the resource) • Otherwise semaphore=0, the process goes to sleep • until the semaphore value >0.when the process wakes up, it returns to step1. Figure: single rail track www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  10. The kernel maintains a semid_ds structure for each semaphore set: struct semid_ds { struct ipc_perm sem_perm; unsigned short sem_nsems; /* # of semaphores in set */ time_t sem_otime; /* last-semop() time */ time_t sem_ctime; /* last-change time */ . . . }; www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  11. The kernel maintains a semid_ds structure for each semaphore set: minimum struct semid_ds { struct ipc_perm sem_perm; unsigned short sem_nsems; /* # of semaphores in set */ time_t sem_otime; /* last-semop() time */ time_t sem_ctime; /* last-change time */ . . . }; www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  12. The table below lists the system limits that affect semaphore sets. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  13. The first function to call is semget to obtain a semaphore ID. #include <sys/sem.h> int semget(key_t key, int nsems, int flag); Returns: semaphore ID if OK, -1 on error When a new set is created, the following members of the semid_ds structure are initialized. The ipc_perm structure is initialized. The mode member of this structure is set to the corresponding permission bits of flag. sem_otime is set to 0. sem_ctime is set to the current time. sem_nsems is set to nsems( number of semaphores in the set ) www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  14. If a new set is being created (typically in the server), we must specify nsems. If we are referencing an existing set (a client), we can specify nsems as 0. The semctl function is the catchall for various semaphore operations. • #include <sys/sem.h> • int semctl(int semid, int semnum, int cmd, ... /* union semun arg */);   • The fourth argument is optional, depending on the command requested, and if present, is of type semun, a union of various command-specific arguments: • union semun { • int val; /* for SETVAL */ • struct semid_ds *buf; /* for IPC_STAT and IPC_SET */ • unsigned short *array; /* for GETALL and SETALL */ • }; www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  15. Command value • IPC_STAT fetch semid_ds structure for this set • IPC_RMID remove semaphore set from the system • GETVAL return value of semval for the member semnum • SETVAL set the value of semval for the member semnum • GETPID return the value of sempid for the member semnum • GETALL fetch all semaphore values www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  16. Semop performs an array of operations on a semaphore set #include<sys/sem.h> Int semop(int semid,struct sembuf semoparray[],size nops); Return 0 if ok -1 on error Semoparray argument is a pointer to an array of semaphore operations,represented by sembuf structures Struct sembuf { unsigned short sem_num; short sem_op; short sem_flg; }; www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  17. Identifiers and Keys • XSI IPC are message queues, semaphores, and shared memory. • Each IPC structure (message queue, semaphore, or shared memory segment) in the kernel is referred to by a non-negative integer identifier. • To send or fetch a message to or from a message queue, for example, all we need know is the identifier for the queue. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

  18. The identifier is an internal name for an IPC object. • Cooperating processes need an external naming scheme to be able to rendezvous using the same IPC object. • For this purpose, an IPC object is associated with a key that acts as an external name. • Whenever an IPC structure is being created (by calling msgget, semget, or shmget), a key must be specified. • The data type of this key is the primitive system data type key_t, which is often defined as a long integer in the header <sys/types.h>. • This key is converted into an identifier by the kernel. www.BookSpar.com | Website for Students | VTU -NOTES -Question Papers

More Related