0% found this document useful (0 votes)
20 views41 pages

Slides 8 - IPC

The document provides an overview of Inter-Process Communication (IPC) methods, including message passing, message queues, shared memory, and semaphores. It details the operations and system calls associated with each IPC method, such as msgget, msgsnd, msgrcv for message queues, and shmget, shmat, shmdt for shared memory. Additionally, it includes examples of implementing these IPC mechanisms in C programming.

Uploaded by

osama.dafai18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views41 pages

Slides 8 - IPC

The document provides an overview of Inter-Process Communication (IPC) methods, including message passing, message queues, shared memory, and semaphores. It details the operations and system calls associated with each IPC method, such as msgget, msgsnd, msgrcv for message queues, and shmget, shmat, shmdt for shared memory. Additionally, it includes examples of implementing these IPC mechanisms in C programming.

Uploaded by

osama.dafai18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

INTER-PROCESS

COMMUNICATION
(IPC)
CONTENTS

• Message Passing
• Message Queues
• Example on Message Queues
• Shared Memory
• Semaphores
• Example on Shared Memory and Semaphores

2
Message Passing

• Message passing is a general method used for


IPC:
o for processes inside the same computer.
o for processes in a networked/distributed
system.

• The process may or may not be blocked while


3
sending a message or attempting to receive a
Synchronization in Message
Passing
• Message passing may be blocking or non-blocking.
• Blocking is considered synchronous
o Blocking send has the sender block until the message is
received
o Blocking receive has the receiver block until a message
is available
• Non-blocking is considered asynchronous
o Non-blocking send has the sender send the message
and continue 4
o Non-blocking receive has the receiver receive a valid
message or null
• Example: blocking send, blocking
receive:
o both are blocked until the message is
received.
o occurs when the communication link is
unbuffered (no message queue).
o provides tight synchronization 5
(rendezvous).
There are really 3 combinations here that make sense:
1. Blocking send, Blocking receive
2. Nonblocking send, Nonblocking receive
3. Nonblocking send, Blocking receive – most popular –
example:
Server process that provides services/resources to
other processes. It needs the expected information
6
before proceeding.
Message Queues

• One process establishes a message queue that others may


access.

• Producers add messages to a shared queue

• Consumer(s) remove messages

• OS takes care of memory management, synchronization

7
Messages Queues and Pipes
Compared

8
Unix System V Message Queues

• int msgget(key_t key, int msgflg)

• 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)

• int msgctl(int msqid, int cmd, struct msqid_ds * buf)

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

o const void *msgp: address of the message to be sent


o size_t msgsz: the size of the message to be sent.
o int msgflg: Two possible values:
 0: block, if the message queue is full
 IPC_NOWAIT: don’t wait if message queue is full
11
msgrcv System Call (Message Queue Operation)
• To retrieve message from the message queue.
• includes: <sys/types.h> <sys/ipc.h> <sys/msg.h>
• int msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
Returns:
oSuccess: number of bytes actually received
oFailure: -1;
Arguments:
o int msqid: the message queue identifier.
o void *msgp: a pointer to received message location (structure).
o size_t msgsz: the maximum size of the message in bytes.
o long msgtype: the type of the message to be retrieved.
o int msgflg: to indicate what action should be taken.
 0: error if size of message exceeds msgsz
 MSG_NOERROR: if size of message exceeds msgsz, accept msgsz bytes
 IPC_NOWAIT: return –1 with errno set to ENOMSG
12
Type of Message (in msgrcv System
Call)
int msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
• long msgtype: the type of the message to be retrieved.
o Actions for msgrcv as indicated by msgtyp value:

msgtyp value action

0 return first (oldest) message on queue

>0 return first message with type equal to msgtyp

<0 return the first message with lowest type less than or equal to msgtyp

13
msgctl System Call - Message Queue Control

Ownership and access permissions, established when the message


queue was created, can be examined and modified using the msgctl
system call.
• includes: <sys/types.h> <sys/ipc.h> <sys/msg.h>
• int msgctl(int msqid, int cmd, struct msqid_ds * buf)
Returns:
o Success: 0
o Failure: -1

