OS_lab_BCS303.
OS_lab_BCS303.
Develop a C program to implement the Process system calls(fork(), exec(), wait, create process,
terminate process)
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main( )
{
int i, pid;
pid=fork( );
if(pid == -1)
{
printf("fork failed");
exit(0);
}
else if(pid==0)
{
printf("\n Child process starts");
for(i=0; i<5; i++)
{
printf("\n Child process %d is called", i);
}
}
else
{
wait(0);
printf("\n Parent process ends");
}
}
Output:
Child process starts
Child process 0 is called
Child process 1 is called
Child process 2 is called
Child process 3 is called
Child process 4 is called
Parent process ends
Program 2:
Simulate the following CPU scheduling algorithms to find turnaround time and waiting time
a)FCFS b) SJF C)Round robin d) Priority.
a) FCFS scheduling
#include<stdio.h>
int main()
{
char pn[10][10];
int arr[10], bur[10], star[10], finish[10], tat[10], wt[10], i, n;
int totwt=0, tottat=0;
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter the Process Name, Arrival Time & Burst Time:");
scanf("%s %d %d", &pn[i], &arr[i], &bur[i]);
}
for(i=0; i<n; i++)
{
if(i==0)
{
star[i]=arr[i];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
else
{
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
}
printf("\nPName Arrtime Burtime Start TAT Finish");
for(i=0;i<n;i++)
{
printf("\n%s\t%6d\t\t%6d\t%6d\t%6d\t%6d",pn[i],arr[i],bur[i],star[i],tat[i],
finish[i]);
totwt = totwt+wt[i];
tottat = tottat+ tat[i];
}
printf("\nAverage Waiting time:%f", (float)totwt/n);
printf("\nAverage Turn Around Time:%f", (float)tottat/n);
}
Output 1:
Output 2:
Enter the number of processes:3
Enter the Process Name, Arrival Time & Burst Time:P1 0 24
Enter the Process Name, Arrival Time & Burst Time:P2 0 3
Enter the Process Name, Arrival Time & Burst Time:P3 0 3
PName Arrtime Burtime Start TAT Finish
P1 0 24 0 24 24
P2 0 3 24 27 27
P3 0 3 27 30 30
Average Waiting time:17.000000
Average Turn Around Time:27.000000
b) Shortest Job First (SJF) scheduling
#include<stdio.h>
#include<string.h>
void main()
{
int i=0,pno[10],bt[10],n,wt[10],temp=0,j,tat[10];
float totwt,tottat;
float avgwt=0,avgtat=0;
printf("\n Enter the no of process ");
scanf("\n %d",&n);
printf("\n Enter the process numbers");
for(i=0;i<n;i++)
{
scanf("%d",&pno[i]);
}
printf("\n Enter the burst time of each process");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[i]>bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pno[i];
pno[i]=pno[j];
pno[j]=temp;
}
}
}
wt[0]=0;
for(i=1; i<n; i++)
{
wt[i] = bt[i-1] + wt[i-1];
totwt = totwt + wt[i];
}
printf("\n process no \t burst time\t waiting time \t turn around time\n");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
tottat= tottat+tat[i];
printf("\n %d \t\t %d\t\t %d \t\t %d", pno[i] , bt[i] , wt[i] , tat[i]);
}
printf("\n Average waiting time %f\n ", (float)totwt/n);
printf("\n Average turn around time %f \n",(float)tottat/n);
}
Output 1:
Enter the no of process 4
Enter the process numbers 1 2 3 4
Enter the burst time of each process 6 8 7 3
process no Burst time Waiting Time turn around time
4 3 0 3
1 6 3 9
3 7 9 16
2 8 16 24
Average waiting time 7.000000
Output 2:
Enter the no of process 4
Enter the process numbers 1 2 3 4
Enter the burst time of each process 21 3 6 2
process no burst time waiting time turn around time
4 2 0 2
2 3 2 5
3 6 5 11
1 21 11 32
Average waiting time 4.500000
P[2] | 9 | 4
P[3] | 11 | 8
P[4] | 14 | 10
P[1] | 21 | 12
Average Waiting Time= 8.500000
Avg Turnaround Time = 13.750000
d) Priority scheduling
#include <stdio.h>
int burst[n],priority[n],index[n];
for(int i=0;i<n;i++)
{
printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
scanf("%d %d",&burst[i],&priority[i]);
index[i]=i+1;
}
for(int i=0;i<n;i++)
{
int temp=priority[i],m=i;
for(int j=i;j<n;j++)
{
if(priority[j] > temp)
{
temp=priority[j];
m=j;
}
}
swap(&priority[i], &priority[m]);
swap(&burst[i], &burst[m]);
swap(&index[i],&index[m]);
}
int t=0;
int total_Turn_Around = 0;
for(int i=0; i < n; i++){
total_Turn_Around += burst[i];
}
float avg_Turn_Around = (float) total_Turn_Around / n;
printf("Average TurnAround Time is %f",avg_Turn_Around);
return 0;
}
Output:
Enter Number of Processes: 3
Enter Burst Time and Priority Value for Process 1: 5 2
Enter Burst Time and Priority Value for Process 2: 6 1
Enter Burst Time and Priority Value for Process 3: 7 3
Order of process Execution is
P3 is executed from 0 to 7
P1 is executed from 7 to 12
P2 is executed from 12 to 18
Program 3:
Develop a C program to simulate producer-consumer problem using semaphores.
#include<stdio.h>
void main()
{
int buffer[10], bufsize, in, out, produce, consume,count=0 ,choice=0;
in = 0;
out = 0;
bufsize = 5;
while(choice != 4)
{
printf("\n1. Produce\t2. Consume\t3. Display 4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
if( (in+1 ) %bufsize == out)
printf("\nBuffer is Full");
else
{
printf("\nEnter the value: ");
scanf("%d", &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
count++;
}
break;
case 2:
if(in == out)
printf("\nBuffer is Empty");
else
{
consume = buffer[out];
printf("\nThe consumed value is %d", consume); out = (out+1)%bufsize;
count--;
}
break;
case 3 :
if(count==0)
printf("Buffer empty\n");
else {
for(int i=0;i<count;i++)
printf("%d\t",buffer[i]);
}
} //switch close
} // while close
} //end
Output 1:
1. Produce 2. Consume 3. Display 4.Exit
Enter your choice: 1
Enter the value: 10
1. Produce 2. Consume 3. Display 4.Exit
Enter your choice: 1
Enter the value: 20
1. Produce 2. Consume 3. Display 4.Exit
Enter your choice: 1
Enter the value: 30
1. Produce 2. Consume 3. Display 4.Exit
Enter your choice: 2
The consumed value is 10
1. Produce 2. Consume 3. Display 4.Exit
Enter your choice: 3
10 20
1. Produce 2. Consume 3. Display 4.Exit
Enter your choice: 4
Output 2:
1. Produce 2. Consume 3. Display 4.Exit
Enter your choice: 2
Buffer is Empty
1. Produce 2. Consume 3. Display 4.Exit
Enter your choice: 4
Output 3:
// output when buffer size is 3
1. Produce 2. Consume 3. Display 4.Exit
Enter your choice: 1
Enter the value: 10
1. Produce 2. Consume 3. Display 4.Exit
Enter your choice: 1
Enter the value: 20
11. Produce 2. Consume 3. Display 4.Exit
Enter your choice:
30
1. Produce 2. Consume 3. Display 4.Exit
Enter your choice: 1
Buffer is Full
1. Produce 2. Consume 3. Display 4.Exit
Enter your choice: 4