Slides 8 - IPC
Slides 8 - IPC
COMMUNICATION
(IPC)
CONTENTS
• Message Passing
• Message Queues
• Example on Message Queues
• Shared Memory
• Semaphores
• Example on Shared Memory and Semaphores
2
Message Passing
7
Messages Queues and Pipes
Compared
8
Unix System V Message Queues
• int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg)
• int msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
9
msgget System Call
To create a message queue.
• includes: <sys/types.h> <sys/ipc.h> <sys/msg.h>
• int msgget(key_t key, int msgflg)
Returns:
o Success: message queue identifier
o Failure: -1
Arguments:
o key: to be specified directly by the user or generated using
ftok.
o msgflg: IPC_CREAT, IPC_EXCL or permission value.
10
msgsnd System Call (Message Queue Operation)
• To place (send) a message in the message queue.
• includes: <sys/types.h> <sys/ipc.h> <sys/msg.h>
• int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg)
Returns: The msgp argument is a pointer to a caller-
o Success: 0 defined structure of the following general
form:
o Failure: -1 struct msgbuf {
long mtype; /* message type,
Arguments: must be > 0 */
char mtext[1]; /* message data */
o int msgid: valid message queue identifier };
<0 return the first message with lowest type less than or equal to msgtyp
13
msgctl System Call - Message Queue Control
14
Arguments of msgctl System Call
16
Example – Message Queues
(sender.c)
/* sender.c program */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/ipc.h>
int main() {
typedef struct{
long type;
char buf[64];
} msg_t;
msg_t msg;
int qid;
int main() {
typedef struct{
long type;
char buf[64];
} msg_t;
Process Process
2 2 Read
Write
Shared
Memory Read
Process Process
1 1
0x0000 0x0000 22
shmget System Call
• Used to create or access a shared memory segment.
includes: <sys/types.h> <sys/ipc.h> <sys/shm.h>
int shmget(key_t key, int size, int shmflg);
Returns:
o Success: unique shared memory identifier
o Failure: -1
• key_t key: key for creating/accessing shared memory
• int size: size in bytes of shared memory segment. 0 for accessing
existing segment.
• int shmflg:
o Creating: IPC_CREAT and permissions
o 0 for accessing only 23
shmat System Call
• Used to attach (map) the referenced shared memory segment
into the calling process’s data segment. The pointer returned is
to the first byte of the segment.
includes: <sys/types.h> <sys/ipc.h> <sys/shm.h>
void *shmat(int shmid, void *shmaddr, int shmflg);
Returns:
o Success: Reference to the data segment address of
shared memory
o Failure: -1
• int shmid: a valid shared memory identifier.
• void *shmaddr: to assign the location of the shared memory
(can use 0)
24
• int shmflg: access permissions and attachment conditions (can
use 0.
shmdt System Call
• Used to detach the calling process’s data segment from the shared
memory segment.
includes: <sys/types.h> <sys/ipc.h> <sys/shm.h>
int shmdt (void * shmaddr);
Returns:
o Success: 0
o Failure: -1
25
shmctl System Call - Shared Memory
Control
• Permits the user to perform a number of generalized control
operations on an existing shared memory segment and on the
system shared memory data structure.
includes: <sys/types.h> <sys/ipc.h> <sys/shm.h>
int shmctl(int shmid, int cmd, struct shmid_ds * buf);
Returns:
o Success: 0
o Failure: -1
• int shmid: a valid shared memory segment identifier.
• int cmd: the operation shmctl is t perform.
• struct shmid_ds * buf: a reference to the shmid_ds structure
26
Operations of shmctl()
• IPC_STAT: Return the current value of the shmid_ds structure for the
shared memory segment indicated by the shmid value.
• IPC_SET: Modify a limited number of members in the permission
structure found within the shmid_ds structure.
• IPC_RMID: Remove the system data structure for the referenced
shared memory identifier (shmid). Once all references to the
shared memory segment are eliminated, the system will remove
the actual shared memory segment.
• SHM_LOCK: Lock, in memory, the shared memory segment
referenced by the shmid argument. Superuser access required
• SHM_UNLOCK: Unlock the shared memory segment referenced by
the shmid argument. Superuser access required
27
Unix System V Semaphores
• semget( )
o creates a semaphore set or accesses an existing
semaphore set.
• semop( )
o performs operations on one or more of the semaphores
• semctl( )
o performs various control operations on a semaphore
28
semget - obtain a semaphore set
29
semop - perform a semaphore operation
• sem_op == 0
• wait until semval is zero
• immediate return if already zero
• sem_op < 0
• wait until semval ≥ | sem_op | then add sem_op to semval
(decreases semval due to negative value)
• allocation of resources the semaphore controls
32
Critical Section Control
• Initial semval is 1
• Enter Critical Section
• sem_op = -1
• sem_flg = 0
• Exit Critical Section
• sem_op = 1
• sem_flg = 0
33
Example on Shared Memory and
Semaphores
Calculation will be divided into two parts, one doing even i and one doing odd i
Process 1 Process 2
sum1 = 0; sum2 = 0;
for (i = 0; i < 1000; i = i + 2) for (i = 1; i < 1000; i = i + 2)
sum1 = sum1 + a[i]; sum2 = sum2 + a[i];
35
36
37
38
39
40
41