0% found this document useful (0 votes)
37 views

Assignment 3

The document describes an assignment to demonstrate process synchronization using semaphores. It includes code for implementing the producer-consumer problem with semaphores. The key points are: 1) P and V operations are used to implement critical sections and synchronize access to shared resources between processes. P blocks if the semaphore value is 0, otherwise decrements. V increments if processes are waiting, otherwise does nothing. 2) The producer-consumer problem is solved using mutex, full, and empty semaphores. Producer increments full and decrements empty when adding an item. Consumer does the reverse when removing an item. 3) The producer sleeps if the buffer is full. The

Uploaded by

Kshitij Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Assignment 3

The document describes an assignment to demonstrate process synchronization using semaphores. It includes code for implementing the producer-consumer problem with semaphores. The key points are: 1) P and V operations are used to implement critical sections and synchronize access to shared resources between processes. P blocks if the semaphore value is 0, otherwise decrements. V increments if processes are waiting, otherwise does nothing. 2) The producer-consumer problem is solved using mutex, full, and empty semaphores. Producer increments full and decrements empty when adding an item. Consumer does the reverse when removing an item. 3) The producer sleeps if the buffer is full. The

Uploaded by

Kshitij Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 3

By:- Kshitij Sharma 102103374

1(a) (Semaphore)

Aim: Write a program that demonstrates how two processes can


share a variable using

semaphore.

Some points regarding P and V operation:

1. P operation is also called wait, sleep, or down operation, and V operation is


also called signal, wake-up, or up operation.

2. Both operations are atomic and semaphore(s) is always initialized to one.


Here atomic means that variable on which read, modify and update
happens at the same time/moment with no pre-emption i.e. in-between
read, modify and update no other operation is performed that may change
the variable.

3. A critical section is surrounded by both operations to implement process


synchronization. See the below image. The critical section of Process P is in
between P and V operation.
CODE:=
struct semaphore {

enum value(0, 1);

// q contains all Process Control Blocks (PCBs)

// corresponding to processes got blocked

// while performing down operation.

Queue<process> q;

};

P(semaphore s)

if (s.value == 1) {

s.value = 0;

else {

// add the process to the waiting queue

q.push(P) sleep();}}

V(Semaphore s)

if (s.q is empty) {

s.value = 1;}

else {

// select a process from waiting queue

Process p = q.front();

// remove the process from waiting as it has been

// sent for CS

q.pop();

wakeup(p);

}}
1(b) (Producer & Consumer)

Aim: To write a C program to implement the Producer & Consumer


Problem (Semaphore)

->The producer is to either go to sleep or discard data if the buffer is full. The
next time the consumer removes an item from the buffer, it notifies the producer,
who starts to fill the buffer again. In the same manner, the consumer can go to
sleep if it finds the buffer to be empty. The next time the producer puts data into
the buffer, it wakes up the sleeping consumer.

// C :-
#include <stdio.h>

#include <stdlib.h>

int mutex = 1;

int full = 0;

int empty = 10, x = 0;

void producer()

--mutex;

++full;

--empty;

x++;

printf("\nProducer produces"

"item %d",

x);

++mutex;

void consumer()

{
--full;

++empty;

printf("\nConsumer consumes "

"item %d",

x);

x--;

++mutex;

int main()

int n, i;

printf("\n1. Press 1 for Producer"

"\n2. Press 2 for Consumer"

"\n3. Press 3 for Exit");

for (i = 1; i > 0; i++) {

printf("\nEnter your choice:");

scanf("%d", &n);

switch (n) {

case 1:

if ((mutex == 1)

&& (empty != 0)) {

producer();}

else {

printf("Buffer is full!");

break;

case 2:

if ((mutex == 1)
&& (full != 0)) {

consumer();

// is empty

else {

printf("Buffer is empty!");

break;

case 3:

exit(0);

break;

You might also like