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

OS_lab_BCS303.

The document contains C programs that implement various system calls and CPU scheduling algorithms. It includes examples for process creation and termination using fork and wait, as well as simulations for FCFS, SJF, Round Robin, and Priority scheduling algorithms. Additionally, it features a producer-consumer problem simulation using semaphores.

Uploaded by

niwic12695
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)
10 views

OS_lab_BCS303.

The document contains C programs that implement various system calls and CPU scheduling algorithms. It includes examples for process creation and termination using fork and wait, as well as simulations for FCFS, SJF, Round Robin, and Priority scheduling algorithms. Additionally, it features a producer-consumer problem simulation using semaphores.

Uploaded by

niwic12695
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/ 16

Program1 :

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

Output :(without wait() system call )


Parent process ends
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

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:

Enter the number of processes:5


Enter the Process Name, Arrival Time & Burst Time:P3 0 4
Enter the Process Name, Arrival Time & Burst Time:P4 0 7
Enter the Process Name, Arrival Time & Burst Time:P1 2 2
Enter the Process Name, Arrival Time & Burst Time:P2 5 6
Enter the Process Name, Arrival Time & Burst Time:P5 7 4
PName Arrtime Burtime Start TAT Finish
P3 0 4 0 4 4
P4 0 7 4 11 11
P1 2 2 11 11 13
P2 5 6 13 14 19
P5 7 4 19 16 23
Average Waiting time:6.600000
Average Turn Around Time:11.200000

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

Average turn around time 13.000093

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

Average turn around time 12.500000


c) Round Robin scheduling
#include<stdio.h>
int main()
{
int cnt,j,n,t,remain,flag=0,tq;
int wt=0,tat=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(cnt=0;cnt<n;cnt++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d :",cnt+1);
scanf("%d",&at[cnt]);
scanf("%d",&bt[cnt]);
rt[cnt]=bt[cnt];
}
printf("Enter Time Quantum:\t");
scanf("%d",&tq);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(t=0,cnt=0;remain!=0;)
{
if(rt[cnt]<=tq && rt[cnt]>0)
{
t+=rt[cnt];
rt[cnt]=0;
flag=1;
}
else if(rt[cnt]>0)
{
rt[cnt]-=tq;
t+=tq;
}
if(rt[cnt]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",cnt+1,t-at[cnt],t-at[cnt]-bt[cnt]);
wt+=t-at[cnt]-bt[cnt];
tat+=t-at[cnt];
flag=0;
}
if(cnt==n-1)
cnt=0;
else if(at[cnt+1]<=t)
cnt++;
else
cnt=0;
}
printf("\nAverage Waiting Time= %f\n",wt*1.0/n);
printf("Avg Turnaround Time = %f",tat*1.0/n);
return 0;
}
Output:
Enter Total Process: 4
Enter Arrival Time and Burst Time for Process Process Number 1 :0 9
Enter Arrival Time and Burst Time for Process Process Number 2 :1 5
Enter Arrival Time and Burst Time for Process Process Number 3 :2 3
Enter Arrival Time and Burst Time for Process Process Number 4 :3 4
Enter Time Quantum: 5
Process |Turnaround Time|Waiting Time

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>

void swap(int *a,int *b)


{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int n;
printf("Enter Number of Processes: ");
scanf("%d",&n);

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;

printf("Order of process Execution is\n");


for(int i=0;i<n;i++)
{
printf("P%d is executed from %d to %d\n",index[i],t,t+burst[i]);
t+=burst[i];
}
printf("\n");
printf("Process Id\tBurst Time\tWait Time\n");
int wait_time=0;
int total_wait_time = 0;
for(int i=0;i<n;i++)
{
printf("P%d\t\t%d\t\t%d\n",index[i],burst[i],wait_time);
total_wait_time += wait_time;
wait_time += burst[i];
}

float avg_wait_time = (float) total_wait_time / n;


printf("Average waiting time is %f\n", avg_wait_time);

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

Process Id Burst Time Wait Time


P3 7 0
P1 5 7
P2 6 12
Average waiting time is 6.333333
Average TurnAround Time is 6.000000

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

You might also like