Assignment 3
Assignment 3
1(a) (Semaphore)
semaphore.
Queue<process> q;
};
P(semaphore s)
if (s.value == 1) {
s.value = 0;
else {
q.push(P) sleep();}}
V(Semaphore s)
if (s.q is empty) {
s.value = 1;}
else {
Process p = q.front();
// sent for CS
q.pop();
wakeup(p);
}}
1(b) (Producer & Consumer)
->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;
void producer()
--mutex;
++full;
--empty;
x++;
printf("\nProducer produces"
"item %d",
x);
++mutex;
void consumer()
{
--full;
++empty;
"item %d",
x);
x--;
++mutex;
int main()
int n, i;
scanf("%d", &n);
switch (n) {
case 1:
if ((mutex == 1)
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;