






































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







































#include <sys/msg.h> #include <sys/ipc.h> #include <sys/types.h>
#include <unistd.h> #include <errno.h> #include <stdio.h>
int main() { /key to be passed to msgget()/ key_t key; /msgflg to be passed to msgget()/ int msgflg; /return value from msgget()/ int msgid;
key = 1234; /* read and write permission for owner, and create a message queue if not exists*/
msgflg = 0666 | IPC_CREAT;
if ((msgid = msgget(key, msgflg))== 1) { perror("msgget: msgget failed"); exit(1); } else{ printf("msgget succeeded\n"); exit(0); } }
(a) Function msgctl() alters the permis- sions and other characteristics of a message queue. int msgctl(int msgid, int cmd, struct msgid_ds *buf ) It returns 0 on success, -1 for failure.
(b) Parameters: msgid: message identifier, which is the return value from msgget().
#include <sys/msg.h> #include <sys/ipc.h> #include <sys/types.h>
#include <unistd.h> #include <errno.h> #include <stdio.h>
int main() { /key to be passed to msgget()/ key_t key; /msgflg to be passed to msgget()/ int msgflg; /return value from msgget()/ int msgid;
key = 1234; /* read and write permission for owner, and create a message queue if not exists*/
msgflg = 0666 | IPC_CREAT;
if((msgid = msgget(key, msgflg))== 1){ perror("msgget: msgget failed"); exit(1); }else{ printf("msgget succeeded\n"); if(msgctl(msgid, IPC_RMID, 0) == -1){ perror("msgctl: msgctl failed"); exit(1); }else{ printf("msgctl succeeded\n"); exit(0); } } }
(a) The msgsnd() and msgrcv() func- tions send and receive messages, re- spectively:
int msgsnd(int msgid, const void *msgp, size_t msgsz, int msgflg);
msgsz: specifies the length of the message in bytes. msgpriori: reception priority. Set 0 means you simply want to retrieve the message in the order in which they were sent. Or some other val- ues must be equal to the specified message type. msgflg: for msgsnd(), controls what happens if either the queue is full, or reaches the system limit on queued messages. Set as 0 means process suspends and wait for the space be- come available. msgflg: for msgrcv(), controls what happends if no message of the proper priority is waiting to be received. Set as 0 means process suspends and wait for an message with proper pri- ority to arrive.
msgSend.c
#include <sys/msg.h> #include <sys/ipc.h> #include <sys/types.h>
#include <unistd.h> #include <errno.h> #include <stdio.h>
#define MAX_TEXT 512
/define a message structure/ struct my_msg{ long msgtype; char message[MAX_TEXT]; };
int main() { /key to be passed to msgget()/ key_t key; /msgflg to be passed to msgget()/
/set the msgtype / sometext.msgtype = 1; /copy from buffer to message/ strcpy(sometext.message, buffer); /*send the message */ if (msgsnd(msgid, &sometext, MAX_TEXT, 0) == perror("msgsnd: msgsnd failed\n"); exit(1); } if(strncmp(buffer, "end", 3)== 0){ printf("message ends.\n"); exit(0); } } }
msgRcv.c
#include <sys/msg.h> #include <sys/ipc.h> #include <sys/types.h>
#include <unistd.h>
#include <errno.h> #include <stdio.h>
/define a message structure/ struct my_msg{ long msgtype; char message[BUFSIZ]; };
int main() { /key to be passed to msgget()/ key_t key; /msgflg to be passed to msgget()/ int msgflg; /return value from msgget()/ int msgid; /declare an instance of structure of msg/ struct my_msg sometext;
/initialize the key/ key = 1234; /* read and write permission for owner,*
perror("msgctl: msgctl failed"); exit(1); } printf("program succeeded\n"); exit(0); } } }
(b) Guarding this track is a semaphore.
(c) A train must wait before entering the single track until the semaphore is in a state that permits travel.
(d) When the train enters the track, the semaphore changes state to prevent other trains from entering the track. 2
V(sv): If some other process has been waiting for sv, then resume ex- ecution; If no process is suspended waiting for sv, increment sv.
int semget(key_t key, int nsems, int semflg);
When the call succeeds, it returns the semaphore ID (semid).
(b) Parameters:
key: is a access value associated with the semaphore ID. nsems: the number of semaphores required(since UNIX provides an ar- ray of individual semaphores, to give control of multiple resources). Set 1, means create one semaphore. semflg: specifies the initial access permissions and creation control flags.
(c) Example
#include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h>
#include <unistd.h> #include <errno.h> #include <stdio.h>
key_t key; /key to pass to semget()/ int semflg; /semflg to pass tosemget()/ int nsems; /nsems to pass to semget()/