Professional Documents
Culture Documents
ASSIGNMENT - 2 19BIT0084
Operating Systems Lab
1. A pair of processes involved in exchanging a sequence of integers. The number of integers that
can be produced and consumed at a time is limited to 100. Write a Program to implement the
producer and consumer problem using POSIX semaphore for the above scenario.
CODE :
//19BIT0084
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#include<stdlib.h>
pthread_mutex_t mutex;
int count;
int buffer[buffersize];
int lt = 0, c = 0;
void
display ()
void
initialize (int n)
count = 0;
full = 0;
empty = n;
display ();
}
void
buffer[count++] = input;
empty--;
int
read ()
full++;
return (buffer[--count]);
void *
int input;
input = lt++;
sem_wait (&empty);
pthread_mutex_lock (&mutex);
write (input);
c++;
if (c == 5)
printf ("\n");
c = 0;
pthread_mutex_unlock (&mutex);
sem_post (&full);
return (0);
void *
int input;
sem_wait (&full);
pthread_mutex_lock (&mutex);
c++;
if (c == 5)
printf ("\n");
c = 0;
pthread_mutex_unlock (&mutex);
sem_post (&empty);
return (0);
int
main ()
int n, i;
if (n > buffersize)
{
printf ("\n\n\n\n");
exit (0);
initialize (n);
sleep (5);
display ();
display ();
exit (0);
}
2. Write a Program to implement the solution for dining philosopher’s problem.
CODE :
//19BIT0084
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#define N 5
#define THINKING 2
#define HUNGRY 1
#define EATING 0
int state[N];
int phil[N] = { 0, 1, 2, 3, 4 };
sem_t mutex;
sem_t S[N];
if (state[phnum] == HUNGRY
state[phnum] = EATING;
sleep(2);
sem_post(&S[phnum]);
sem_wait(&mutex);
state[phnum] = HUNGRY;
test(phnum);
sem_post(&mutex);
sem_wait(&S[phnum]);
sleep(1);
sem_wait(&mutex);
state[phnum] = THINKING;
test(RIGHT);
sem_post(&mutex);
while (1) {
int* i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
int main()
int i;
pthread_t thread_id[N];
sem_init(&mutex, 0, 1);
sem_init(&S[i], 0, 0);
pthread_create(&thread_id[i], NULL,
philospher, &phil[i]);
pthread_join(thread_id[i], NULL);
}
}
3. Servers can be designed to limit the number of open connections. For example, a server may
wish to have only N socket connections at any point in time. As soon as N connections are made,
the server will not accept another incoming connection until an existing connection is released.
Write a program to illustrate how semaphores can be used by a server to limit the number of
concurrent connections.
CODE :
//19BIT0084
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <time.h>
#define NO_OF_PROCESSES_IN_QUEUE 5
#define NO_OF_SLOTS 1
int slots[NO_OF_SLOTS];
int frontView=-1;
int rearView=-1;
int sCount=0;
pthread_mutex_t mutex;
sem_t full;
sem_t empty;
pthread_detach(pthread_self());
sem_wait(&empty);
pthread_mutex_lock(&mutex);
rearView = rearView + 1;
rearView = 0;
slots[rearView] = process;
sem_post(&full);
sCount--;
int process = 0;
int processRemoved= 0;
sem_wait(&full);
pthread_mutex_lock(&mutex);
frontView = frontView + 1;
frontView = 0;
process = slots[frontView];
printf("%d Process is done. \n It was removed from the queue \n\n", process+1);
processRemoved++;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
sCount++;
printf("value after the sCount's increment by the Entry Code : %d \n\n",sCount);
usleep((int)(drand48()*50*1000*10));
int main(){
int i;
pthread_t supervisorid;
pthread_t queueId;
srand48(time(NULL));
pthread_mutex_init(&mutex,NULL);
sem_init(&full, 0, 5);
sem_init(&empty, 0, NO_OF_SLOTS);
pthread_join(queueId, NULL);
}
4. Write a Program to implement banker’s algorithm for Deadlock avoidance.
CODE :
//19BIT0084
#include <stdio.h>
#include <stdlib.h>
int main()
count = 0;
scanf("%d", &p);
completed[i] = 0;
scanf("%d", &Max[i][j]);
scanf("%d", &alloc[i][j]);
scanf("%d", &avail[i]);
do
printf("\t\t");
printf("\n");
process = -1;
process = i ;
process = -1;
break;
if(process != -1)
break;
if(process != -1)
{
safeSequence[count] = process + 1;
count++;
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
if(count == p)
printf(">\n");
else