0% found this document useful (0 votes)
43 views5 pages

Unit 4 Message Queues Semaphores

The document explains the concept of IPC using message queues and semaphores, detailing how message queues allow processes to communicate by sending and receiving messages identified by type. It outlines the system calls for managing message queues, provides example C programs for writing and reading messages, and describes semaphores as synchronization mechanisms with two atomic operations: wait and signal. Additionally, it categorizes semaphores into binary, counting, general, strong, and busy-wait types, explaining their initialization and operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views5 pages

Unit 4 Message Queues Semaphores

The document explains the concept of IPC using message queues and semaphores, detailing how message queues allow processes to communicate by sending and receiving messages identified by type. It outlines the system calls for managing message queues, provides example C programs for writing and reading messages, and describes semaphores as synchronization mechanisms with two atomic operations: wait and signal. Additionally, it categorizes semaphores into binary, counting, general, strong, and busy-wait types, explaining their initialization and operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

IPC using Message Queues

A message queue is a linked list of messages stored within the kernel and identified by a message
queue identifier. A new queue is created or an existing queue opened by msgget().
New messages are added to the end of a queue by msgsnd(). Every message has a positive long integer
type field, a non-negative length, and the actual data bytes (corresponding to the length), all of which
are specified to msgsnd() when the message is added to a queue. Messages are fetched from a queue
by msgrcv(). We don’t have to fetch the messages in a first-in, first-out order. Instead, we can fetch
messages based on their type field.
All processes can exchange information through access to a common system message queue. The
sending process places a message (via some (OS) message-passing module) onto a queue which can be
read by another process. Each message is given an identification or type so that processes can select the
appropriate message. Process must share a common key in order to gain access to the queue in the first
place.

System calls used for message queues:


 ftok(): is use to generate a unique key.
 msgget(): either returns the message queue identifier for a newly created message queue or
returns the identifiers for a queue which exists with the same key value.
 msgsnd(): Data is placed on to a message queue by calling msgsnd().
 msgrcv(): messages are retrieved from a queue.
 msgctl(): It performs various operations on a queue. Generally it is use to destroy message
queue.
// C Program for Message Queue (Writer Process)
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX 10
// structure for message queue
struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;
int main()
{
key_t key;
int msgid;

// ftok to generate unique key


key = ftok("progfile", 65);
// msgget creates a message queue
// and returns identifier
msgid = msgget(key, 0666 | IPC_CREAT);
message.mesg_type = 1;
printf("Write Data : ");
fgets(message.mesg_text,MAX,stdin);
// msgsnd to send message
msgsnd(msgid, &message, sizeof(message), 0);
// display the message
printf("Data send is : %s \n", message.mesg_text);
return 0;
}

// C Program for Message Queue (Reader Process)


#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// structure for message queue
struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;
int main()
{
key_t key;
int msgid;

// ftok to generate unique key


key = ftok("progfile", 65);
// msgget creates a message queue
// and returns identifier
msgid = msgget(key, 0666 | IPC_CREAT);
// msgrcv to receive message
msgrcv(msgid, &message, sizeof(message), 1, 0);
// display the message
printf("Data Received is : %s \n", message.mesg_text);
// to destroy the message queue
msgctl(msgid, IPC_RMID, NULL);
return 0;
}

Semaphores
A semaphore is a signaling mechanism and a thread that is waiting on a semaphore can be signaled by
another thread. This is different than a mutex as the mutex can be signaled only by the thread that
called the wait function.
A semaphore uses two atomic operations, wait and signal for process synchronization.
A Semaphore is an integer variable, which can be accessed only through two
operations wait() and signal().
There are two types of semaphores: Binary Semaphores and Counting Semaphores
 Binary Semaphores: They can only be either 0 or 1. They are also known as mutex locks, as
the locks can provide mutual exclusion. All the processes can share the same mutex
semaphore that is initialized to 1. Then, a process has to wait until the lock becomes 0.
Then, the process can make the mutex semaphore 1 and start its critical section. When it
completes its critical section, it can reset the value of mutex semaphore to 0 and some other
process can enter its critical section.
 Counting Semaphores: They can have any value and are not restricted over a certain
