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

Operating System Assignment 2

The document contains code for 4 programs implementing operating systems concepts: 1. Producer-consumer problem using POSIX semaphores with a buffer size of 100 2. Dining philosophers problem using mutexes and semaphores 3. Server limiting number of concurrent connections using semaphores 4. Banker's algorithm for deadlock avoidance using matrices to track resource allocation and availability
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
169 views

Operating System Assignment 2

The document contains code for 4 programs implementing operating systems concepts: 1. Producer-consumer problem using POSIX semaphores with a buffer size of 100 2. Dining philosophers problem using mutexes and semaphores 3. Server limiting number of concurrent connections using semaphores 4. Banker's algorithm for deadlock avoidance using matrices to track resource allocation and availability
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

KHYATI HITESH PARMAR

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 :

//KHYATI HITESH PARMAR

//19BIT0084

#include<stdio.h>

#include<semaphore.h>

#include<pthread.h>

#include<stdlib.h>

#define buffersize 100

pthread_mutex_t mutex;

pthread_t tidP[20], tidC[20];

int full, empty;

int count;

int buffer[buffersize];

int lt = 0, c = 0;

void

display ()

printf ("\nInputues semaphore: \nfull=%d\t\tempty=%d\n", full, empty);

void

initialize (int n)

pthread_mutex_init (&mutex, NULL);

count = 0;

full = 0;

empty = n;

display ();

}
void

write (int input)

buffer[count++] = input;

printf ("%d\t", input);

empty--;

int

read ()

full++;

return (buffer[--count]);

void *

producer (void *param)

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 *

consumer (void *param)

int input;

sem_wait (&full);

pthread_mutex_lock (&mutex);

input = read ();

printf ("%d\t", input);

c++;

if (c == 5)

printf ("\n");

c = 0;

pthread_mutex_unlock (&mutex);

sem_post (&empty);

return (0);

int

main ()

//printf ("\n This file is created by: Mridul Kumar 16BIT0452\n");

int n, i;

printf ("\nEnter the total integers to be produced: ");

scanf ("%d", &n);

if (n > buffersize)
{

printf ("\n\n\n\n");

exit (0);

initialize (n);

printf ("\nProducer produced integers\n");

for (i = 0; i < n; i++)

pthread_create (&tidP[i], NULL, producer, NULL);

sleep (5);

display ();

printf ("\nConsumers produced integers:\n");

for (i = 0; i < n; i++)

pthread_create (&tidC[i], NULL, consumer, NULL);

for (i = 0; i < n; i++)

pthread_join (tidP[i], NULL);

for (i = 0; i < n; i++)

pthread_join (tidC[i], NULL);

display ();

exit (0);

}
2. Write a Program to implement the solution for dining philosopher’s problem.

CODE :

//KHYATI HITESH PARMAR

//19BIT0084

#include <pthread.h>

#include <semaphore.h>

#include <stdio.h>

#define N 5

#define THINKING 2

#define HUNGRY 1

#define EATING 0

#define LEFT (phnum + 4) % N

#define RIGHT (phnum + 1) % N

int state[N];

int phil[N] = { 0, 1, 2, 3, 4 };

sem_t mutex;

sem_t S[N];

void test(int phnum)


