Os Programs C
Os Programs C
//EXEC.c
#include<stdio.h>
#include<unistd.h>
int main()
{
int i;
return 0;
}
P2 0 11
P3 0 11
Gantt Chart:
advertisement
Waiting Time: Time Difference between turnaround time and burst time.
Waiting Time = Turnaround Time – Burst Time
P1 waiting time: 0
P2 waiting time: 5
P3 waiting time: 16
1. /*
2. * FCFS Scheduling Program in C
3. */
4.
First Come First Serve with Arrival time in C programming language code
#include<stdio.h>
int a[10],b[10],no[10],wt[10],ta[10],st;
void main()
{
int i,j,sb=0,n,l,temp,c;
float avgw,tw=0,tt=0,avgt;
printf("Enter no of processes\n");
scanf("%d",&n);
for(i=0;i<n;i++)
no[i]=i;
for(i=0;i<n;i++)
{
printf("Enter Arrival Time for process %d\n",i);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
printf("Enter Burst Time for process %d\n",i);
scanf("%d",&b[i]);
}
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
temp=no[j];
no[j]=no[j+1];
no[j+1]=temp;
}
l=a[0];
for(i=0;i<n;i++) //For getting process with least arrival time.
{
if(l>a[i])
l=a[i];
}
for(i=0;i<n;i++)
{
if(i==0)
sb=l;
else
{
sb=sb+b[i-1];
st=sb;
wt[i]=st-a[i];
}
}
for(i=0;i<n;i++)
ta[i]=wt[i]+b[i];
for(i=0;i<n;i++)
tw=tw+wt[i];
avgw=tw/n;
for(i=0;i<n;i++)
tt=tt+ta[i];
avgt=tt/n;
/*printf("\tGantt Chart :\n");
printf("\t+");
for(i=0;i<n;i++)
printf("-------+");
printf("\n");
printf("\t|");
for(i=0;i<n;i++)
printf(" %d |",no[i]);
printf("\n");
printf("\t+");
for(i=0;i<n;i++)
printf("-------+");
printf("\n\t%d",l);
c=l;
for(i=0;i<n;i++)
{
c=c+b[i];
printf("\t%d",c);
}
printf("\n\n");*/
printf("\tProcess\t\tAT\t\tBT\t\tWT\t\tTAT\n");
for(i=0;i<n;i++)
{
printf("\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",no[i],a[i],b[i],wt[i],ta[i]);
}
printf("\nAverage WT is %f\n",avgw);
printf("\nAverage TAT is %f\n",avgt);
}
/*output
Enter Arrival Time for process 1
2
Enter Arrival Time for process 2
3
Enter Arrival Time for process 3
4
Enter Burst Time for process 0
2
Enter Burst Time for process 1
1
Enter Burst Time for process 2
3
Enter Burst Time for process 3
7
Process AT BT WT TAT
0 1 2 0 2
1 2 1 1 2
2 3 3 1 4
3 4 7 3 10
Average WT is 1.250000
Program Explanation
1. Initialize two array pid[] and bt[] of size 15.
2. Ask the user for number of processes n.
3. Ask the user for process id and burst time for all n processes and store them
into pid[] and bt[] respectively.
4. Calculate waiting time of each process by the formula wt[i] = wt[i-1] + bt[i-1].
5. Print Process Id, Burst Time, waiting time and Turnaround time of each process in
tabular manner.
6. Calculate and print turnaround time of each process = bt[i] + wt[i].
7. Add waiting time of all the processes and store it in the variable twt.
8. Add turnaround time of all the processes and store it in the variable tat.
9. Calculate average waiting time as awt = twt/n.
10. Calculate average turnaround time as att = tat/n;
11. Print average waiting time and average turnaround time.
12. Exit.
Time Complexity: O(n)
Time complexity of the FCFS Scheduling program is O(n), as the for loop runs for n
number of processes.
Space Complexity: O(n)
In the above program, space complexity is O(n) as arrays of size n have been initialized
to store the values in it.
Run Time Testcases
In this case, we enter “3” as the number of processes, and the burst time are “p1: 5”, “p2:
11”, and “p3: 11” to find average waiting time and average turnaround time using FCFS
Scheduling algorithm.
P1 0 5
P2 0 4
P3 0 12
P4 0 7
Gantt Chart:
advertisement
Waiting Time: Time Difference between turnaround time and burst time.
Waiting Time = Turnaround Time – Burst Time
P1 waiting time: 4
P2 waiting time: 0
P3 waiting time: 16
P4 waiting time: 9
#include<stdio.h>
int main()
{
int at[10],bt[10],pr[10];
int n,i,j,temp,time=0,count,over=0,sum_wait=0,sum_turnaround=0,start;
float avgwait,avgturn;
printf("Enter the number of processes\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the arrival time and burst time for process %d\n",i+1);
scanf("%d%d",&at[i],&bt[i]);
pr[i]=i+1;
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(at[i]>at[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
}
}
}
printf("\n\nProcess\t|Arrival time\t|Burst time\t|Start time\t|End
time\t|waiting time\t|Turnaround time\n\n");
while(over<n)
{
count=0;
for(i=over;i<n;i++)
{
if(at[i]<=time)
count++;
else
break;
}
if(count>1)
{
for(i=over;i<over+count-1;i++)
{
for(j=i+1;j<over+count;j++)
{
if(bt[i]>bt[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
}
}
}
}
start=time;
time+=bt[over];
printf("p[%d]\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\t|\t%d\n",pr[over],
at[over],bt[over],start,time,time-at[over]-bt[over],time-
at[over]);
sum_wait+=time-at[over]-bt[over];
sum_turnaround+=time-at[over];
over++;
}
avgwait=(float)sum_wait/(float)n;
avgturn=(float)sum_turnaround/(float)n;
printf("Average waiting time is %f\n",avgwait);
printf("Average turnaround time is %f\n",avgturn);
return 0;
}
OUTPUT :
}
/* Sample-output
Advantages of SJF
Disadvantages of SJF
There are chances that this algorithm will face starvation if there are too many
processes that have shorter burst times then it will cause longer processes to
wait in the queue will lead to Starvation.
This algorithm can lead to a long turnaround time.
It is not an easy task to get knowledge about the length of future jobs.
Program Explanation
1. Initialize two array pid[] and bt[] of size 20.
2. Ask the user for number of processes n.
3. Ask the user for process id and burst time for all n processes and store them
into pid[] and bt[] respectively.
4. Sort all the processes according to their burst time.
5. Assign waiting time = 0 to the smallest process.
6. Calculate waiting time of each process by using two loops and adding all the burst
time of previously completed processes.
7. Print Process Id, Burst Time, waiting time and Turnaround time of each process in
tabular manner.
8. Calculate and print turnaround time of each process = bt[i] + wt[i].
9. Add waiting time of all the processes and store it in the variable total.
10. Add turnaround time of all the processes and store it in the variable totalT.
11. Calculate average waiting time as avg_wt = total/n.
12. Calculate average turnaround time as avg_tat = totalT/n;
13. Print average waiting time and average turnaround time.
14. Exit.
Time Complexity: O(n2)
The above program for implementing SJF Scheduling has a time complexity of O(n2),
as the for loop runs for n^2 times for calculating waiting time of each process.
Space Complexity: O(n)
In the SJF Scheduling program, space complexity is O(n) as arrays of size n have
been initialized to store the values in it.
Run Time Testcases
In this case, we enter “3” as the number of processes, and the burst time are “p1: 5”,
“p2: 4”, “p3: 12”, and “p4: 7” to find average waiting time and average turnaround
time using SJF Scheduling algorithm.
Example:
Following is the example of non preemptive scheduling with arrival time zero.
P1 5 1
P2 7 6
P3 2 4
P4 3 5
Since scheduling is non preemptive, which means that the process will be fully
executed once its execution is started. So processes will be executed in the same
order of priority.
Turn Around
Process Id Burst Time Wait Time
Time
P2 7 0 7
P4 3 7 10
P3 2 10 12
P1 5 12 17
Problem Solution:
Priority Scheduling Algorithm:
Step 1: Start the Program.
Step 2: Input the number of processes.
Step 3: Input the burst time and priority for each process.
Step 4: Sort the element on the basis of priority.
Step 5: Print order of execution of their process with their time stamp (wait time and
turnaround time).
Step 6: End the Program.
Program/Source Code
Here is the source code of the C program to implement priority scheduling. The C
program is successfully compiled and run on a Linux system. The program output is
also shown below.
#include<stdio.h>
#include<string.h>
int main()
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
scanf("%d",&n);
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
if(i==0)
st[i]=at[i];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
else
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],
p[i ],wt[i],ta[i]);
return 0;
OUTPUT:
P1 0 10
P2 1 8
P3 2 7
Time Slot is 5 Sec.
First P1 is executed for 5 seconds, left burst time is 5 sec
Then P2 is executed for 5 seconds, left burst time is 3 sec
Then P3 is executed for 5 seconds, left burst time is 2 sec
Then P1 is executed for 5 seconds, execution of P1 is completed.
Then P2 is executed for 3 seconds, execution of P2 is completed.
Then P1 is executed for 2 sec, execution P3 is completed.
Execution of all processes completed
P1 10 20 10
P2 8 22 14
P3 7 23 16
Average Waiting Time = (20 + 22 + 23)/3 = 65/3 = 21.666666
Average Turnaround Time = (10 + 14 + 16)/3 = 40/3 = 13.333333
Advantages:
There is no starvation of resources as each process gets equal share.
Every Process gets equal allocation of CPU.
Increases Performance time in terms of response time.
Disadvantages:
More time is wasted on context switching.
Frequent context switching increases CPU overhead.
Average Waiting time increases for each process.
Program/Source Code
Here is the source code of the C program to implement Round Robin scheduling.
The C program is successfully compiled and run on a Linux system. The program
output is also shown below.
/*
* Round Robin Scheduling Program in C
*/
#include<stdio.h>
int main()
{
//Input no of processed
int n;
printf("Enter Total Number of Processes:");
scanf("%d", &n);
int wait_time = 0, ta_time = 0, arr_time[n], burst_time[n], temp_burst_time[n];
int x = n;
Process No 1 10 20 10
Process No 2 8 22 14
Process No 3 7 23 16
Program Explanation
1. Ask the user for number of processes n.
2. After that, ask the user for the arrival time and burst time of each process. Also input
the time quantum.
3. In the loop, if time slot is greater than left burst time, execute process and find burst
time.
4. Else if burst time is greater than time slot, execute it up to time slot and again push
into the queue.
5. when the execution is completed, print the process information such as turnaround
time and waiting time.
Time Complexity: O(total execution time)
All processes are executed up to their execution, so time complexity is O(total execution
time).
Space Complexity: O(n)
Space is required to store burst time and arrival time, so space complexity is O(n).
Run Time Testcases
In this case, we enter “3” as the number of processes, and the arrival time and burst
time are “p1: 0 10”, “p2: 1 8”, and “p3: 2 7” to find average waiting time and average
turnaround time using Round Robin Scheduling algorithm. (Quantum Time = 5)
3.producer-consumer problem using semaphores in C
#include <stdio.h>
#include <stdlib.h>
// Initialize a mutex to 1
int mutex = 1;
// Item produced
x++;
printf("\nProducer produces"
"item %d",
x);
int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");
for (i = 1; i > 0; i++)
{
printf("\nEnter your choice:");
scanf("%d", &n);
// Switch Cases
switch (n) {
case 1:
case 2:
// Exit Condition
case 3:
exit(0);
break;
}
}
}
#include<stdio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
int i,j;
input();
show();
cal();
return 0;
void input()
{
int i,j;
scanf("%d",&n);
scanf("%d",&r);
for(i=0;i<n;i++)
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
for(i=0;i<n;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
void show()
int i,j;
for(i=0;i<n;i++)
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
printf("%d ",alloc[i][j]);
printf("\t");
for(j=0;j<r;j++)
printf("%d ",max[i][j]);
printf("\t");
if(i==0)
for(j=0;j<r;j++)
printf("%d ",avail[j]);
void cal()
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int safe[100];
int i,j;
for(i=0;i<n;i++)
finish[i]=0;
for(i=0;i<n;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
printf("\n");
while(flag)
{
flag=0;
for(i=0;i<n;i++)
int c=0;
for(j=0;j<r;j++)
if((finish[i]==0)&&(need[i][j]<=avail[j]))
c++;
if(c==r)
for(k=0;k<r;k++)
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
printf("P%d->",i);
if(finish[i]==1)
i=n;
}
}
for(i=0;i<n;i++)
if(finish[i]==1)
c1++;
else
printf("P%d->",i);
if(c1==n)
else
OUTPUT:
753
322
902
222
433
010
200
302
211
002
P1->p3->p4->p2->p0->
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
ff[i]=j;
highest=temp;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
FIRST-FIT:
#include<stdio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme - First Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files :-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp; bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragement");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i];
}
INPUT
Enter the number of blocks: 3 Enter the number of files: 2
Enter the size of the blocks:- Block 1: 5
Block 2: 2
Block 3: 7
Enter the size of the files:- File 1: 1
File 2: 4
OUTPUT
File No File Size Block No Block Size Fragment
1 1 1 5 4
2 4 3 7 3
Write a C program to simulate the BEST FIT contiguous memory
allocation technique
BEST-FIT
#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,lowest=10000;
static int bf[max],ff[max];
clrscr();
printf("\n\tMemory Management Scheme – Best Fit");
printf("\nEnter the number of blocks:"); scanf("%d",&nb);
printf("Enter the number of files:"); scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
printf("Block %d:",i);
scanf("%d",&b[i]);
INPUT
Enter the number of blocks: 3 Enter the number of files: 2
OUTPUT
File No File Size Block No Block Size Fragment
1 1 2 2 1
2 4 1 5 1
{
count[i]=0;
m[i]=-1;
}
printf("\nThe Page Replacement process is -- \n");
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
flag[i]=1;
count[j]=next;
next++;
}
}
if(flag[i]==0)
{
if(i<f)
{ m[i]=rs[i];
count[i]=next;
next++;
}
else
{ min=0;
for(j=1;j<f;j++)
if(count[min] > count[j])
min=j;
m[min]=rs[i];
count[min]=next;
next++;
}
pf++;
}
for(j=0;j<f;j++)
printf("%d\t", m[j]);
if(flag[i]==0)
printf("PF No. -- %d" , pf);
printf("\n");
}
printf("\nThe number of page faults using LRU are %d",pf);
getch();
}
INPUT
Enter the length of reference string -- 20
Enter the reference string -- 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
Enter the number of frames -- 3
OUTPUT
The Page Replacement process is --
7 -1 -1 PF No. – 1
7 0 -1 PF No. – 2
7 0 1 PF No. – 3
2 0 1 PF No. – 4
201
2 0 3 PF No. – 5
203
4 0 3 PF No. – 6
4 0 2 PF No. – 7
4 3 2 PF No. – 8
0 3 2 PF No. – 9
032
032
1 3 2 PF No. – 10
132
1 0 2 PF No. – 11
102
1 0 7 PF No. – 12
107
107
The number of page faults using LRU are 12
0 7 0 -1
1 7 0 1
2 2 0 1
3 2 3 1
0 2 3 0
4 4 3 0
2 4 2 0
3 4 2 3
0 0 2 3
1 0 1 3
2 0 1 2
7 7 1 2
0 7 0 2
1 7 0 1
Page Fault Is 15