domain. They can be used to control access to a resource that has a limitation on the number
of simultaneous accesses. The semaphore can be initialized to the number of instances of
the resource. Whenever a process wants to use that resource, it checks if the number of
remaining instances is more than zero, i.e., the process has an instance available. Then, the
process can enter its critical section thereby decreasing the value of the counting semaphore
by 1. After the process is over with the use of the instance of the resource, it can leave the
critical section thereby adding 1 to the number of available instances of the resource.
Semaphores and its types
Overview :
Semaphores are compound data types with two fields one is a Non-negative integer S.V and the second
is Set of processes in a queue S.L. It is used to solve critical section problems, and by using two atomic
operations, it will be solved. In this, wait and signal that is used for process synchronization.
States of the process :
Let’s go through the stages of the process that comes in its lifecycle. This will help in understanding
semaphore.
1. Running
It states that the Process in execution.
2. Ready
It states that the process wants to run.
3. Idle
The process runs when no processes are running
4. Blocked
The processes not ready not a candidate for a running process. It can be awakened by some external
actions.
5. Inactive
The initial state of the process. The process is activated at some point and becomes ready.
6. Complete
When a process executes its final statement.
Initialization of semaphore:
Semaphore S must be initialized with a value of S.V. > 0 and with empty S.L.
Semaphore S <- (k,φ)
Atomic Operations in semaphore:
Here we will discuss the two atomic operations wait and signal as follows.
Operation-1 :
Wait(S) :
According to the value of S.V. if it is non-zero, decrement its value and process p can continue its
execution and if it is zero, process p is added to set component and the state of the process p become
blocked in this case process p is said to have been blocked on the semaphore.
Algorithm –
if(S.V > 0)
{
S.V. = S.V -1
}
else{
S.L. = S.L. U p
p.state = blocked
}
Operation-2 :
Signal(S) :
According to the value of S.L., if it is empty increment the value of the integer and if it is non-empty
unblock q an arbitrary of the set of processes blocked o S.L. and change the status of p to ready.
Algorithm –
if(S.L. == φ){
S.V. = S.V.+1
}
else{
Let q be some process in S.L. S
S.L. = S.L. - {q}
q.state = ready
}
Types of Semaphores :
Here we will discuss the types of Semaphores as follows.
Type-1 :
General Semaphore :
A semaphore whose integer component can take arbitrary non-negative values of S.L. these are called
General Semaphore. They are kind of weak semaphore.
Type-2 :
Binary Semaphore :
A semaphore whose integer component S.L. takes only the values 0 and 1 is called a binary semaphore.
This is also known as “mutex” which stands for mutual exclusion.
Initialization –
S <- (0, φ) or S <- (1, φ)
Wait Operation –
It remains Unchanged as above.
Signal Operation –
It slightly changes as follows
Algorithm :
Signal(S) :
if (S.V.== 1)
{
// Undefined
// Return
}
else if (S.L == empty) { S.V.= 1 }
else
{
// Let q be some process in S.L.

S.L.= S.L.
-{q}
q.state = ready
}
Type-3 :
Strong Semaphore :
In Strong semaphore, S.L. remains unchanged as like weak semaphores whereas S.V. is replaced by the
queue. As because of removal of arbitrary process in weak semaphore it may lead to starvation whereas in
this case, it remains free from starvation.
Initialization –
S <- (0,empty)
Wait Operation –
Algorithm :
Wait(S) :
if (S.V > 0)
{
S.V.= S.V - 1
}
else
{
S.L.= S.L.U p
p.state = blocked
}
Signal Operation –
Algorithm :
Signal(S) :
if (S.L.== empty)
{
S.V.= S.V.+ 1
}
else
{
q = head(S.L.)
S.L. = S.L.
-{q}
q.state = ready
}
Type-4 :
Busy- Wait Semaphore :
It does not have a component S.L. and Semaphore S is identified only by S.V.Busy-Wait Semaphore are
appropriate in a multi-processor system where the waiting process has its own processor and is not
wasting CPU time that could be used for computation.
Wait Operation –
Algorithm :
Wait(S):
await S>0
S=S-1
Signal Operation –
Algorithm :
Signal(S):
S=S+1
Conclusion :
In this section, we have discussed semaphores and their types, and also we have discussed it’s atomic
operations. These are the basics of the Semaphore which are used for solving the problem of a critical
section.

You might also like