Message Queues, Semaphores, Shared Memory
Message Queues, Semaphores, Shared Memory
Semaphores,
Shared Memory
Message Queues
#include <sys/msg.h>
#include <sys/ipc.h>
int msgget(key_t key, int msgflg);
(b) Parameters:
Key: an arbitrary integer number;
msgflg: the permission mode and
creation control flag.
(c) Example
#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*/
(b) Parameters:
msgid: message identifier, which is
the return value from msgget().
struct msgid_ds {
uid_t msg_perm.uid;
uid_t msg_perm.gid;
mode_t msg_perm.mode;
}
(c) Example
#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*/
(b) Parameters:
msgp: a pointer to a structure that
contains the type of the message
and its text.
The structure below is an example
of what this user-defined buffer might
look like:
struct mymsg {
/* message type */
long msgtype;
/* message text of length MSGSZ */
char mtext[MSGSZ];
}
• Examples
msgSend.c
#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;
/*declare an instance of structure of msg*/
struct my_msg sometext;
/* declare a buffer for text msg*/
char buffer[BUFSIZ];
/*initialize the key*/
key = 1234;
/* read and write permission for owner,
and create a message queue if not exists*/
msgflg = 0666 | IPC_CREAT;
msgRcv.c
#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;
/*declare an instance of structure of msg*/
struct my_msg sometext;
• Introduction to semaphores
2. Basic idea
Dijkstra’s model was the operation of
railroads:
#include <sys/sem.h>
#include <sys/ipc.h>
#include <sys/types.h>
(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>
int main()
{
key = 1234;
nsems = 1;
semflg = 0666 | IPC_CREAT;
if((semid = semget(key, nsems, semflg))
==-1){
perror("semget: semget failed");
exit(1);
}
printf(‘‘semget succeed\n’’);
exit(0);
}
2. Controlling semaphores
(b) Parameters:
It must be called with a valid semaphore
ID, semid.
The semnum value selects a semaphore
within an array by its index.
The union semun is optional, de-
pending upon the operation requested.
If required it is of type union se-
mun, which must be explicitly de-
clared by the application program.
“sem-union” is a member of this union.
The cmd argument is one of the fol-
lowing control flags:
SETVAL: Set the value of a single
semaphore.
IPC-RMID: Remove the specified
semaphores.
(c) Examples
union semun{
int val;
struct semid_ds *buf;
ushort *array;
}sem_union;
sem_union.val = 1;
i= semctl(semid,semnum,cmd,sem_union);
if (i == -1){
perror("semctl: semctl failed");
}
}
i= semctl(semid,semnum,cmd,sem_union);
if (i == -1){
perror("semctl: semctl failed");
}
}
3. Semaphores operation
(b) Parameters:
semid: the semaphore ID returned
by a previous semget() call.
sops: is a pointer to an array of
structures, each containing the fol-
lowing information about a semaphore
operation: The semaphore number
The operation to be performed Con-
trol flags, normally set as SEM-UNDO
to track on the semaphore changes
made by current process.
The sembuf structure specifies a semaphore
operation, as defined in < sys/sem.h >.
struct sembuf {
ushort_t sem_num;/* semaphore number */
short sem_op; /* semaphore operation */
short sem_flg;/* operation flags */
};
nsops: number of semaphores in the
array.
(c) Examples
i. waiting P(sv)
sem_b.sem_num = 0;
sem_b.sem_op =-1;/*P(sv)*/
sem_b.sem_flg = SEM_UNDO;
sem_b.sem_num = 0;
sem_b.sem_op = 1; /*V(sv)*/
sem_b.sem_flg = SEM_UNDO;
4. Application
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#define COUNTER_FILE "counter.txt"
union semun{
int val;
struct semid_ds *buf;
ushort *array;
}sem_union;
key = 1234;
nsems = 1;
semflg = 0666 | IPC_CREAT;
counter++;
write_counter(COUNTER_FILE,counter);
printf("write counter: %d\n", counter);
pause_time= rand() %3;
usleep(pause_time);
sem_union.val = 1;
i= semctl(semid,semnum,cmd,sem_union);
if (i == -1) return(0);
return(1);
}
i= semctl(semid,semnum,cmd,sem_union);
if (i == -1){
perror("semctl: semctl failed");
}
}
sem_b.sem_num = 0;
sem_b.sem_op =-1;/*P(sv)*/
sem_b.sem_flg = SEM_UNDO;
sem_b.sem_num = 0;
sem_b.sem_op = 1; /*V(sv)*/
sem_b.sem_flg = SEM_UNDO;
• Overview
Parameters:
key: an access value associated with
the semaphore ID.
size: the size (in bytes) of the re-
quested shared memory.
shmflg: specifies the initial access per-
missions and creation control flags.
Parameters:
cmd argument is one of following con-
trol commands:
SHM_LOCK
-- Lock the specified shared memory segment.
SHM_UNLOCK
-- Unlock the shared memory segment.
IPC_STAT
-- Return the status information contained
in the control structure and place it in
the buffer pointed to by buf.
IPC_SET
-- Set the effective user and group
identification and access permissions.
IPC_RMID
-- Remove the shared memory segment.
buf is a pointer to a type structure,
struct shmid_ds
1. shared_stuff.h
– define a structure contains the con-
tents of shared memory. A flag
written_by_you
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include "shared_stuff.h"
main()
{
int running =1;
void *shm = NULL;
struct shared_stuff_st *memo;
char buffer[SHMSZ];
int shmid;
key_t key;
key = 1234;
while(running){
while(memo -> written_by_you == 0){
sleep(1);
printf("waiting for client...\n");
}
/* Now put some things into the memory
* for the other process to read.*/
printf("Enter some text: ");
fgets(buffer, BUFSIZ, stdin);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include "shared_stuff.h"
main()
{
int running = 1;
void *shm = NULL;
struct shared_stuff_st *memo;
int shmid;
key_t key;
/* We’ll name our shared memory
* segment "1234" */
key = 1234;
THE END