14
Arguments of msgctl System Call

• int msqid: message queue identifier ( generated by msgget


call)

• int cmd: indicates one of the following actions:


• IPC_STAT: return the current value for each member of the msgid_ds
data structure (contains the permission structure).
• IPC_SET: the user can modify a limited number of msgid_ds structure
member value, such as msg_perm.uid, msg_perm.gid,
msg_perm.mode, and msg_qbytes.
• IPC_RMID: removes all associated message queue structure.

• struct msqid_ds *buf : pointer to a msgid_ds struct 15


Remove a Message Queue in a
Program

msgctl (msqid, IPC_RMID, (struct msqid_ds *)


0);

• To remove the message queue with key msqid.

• You must be the .

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;

if ((qid=msgget(123, IPC_CREAT | 0660)) < 0){


perror("msgget\n"); exit(1);
} 17
Example on Message Queues (sender.c -
continued)
printf("Enter a message to send to message queue: ");
fgets(msg.buf, 64, stdin);
msg.type = 1;

if(msgsnd(qid, &msg, 64, 0) < 0){


perror("msgsnd\n"); exit(1);
}

if(msgrcv(qid, &msg, 64, 2, 0) < 0){


perror("msgrcv\n");
exit(1);
}
printf("Received reply: %s\n", msg.buf);

if(msgctl(qid, IPC_RMID, NULL) < 0){


perror("msgctl\n"); exit(1);
}
exit(0);
} 18
Example on Message Queues
(receiver.c)
/* receiver.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;

int qid, length;


msg_t msg;

if ((qid=msgget(123, 0)) < 0){


perror("msgget\n"); exit(1);
} 19
Example on Message Queues (receiver.c -
continued)
printf(“Wating to receive a message from message queue.\n");

if(msgrcv(qid, &msg, 64, 0, 0) < 0){


perror("msgrcv\n"); exit(1);
}
printf("Received message: %s\n", msg.buf);
printf("Enter a reply message: ");
fgets(msg.buf, 64, stdin);
msg.type = 2;

if(msgsnd(qid, &msg, 64, 0) < 0){


perror("msgsnd\n"); exit(1);
}
exit(0);
} 20
UNIX System V Shared Memory
• Process first creates shared memory segment
segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
• Process wanting access to that shared memory must attach to it
shared_memory = (char *) shmat(segment_id, NULL, 0);
• Now the process could write to the shared memory
sprintf(shared_memory, "Writing to shared memory");
• When done a process can detach the shared memory from its address
space
shmdt(shared_memory);
• Now process can remove the shared memory segment 21
shmdt(shared_id, IPC_RMID, NULL);
Shared Memory vs Message Passing
Shared Memory Message Passing
0xFFFF 0xFFFF
Kernel Kernel
Memory Memory
Message
Queue
Write

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

• void *shmaddr: reference to attached memory segment (the


shared memory pointer).

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

int semget(key_t key, int nsems, int flag)


• key
o IPC_PRIVATE - new semaphore object created
o integer constant - if no IPC object matches key and
IPC_CREAT in flag, new semaphore is created.
• nsems - number of semaphores in set
• flag: permissions

29
semop - perform a semaphore operation

int semop(int semid, struct sembuf *opstr, size_t nops)


• semid - semaphore set id as returned by semget( )
• opstr
o an array of sembuf structures
o gives the sequence of semaphore operations to be
performed
• nops
o the number of semaphore operations in opstr
30
sembuf structure
struct sembuf {
short semnum;
// semaphore number: 0..nsems-1
short sem_op;
// semaphore operation to be performed
short sem_flg;
// operation flags: 0, IPC_NOWAIT, SEM_UNDO
}
31
Operations of semop
• sem_op > 0
• value of sem_op is added to semval
• release of resources the semaphore controls

• 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];

Each process will add its result to an accumulating result, sum :

sum = sum + sum1; sum = sum + sum2;

Sum will need to be shared and protected by a lock.

Shared data structure is created: 34


Shared memory locations for the example

35
36
37
38
39
40
41

You might also like