{

if (state[phnum] == HUNGRY

&& state[LEFT] != EATING

&& state[RIGHT] != EATING) {

// state that eating

state[phnum] = EATING;

sleep(2);

printf("Philosopher %d takes fork %d and %d\n",

phnum + 1, LEFT + 1, phnum + 1);

printf("Philosopher %d is Eating\n", phnum + 1);

sem_post(&S[phnum]);

void take_fork(int phnum)

sem_wait(&mutex);

state[phnum] = HUNGRY;

printf("Philosopher %d is Hungry\n", phnum + 1);

test(phnum);

sem_post(&mutex);

sem_wait(&S[phnum]);

sleep(1);

void put_fork(int phnum)

sem_wait(&mutex);

state[phnum] = THINKING;

printf("Philosopher %d putting fork %d and %d down\n",

phnum + 1, LEFT + 1, phnum + 1);

printf("Philosopher %d is thinking\n", phnum + 1);


test(LEFT);

test(RIGHT);

sem_post(&mutex);

void* philospher(void* num)

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

for (i = 0; i < N; i++)

sem_init(&S[i], 0, 0);

for (i = 0; i < N; i++) {

pthread_create(&thread_id[i], NULL,

philospher, &phil[i]);

printf("Philosopher %d is thinking\n", i + 1);

for (i = 0; i < N; 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 :

//KHYATI HITESH PARMAR

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

void *Signal(void *parameter){

while( sCount < -5) {

int process = *((int*)(&parameter));

pthread_detach(pthread_self());

sem_wait(&empty);

pthread_mutex_lock(&mutex);

rearView = rearView + 1;

if (rearView >= NO_OF_SLOTS){

rearView = 0;

slots[rearView] = process;

printf("%d.Process was called \n", process+1);


pthread_mutex_unlock(&mutex);

sem_post(&full);

sCount--;

printf(" Counting value sCount after decrement by Exit Code : %d\n",sCount);

void *Wait(void *parameter){

int process = 0;

int processRemoved= 0;

printf("Processes started \n");

while (processRemoved < NO_OF_PROCESSES_IN_QUEUE){

sem_wait(&full);

pthread_mutex_lock(&mutex);

frontView = frontView + 1;

if (frontView >= NO_OF_SLOTS )

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_create(&queueId, NULL, Wait, NULL);

for(i=0; i < NO_OF_PROCESSES_IN_QUEUE;i++){

pthread_create(&supervisorid, NULL, Signal , (void *)i);

pthread_join(queueId, NULL);

}
4. Write a Program to implement banker’s algorithm for Deadlock avoidance.

CODE :

//KHYATI HITESH PARMAR

//19BIT0084

#include <stdio.h>

#include <stdlib.h>

int main()

int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];

int p, r, i, j, process, count;

count = 0;

printf("Enter the no of processes : ");

scanf("%d", &p);

for(i = 0; i< p; i++)

completed[i] = 0;

printf("\n\nEnter the no of resources : ");


scanf("%d", &r);

printf("\n\nEnter the Max Matrix for each process : ");

for(i = 0; i < p; i++)

printf("\nFor process %d : ", i + 1);

for(j = 0; j < r; j++)

scanf("%d", &Max[i][j]);

printf("\n\nEnter the allocation for each process : ");

for(i = 0; i < p; i++)

printf("\nFor process %d : ",i + 1);

for(j = 0; j < r; j++)

scanf("%d", &alloc[i][j]);

printf("\n\nEnter the Available Resources : ");

for(i = 0; i < r; i++)

scanf("%d", &avail[i]);

for(i = 0; i < p; i++)

for(j = 0; j < r; j++)

need[i][j] = Max[i][j] - alloc[i][j];

do

printf("\n Max matrix:\tAllocation matrix:\n");


for(i = 0; i < p; i++)

for( j = 0; j < r; j++)

printf("%d ", Max[i][j]);

printf("\t\t");

for( j = 0; j < r; j++)

printf("%d ", alloc[i][j]);

printf("\n");

process = -1;

for(i = 0; i < p; i++)

if(completed[i] == 0)//if not completed

process = i ;

for(j = 0; j < r; j++)

if(avail[j] < need[i][j])

process = -1;

break;

if(process != -1)

break;

if(process != -1)
{

printf("\nProcess %d runs to completion!", process + 1);

safeSequence[count] = process + 1;

count++;

for(j = 0; j < r; j++)

avail[j] += alloc[process][j];

alloc[process][j] = 0;

Max[process][j] = 0;

completed[process] = 1;

while(count != p && process != -1);

if(count == p)

printf("\ndeadlock will not occur\n");

printf("Safe Sequence : < ");

for( i = 0; i < p; i++)

printf("%d ", safeSequence[i]);

printf(">\n");

else

printf("\ndeadlock will occur");

You might also like