Operating Systems
CSE2005
Name-Anjali Sachdeva
Reg No-16BEC0014
1. PHILOSOPHER DINING PROBLEM
Input Code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/sem.h>
const int N_PHILOSOPHERS = 5; // Number of philosophers
const int MEALS_TO_HEAVEN = 10; // Number of iterations before
philosophers finish
const int MAX_DELAY = 500000; // Maximum delay between meal iterations
// Semaphore ids
int chopsticks; // ID for array of IPC semaphores
int not_at_table; // Start dinner when semaphore reaches 0
int philosopher(int n); // Defined after main()
int main(){
int i, status;
pid_t phil[N_PHILOSOPHERS];
printf("The Dining-Philosophers Problem\n");
// Parent process only:
//
// Allocate chopsticks: semaphores which are initially set to value
// 1. 5 chopsticks total in an array.
chopsticks = semget(IPC_PRIVATE, N_PHILOSOPHERS, IPC_CREAT | 0600);
for(i=0; i<N_PHILOSOPHERS; i++){
semctl(chopsticks, i, SETVAL, 1);
}
not_at_table = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
// Prevent children from starting to eat
semctl(not_at_table, 0, SETVAL, 5);
// Parent generates child processes
for(i=0; i < N_PHILOSOPHERS; i++){
int pid = fork();
if(pid == 0){ // child has pid 0
int ret = philosopher(i); // child acts as philosopher
exit(ret); // then exits
}
else{ // parent gets pid > 0
phil[i] = pid; // parent tracks children
}
}
// Parent waits on all children to finish
for(i = 0; i < N_PHILOSOPHERS; i++) {
waitpid(phil[i], &status, 0);
}
// Eliminate the chopsticks and table semaphores
semctl(chopsticks, 0, IPC_RMID, 0);
semctl(not_at_table, 0, IPC_RMID, 0);
return 0;
}
// Code for dining philosopher child processes
int philosopher(int n){
int i, j, first, second;
struct sembuf op; // Used to perform semaphore operations
op.sem_flg = 0;
srand(n); // Seed random number generator
// Avoid deadlock via slightly different order of chopstick requests
// for last philospher
first = (n < N_PHILOSOPHERS)? n : 0;
second = (n < N_PHILOSOPHERS)? n + 1 : N_PHILOSOPHERS-1;
// Check in for dinner
op.sem_op = -1;
op.sem_num = 0;
semop(not_at_table, &op, 1);
printf("Philosopher %d at the table\n", n);
// Wait for everyone to check in before start the meal
op.sem_op = 0;
op.sem_num = 0;
semop(not_at_table, &op, 1);
// Main loop of thinking/eating cycles
for(i = 0; i < MEALS_TO_HEAVEN; i++) {
int sleep_time = rand() % MAX_DELAY;
usleep(sleep_time); // sleep for 0-9 microseconds
printf("%2d: Philosopher %d is thinking ...\n", i,n);
/* get first chopstick */
op.sem_op = -1;
op.sem_num = first;
semop(chopsticks, &op, 1);
/* get second chopstick */
op.sem_op = -1;
op.sem_num = second;
semop(chopsticks, &op, 1);
printf("%2d: Philosopher %d is eating ...\n", i,n);
/* release first chopstick */
op.sem_op = +1;
op.sem_num = first;
semop(chopsticks, &op, 1);
/* release second chopstick */
op.sem_op = +1;
op.sem_num = second;
semop(chopsticks, &op, 1);
}
printf("Philosopher %d going to heaven\n",n);
exit(n);
}
Output :
2. BOUNDED BUFFER PROBLEM
Input Code:
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
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();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
return 0;
int wait(int s)
return (--s);
int signal(int s)
return(++s);
}
void producer()
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
void consumer()
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
Output:
3. READER WRITER PROBLEM
Input Code:
#include<stdio.h>
#include<stdlib.h>
int rw_mutex=1,mutex=1,x=0;
int main()
int n;
void writer();
void reader();
int wait(int);
int signal(int);
printf("\n1.Writer\n2.Reader\n3.Exit");
while(1)
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
case 1: if((mutex==1)&&(rw_mutex==1))
writer();
else
printf("Can't write");
break;
case 2: if((mutex==1)&&(rw_mutex==0))
reader();
else
printf("Can't read");
break;
case 3:
exit(0);
break;
return 0;
int wait(int s)
return (--s);
int signal(int s)
return(++s);
}
void writer()
mutex=wait(rw_mutex);
x++;
printf("\nWriter writes %d",x);
if(x==1)
rw_mutex=wait(rw_mutex);
mutex=signal(mutex);
void reader()
mutex=wait(mutex);
printf("\nReader reads %d",x);
x--;
if(x==0)
rw_mutex=signal(rw_mutex);
mutex=signal(mutex);
}
Output:
4. PRODUCER CONSUMER PROBLEM
Input Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
# define NUM_LOOPS 20
int main(int argc, char* argv[])
int sem_set_id;
nt child_pid;
int i;
struct sembuf sem_op;
int rc;
int val;
sem_set_id = semget(IPC_PRIVATE, 1, 0600);
if (sem_set_id == -1) {
perror("main: semget");
exit(1);
printf("Semaphore set created, semaphore set id '%d'.n", sem_set_id);
rc = semctl(sem_set_id, 0, SETVAL, 0);
child_pid = fork();
switch (child_pid) {
case -1:
perror("fork");
exit(1);
case 0:
for (i=0; i<NUM_LOOPS; i++) {
sem_op.sem_num = 0;
sem_op.sem_op = -1;
sem_op.sem_flg = 0;
semop(sem_set_id, &sem_op,1);
printf("consumer: '%d\n",i);
val=semctl(sem_set_id,0,GETVAL,0);
printf( "consumer after completion %d\n",val);
fflush(stdout);
sleep(3);
break;
default:
for (i=0; i<NUM_LOOPS; i++)
printf("producer: '%d\n", i);
fflush(stdout);
sem_op.sem_num = 0;
sem_op.sem_op = 1;
sem_op.sem_flg = 0;
semop(sem_set_id, &sem_op, 1);
val=semctl(sem_set_id,0,GETVAL,0);
sleep(2);
printf(" producer after completion %d\n",val);
break;
return 0;
}
Output:
5. Banker’s Algorithm
Input Code:
#include<stdio.h>
#define true 1
#define false 0
int m,n,i,j,count=0,process;
int max[10][10],alloc[10][10],need[10][10],c[10],avail[10],finish[10];
void readtable(int t[10][10])
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&t[i][j]);
void printtable(int t[10][10])
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf("\t%d",t[i][j]);
printf("\n");
void readvector(int v[10])
for(j=0;j<n;j++)
scanf("%d",&v[j]);
void printvector(int v[10])
for(j=0;j<n;j++)
printf("\t%d",v[j]);
void init()
printf("enter the number of process\n");
scanf("%d",&m);
printf("enter the number of resources\n");
scanf("%d",&n);
printf("enter the claim table\n");
readtable(max);
printf("enter the allocation table\n");
readtable(alloc);
printf("enter the max units of each resource\n");
readvector(c);
for(i=0;i<n;i++)
finish[i]=false;
void findavail()
{
int sum;
for(j=0;j<n;j++)
sum=0;
for(i=0;i<m;i++)
sum=sum+alloc[i][j];
avail[j]=c[j]-sum;
void findneed()
for(i=0;i<m;i++)
for(j=0;j<n;j++)
need[i][j]=max[i][j]-alloc[i][j];
void selectprocess()
int flag;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(need[i][j]<=avail[j])
flag=1;
else
flag=0;
break;
if((flag==1)&&(finish[i]==false))
process=i;
count++;
break;
printf("current status is\n");
printtable(alloc);
if(flag==0)
printf("system is in unsafe state\n");
exit(1);
}
printf("system is in safe state");
void executeprocess(int p)
printf("excuting process is %d",p);
printtable(alloc);
void releaseresource()
for(j=0;j<n;j++)
avail[j]=avail[j]+alloc[process][j];
for(j=0;j<n;j++)
alloc[process][j]=0;
need[process][j]=0;
main()
init();
findavail();
findneed();
do
{
selectprocess();
finish[process]=true;
executeprocess(process);
releaseresource();
}while(count<m);
printf("\n all proces executed correctly");
}
Output:
6. Pipes(altered code done in class)
Input Code:
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
main()
int pfd1[2],pfd2[2],p,x1,x2;
char msg3[100];
int x3;
char buf[20];
char msg1[100];
printf("input string in parent");
scanf("%s",msg1);
char msg2[100];
int i;
printf("inpur string in child");
scanf("%s",msg2);
x1=strlen(msg1);
x2=strlen(msg2);
for(i=0;i<x1+x2;i++){
if(i<x1)
msg3[i]=msg1[i];
else
msg3[i]=msg2[i-x1];
pipe(pfd1);
p=fork();
if(p==0)//from parent to child
close(pfd1[1]);
read(pfd1[0],buf,x3);
else //parent
close(pfd1[0]);
write(pfd1[1],msg3,x3);
printf("%s",buf);
//if(p==0)//from child to parent
Output: