0% found this document useful (0 votes)
273 views41 pages

Os Programs C

The document provides details on implementing Shortest Job First (SJF) scheduling in C programming language. It includes: 1) An explanation of the SJF algorithm which prioritizes the process with the shortest burst time. 2) The steps to calculate average waiting time and turnaround time given processes and burst times. 3) An example run of the algorithm showing the Gantt chart and calculations. 4) The source code for a C program that implements SJF scheduling along with sample input/output.
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)
273 views41 pages

Os Programs C

The document provides details on implementing Shortest Job First (SJF) scheduling in C programming language. It includes: 1) An explanation of the SJF algorithm which prioritizes the process with the shortest burst time. 2) The steps to calculate average waiting time and turnaround time given processes and burst times. 3) An example run of the algorithm showing the Gantt chart and calculations. 4) The source code for a C program that implements SJF scheduling along with sample input/output.
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/ 41

1 d)Exec system call

//EXEC.c

#include<stdio.h>
#include<unistd.h>

int main()
{
int i;

printf("I am EXEC.c called by execvp() ");


printf("\n");

return 0;
}

 Now,create an executable file of EXEC.c using command


gcc EXEC.c -o EXEC

FCFS Scheduling Program in C


Problem Description:
Write an FCFS Scheduling Program in C to determine the average waiting time and
average turnaround time has given n processes and their burst times.

FCFS Scheduling Algorithm:


The CPU scheduling algorithm First Come, First Served (FCFS), also known as First In,
First Out (FIFO), allocates the CPU to the processes in the order they are queued in the
ready queue.
FCFS uses non-preemptive scheduling, which means that once a CPU has been assigned
to a process, it stays assigned to that process until it is either not terminated or may be
interrupted by an I/O interrupt.
Problem Solution
1. Enter all the processes and their burst time.
2. Find waiting time, WT of all the processes.
3. For the 1st process, WT = 0.
4. For all the next processes i, WT[i] = BT[i-1] + WT[i-1].
5. Calculate Turnaround time = WT + BT for all the processes.
6. Calculate average waiting time = total waiting time/no. of processes.
7. Calculate average turnaround time = total turnaround time/no. of processes.
Example:
Process Arrival Time Burst Time
P1 0 5

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

Average Waiting Time = (0 + 5 + 16)/3 = 21/3 = 7


Turnaround Time: Difference between completion time and arrival time.
Turnaround Time = Completion Time – Arrival Time
P1 turnaround time: 5-0 = 5
P2 turnaround time: 16-0 = 16
P3 turnaround time: 27-0 = 27

Average Turnaround Time = (5+16+27)/3 = 16ms


Program/Source Code
Here is the source code of the C program for the FCFS Scheduling. The C program is
successfully compiled and run on a Linux system. The program output is also shown
below.

1. /*
2. * FCFS Scheduling Program in C
3. */
4.
First Come First Serve with Arrival time in C programming language code

//FCFS Sheduling Algorithm with GANTT Chart

#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

Average TAT is 4.500000


*/

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.

SJF Scheduling Program in C


Problem Description:
Write an SJF scheduling program in C to determine the average waiting time and
average turnaround time given n processes and their burst times.

SJF Scheduling Algorithm in C:


The CPU scheduling algorithm Shortest Job First (SJF), allocates the CPU to the
processes according to the process with smallest execution time.
SJF uses both preemptive and non-preemptive scheduling. The preemptive version
of SJF is called SRTF (Shortest Remaining Time First). Here we will discuss about SJF
i.e., the non-preemptive scheduling.
Advantages of SJF:
 It has the minimum waiting time among all the scheduling algorithms.
 A process having larger burst time may get into starvation but the
problem can be solved using concept of Ageing.
 It is a greedy algorithm and provides optimal scheduling.
Problem Solution
1. Enter number of processes.
2. Enter the burst time of all the processes.
3. Sort all the processes according to their burst time.
4. Find waiting time, WT of all the processes.
5. For the smallest process, WT = 0.
6. For all the next processes i, find waiting time by adding burst time of all the
previously completed process.
7. Calculate Turnaround time = WT + BT for all the processes.
8. Calculate average waiting time = total waiting time / no. of processes.
9. Calculate average turnaround time= total turnaround time / no. of
processes.
SJF Example:
Process Arrival Time Burst Time

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

Average Waiting Time = (4 + 0 + 16 + 9)/4 = 29/4 = 7.25


Turnaround Time: Difference between completion time and arrival time.
Turnaround Time = Completion Time – Arrival Time
P1 turnaround time: 9-0 = 9
P2 turnaround time: 4-0 = 4
P3 turnaround time: 28-0 = 28
P4 turnaround time: 16-0 = 16

Average Turnaround Time = (9 + 4 + 28 + 16)/4 = 14.25


Program/Source Code
Here is the source code of the C Program to Implement SJF Scheduling. The C
program is successfully compiled and run on a Linux system.

/*C program for non-preemptive SJF scheduling :*/

#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 :

Code for Pre-emptive SJF Scheduling

Let's see an implementation of Pre-emptive SJF Scheduling in C language:


#include<stdio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];

printf("Enter the number of process:");


scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter process name, arrival time& Burst-time:");
//flushall();
scanf("%s%d%d",pn[i],&at[i],&et[i]);
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(et[i]<et[j])
{
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);
}
}
for(i=0; i<n; i++)
{
if(i==0)
st[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\tBurst-time\twaitingtime\ttatime");
for(i=0; i<n; i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);

}
/* Sample-output

Enter the number of process:3


Enter process name, arrival time& Burst-time:0 1 2
Enter process name, arrival time& Burst-time:1 2 3
Enter process name, arrival time& Burst-time:2 3 4

Pname arrivaltime Burst-time waitingtime tatime


0 1 2 0 2
1 2 3 1 4
2 3 4 3 7
Average waiting time is:1.333333milliseconds
Average turnaroundtime is:4.333333milliseconds
*/

Advantages of SJF

The following are the advantages of SJF scheduling:

 This algorithm comes into use when there is long-term scheduling.


 This algorithm reduces the average waiting time.
 This algorithm is useful if there is a batch-type processor.
 This algorithm can be considered an optimal algorithm because, in this
algorithm, the average waiting time is minimum.

Disadvantages of SJF

The following are the disadvantages of the SJF algorithm:

 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.

Priority Scheduling Program in C


Priority Scheduling is a CPU scheduling algorithm in which the CPU performs the
task having higher priority at first. If two processes have the same priority then
scheduling is done on FCFS basis (first come first serve). Priority Scheduling is of two
types : Preemptive and Non-Preemptive.
Preemptive: In this case, resources can be voluntarily snatched.
Non-Preemptive: In this type, if a process is once started, it will execute completely
i.e resources cannot be snatched.
Following are the basic terminologies:
Waiting Time: Time for which the process has to wait in the ready queue.
Turn Around Time: Total time taken by the process for execution (waiting time +
burst time).
Problem Description:
Write a C Program to implement priority scheduling.

Example:
Following is the example of non preemptive scheduling with arrival time zero.

Process Burst Time Priority

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.

Order: P2, P4, P3, P1


P2 will be executed from 0 to 7.
P4 will be executed from 7 to 10.
P3 will be executed from 10 to 12.
P1 will be executed from 12 to 17.

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

printf("Enter the number of process:");

scanf("%d",&n);

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

printf("Enter process name,arrivaltime,execution time & priority:");

scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);

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

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

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

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

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

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

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

printf("\nAverage waiting time is:%fms",awt);

printf("\nAverage turnaroundtime is:%fms",ata);

return 0;

OUTPUT:

Enter the number of process:3


Enter process name,arrivaltime,execution time & priority:1 2 1 3
Enter process name,arrivaltime,execution time & priority:3 5 4 1
Enter process name,arrivaltime,execution time & priority:3 4 6 2

Pname arrivaltime executiontime priority waitingtime tatime


3 5 4 1 0 4
3 4 6 2 5 11
1 2 1 3 13 14
Average waiting time is:6.000000ms
Average turnaroundtime is:9.666667ms
Program Explanation
1. First, enter the total number of processes and store it in variable n.
2. After that, provide the burst time and priority and store it in variable b and p.
3. Finding out highest priority element and placing it at its desired position.
4. Sort the processes on the basis of the priority.
5. After that print the processed with their time stamp (starting time and ending
time). Variable T stores the starting time of process.
6. In the end, print the waiting time and turnaround time for each process.
Waiting time is the time spent in the ready queue, while turnaround time is the total
time taken by process (burst time + waiting time).
Time Complexity: O(n*n)
Sorting takes time of the order of O(n*n), So time complexity is of the order of
O(n*n).
Space Complexity: O(n)
Space is required to store burst time, arrival time and index, So space complexity is
O(n).
Run Time Testcases
In this case, we enter “3” as the number of processes, and the burst time and
priority value are “p1: 10 2”, “p2: 5 0”, and “p3: 8 1”.

Round Robin Scheduling Program in C


Problem Description:
Write a C Program that implements the Round Robin Scheduling algorithm and
determines the average waiting time and turnaround time.

What is Round Robin Scheduling in C?


Round Robin Scheduling is a CPU scheduling algorithm in which each process is
executed for a fixed time slot. Since the resources are snatched after the time slot,
round robin is preemptive.
Preemptive: In this type, resources can be voluntarily snatched.
Non-Preemptive: In this type, if a process is once started, it will execute completely
i.e resources cannot be snatched.
Following are the basic terminologies:

Turnaround Time: Difference between completion time and arrival time.


Turnaround Time = Completion Time – Arrival Time
Waiting Time: Time Difference between turnaround time and burst time.
Waiting Time = Turnaround Time – Burst Time
advertisement
Problem Solution
Round Robin Scheduling Algorithm:
Step 1: Start the Program.
Step 2: Input the number of processes.
Step 3: Input the burst time and arrival time of each process and the limit of the
time slot.
Step 4: Push all processes into the ready queue according to their arrival time. Then
execute each process upto time slot and push left over process in queue again for
execution.
Step 5: After a process is completely executed, print its turn around time and
waiting time.
Example:
Following is the example of round robin scheduling.
Process Id Arrival Time Burst Time

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

Process Id Burst Time Wait Time Turn Around Time

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;

//Input details of processes


for(int i = 0; i < n; i++)
{
printf("Enter Details of Process %d \n", i + 1);
printf("Arrival Time: ");
scanf("%d", &arr_time[i]);
printf("Burst Time: ");
scanf("%d", &burst_time[i]);
temp_burst_time[i] = burst_time[i];
}

//Input time slot


int time_slot;
printf("Enter Time Slot:");
scanf("%d", &time_slot);

//Total indicates total time


//counter indicates which process is executed
int total = 0, counter = 0,i;
printf("Process ID Burst Time Turnaround Time Waiting Time\n");
for(total=0, i = 0; x!=0; )
{
// define the conditions
if(temp_burst_time[i] <= time_slot && temp_burst_time[i] > 0)
{
total = total + temp_burst_time[i];
temp_burst_time[i] = 0;
counter=1;
}
else if(temp_burst_time[i] > 0)
{
temp_burst_time[i] = temp_burst_time[i] - time_slot;
total += time_slot;
}
if(temp_burst_time[i]==0 && counter==1)
{
x--; //decrement the process no.
printf("\nProcess No %d \t\t %d\t\t\t\t %d\t\t\t %d", i+1,burst_time[i],
total-arr_time[i], total-arr_time[i]-burst_time[i]);
wait_time = wait_time+total-arr_time[i]-burst_time[i];
ta_time += total -arr_time[i];
counter =0;
}
if(i==n-1)
{
i=0;
}
else if(arr_time[i+1]<=total)
{
i++;
}
else
{
i=0;
}
}
float average_wait_time = wait_time * 1.0 / n;
float average_turnaround_time = ta_time * 1.0 / n;
printf("\nAverage Waiting Time:%fms", average_wait_time);
printf("\nAvg Turnaround Time:%fms", average_turnaround_time);
return 0;
}
/*Sample Output
Enter Total Number of Processes:3
Enter Details of Process 1
Arrival Time: 0
Burst Time: 10
Enter Details of Process 2
Arrival Time: 1
Burst Time: 8
Enter Details of Process 3
Arrival Time: 2
Burst Time: 7
Enter Time Slot:5

Process ID Burst Time Turnaround Time Waiting Time

Process No 1 10 20 10
Process No 2 8 22 14
Process No 3 7 23 16

Average Waiting Time: 13.333333ms


Avg Turnaround Time: 21.666666ms*/

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;

