Docsity

Shared Memory - Linux System Programming - Lecture Notes, Study notes of Linux skills

Partial preview of the text

Download Shared Memory - Linux System Programming - Lecture Notes and more Study notes Linux skills in PDF only on Docsity!

Message Queues,

Semaphores,

Shared Memory

Message Queues

  • Basic idea of a message queue
    1. Two processes can exchange informa- tion via access to a common system message queue.
    2. A process places a message onto the queue which can be accessed and read by another process.
    3. Message queue has explicit length, has an identification(so called “type”).
    4. Blocking message passing is performed in the way that the sender wait for the receiver’s acknowledge of receiv- ing a message, so that it can continue sending message again.
    5. Non-blocking message passing means that it is not necessary for sender to 1

#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); } }

  1. Controlling a message queue

(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); } } }

  1. Sending and Receiving a message

(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.

  • Examples

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); } } }

Semaphores

  • Introduction to semaphores
    1. Memo Semaphores are a programming construct designed by E. W. Dijkstra in the late 1960s.
    2. Basic idea Dijkstra’s model was the operation of railroads: (a) A single railway track, only one train at a time is allowed.

(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.

  • Programming interface of Semaphores
    1. Creating a new semaphores (a) The function semget() initializes or gains access to a semaphore. It is prototyped by: #include <sys/sem.h> #include <sys/ipc.h> #include <sys/types.h>

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()/