// Number of full slots as 0


int full = 0;

// Number of empty slots as size


// of buffer
int empty = 10, x = 0;

// Function to produce an item and


// add it to the buffer
void producer()
{
// Decrease mutex value by 1
--mutex;

// Increase the number of full


// slots by 1
++full;

// Decrease the number of empty


// slots by 1
--empty;

// Item produced
x++;
printf("\nProducer produces"
"item %d",
x);

// Increase mutex value by 1


++mutex;
}

// Function to consume an item and


// remove it from buffer
void consumer()
{
// Decrease mutex value by 1
--mutex;

// Decrease the number of full


// slots by 1
--full;
// Increase the number of empty
// slots by 1
++empty;
printf("\nConsumer consumes "
"item %d",
x);
x--;

// Increase mutex value by 1


++mutex;
}

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:

// If mutex is 1 and empty is non-zero, then it is possible to produce


if ((mutex == 1)
&& (empty != 0)) {
producer();
}

// Otherwise, print buffer


// is full
else {
printf("Buffer is full!");
}
break;

case 2:

// If mutex is 1 and full is non-zero, then it is possible to consume


if ((mutex == 1&& (full != 0))
{
consumer();
}

// Otherwise, print Buffer is empty


else {
printf("Buffer is empty!");
}
break;

// Exit Condition
case 3:
exit(0);
break;
}
}
}

5.C program for bankers algorithm for deadlock avoidance.

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

printf("********** Banker's Algo ************\n");

input();

show();

cal();

return 0;

void input()

{
int i,j;

printf("Enter the no of Processes\t");

scanf("%d",&n);

printf("Enter the no of resources instances\t");

scanf("%d",&r);

printf("Enter the Max Matrix\n");

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

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

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

printf("Enter the Allocation Matrix\n");

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

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

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

printf("Enter the available Resources\n");

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

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

void show()

int i,j;

printf("Process\t Allocation\t Max\t Available\t");

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;

//find need matrix

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)

printf("\n The system is in safe state");

else

printf("\n Process are in dead lock");

printf("\n System is in unsafe state");


}

OUTPUT:

Enter the no of processes 5

Enter the no of resources instances 3

Enter the max matrix

753

322

902

222

433

Enter the allocation matrix

010

200

302

211

002

Enter available resources 3 2 2

P1->p3->p4->p2->p0->

The system is in safe state.

6./*Write a C program to simulate the WORST-FIT contiguous memory allocation


technique
WORST-FIT*/
#include<stdio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
printf("\n\tMemory Management Scheme - Worst 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) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;

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

Write a C program to simulate the FIRST-FIT contiguous memory


allocation technique

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

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)
if(lowest>temp)
{
ff[i]=j;
lowest=temp;
}
}
}
frag[i]=lowest;
bf[ff[i]]=1;
lowest=10000;
}
printf("\nFile No\tFile Size \tBlock No\tBlock Size\tFragment");
for(i=1;i<=nf && ff[i]!=0;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]);
getch();
}

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 2 2 1
2 4 1 5 1

7. Write a C program to simulate a)FIFO b)LRU page replacement


algorithm

LRU PAGE REPLACEMENT ALGORITHM


#include<stdio.h>
void main()
{
int i, j , k, min, rs[25], m[10], count[10], flag[25], n, f, pf=0, next=1;
printf("Enter the length of reference string -- ");
scanf("%d",&n);
printf("Enter the reference string -- ");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]);
flag[i]=0;
}
printf("Enter the number of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)

{
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

FIFO Page Replacement algorithm in c


code in C:
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\nenter the length of the Reference string:\n");
scanf("%d",&n);
printf("\n enter the reference string:\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n enter the number of Frames:");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n\n");
}
printf("Page Fault Is %d",count);
return 0;
}
output:-
enter the length of the 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


ref string page frames
7 7 -1 -1

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

You might also like