0% found this document useful (0 votes)
30 views23 pages

Os 2nd PGM (FCFS, SJF, RR, Priority)

Uploaded by

Vijaya Lakshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views23 pages

Os 2nd PGM (FCFS, SJF, RR, Priority)

Uploaded by

Vijaya Lakshmi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

CS303-Operating Systems Lab-syllabus

1. Develop a c program to implement the Process system calls

(fork (), exec(), wait(), create process, terminate

process)

2. Simulate the following CPU scheduling algorithms to find turnaround

time and waiting time .

a) FCFS b) SJF c) Round Robin d) Priority.

3. Develop a C program to simulate producer-consumer problem using

semaphores.

4. Develop a C program which demonstrates interprocess

communication between a reader process and a writer process.

Use mkfifo, open, read, write and close APIs in your program.

5. Develop a C program to simulate Bankers Algorithm for

DeadLock Avoidance.

6. Develop a C program to simulate the following contiguous

memory allocation Techniques:

a) Worst fit b) Best fit c) First fit.

7. Develop a C program to simulate page replacement algorithms:

a) FIFO b) LRU

8. Simulate following File Organization Techniques

a) Single level directory b) Two level directory

9. Develop a C program to simulate the Linked file allocation strategies.

10 Develop a C program to simulate SCAN disk scheduling algorithm.


2. Simulate the following CPU scheduling algorithms to find turnaround

time and waiting time .

a) FCFS b) SJF c) Round Robin d) Priority.


FCFS
First in, first out (FIFO), also known as first come, first served (FCFS), is the simplest
scheduling algorithm.
FIFO simply queues processes in the order that they arrive in the ready queue.
In this, the process that comes first will be executed first and next process starts only
after the previous gets fully executed.

Here we are considering that arrival time for all processes is 0.


1. Completion Time: Time at which process completes its execution.

2. Turn Around Time: Time Difference between completion time and arrival time. Turn
Around Time = Completion Time – Arrival Time

3. Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time

EXAMPLE

Given n processes with their burst times, the task is to find average waiting time and
average turnaround time using FCFS scheduling algorithm.

Arrival Burst
Process
Time Time

P1 0 5

P2 0 11

P3 0 11
GANTT CHART

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 = 16

PROGRAM
FCFS.C
// C program for implementation of FCFS // scheduling
#include<stdio.h>
// Function to find the waiting time for all // processes
void findWaitingTime(int processes[], int n, int bt[], int wt[])
{
// waiting time for first process is 0
wt[0] = 0;
// calculating waiting time
for (int i = 1; i < n ; i++ )
wt[i] = bt[i-1] + wt[i-1] ;
}
// Function to calculate turn around time
void findTurnAroundTime( int processes[], int n, int bt[], int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
//Function to calculate average time
void findavgTime( int processes[], int n, int bt[])
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
//Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt);
//Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
//Display processes along with all details
printf("Processes Burst time Waiting time Turn around time\n");
// Calculate total waiting time and total turn // around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d ",(i+1));
printf(" %d ", bt[i] );
printf(" %d",wt[i] );
printf(" %d\n",tat[i] );
}
float s=(float)total_wt / (float)n;
float t=(float)total_tat / (float)n;
printf("Average waiting time = %f",s);
printf("\n");
printf("Average turn around time = %f ",t);
}
// Driver code
int main()
{
//process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
//Burst time of all processes
int burst_time[] = {10, 5, 8};
findavgTime(processes, n, burst_time);
return 0;
}

OUTPUT
2.2 SJF(SHORTEST JOB FIRST SCHEDULING ALGORITHM)

The operating system is responsible for scheduling a job for the processor. There
are various types of algorithms used for scheduling a job to the CPU for executing these
processes. Shortest Job First (SJF) is a type of disk scheduling algorithm in the operating
system in which the processor executes the job first that has the smallest execution time. In
the shortest Job First algorithm, the processes are scheduled according to the burst time of
these processes.
There are two types of SJF

Pre-emptive SJF

Pre-emptive SJF is a type of scheduling algorithm in which jobs are inserted into the ready
queue as soon as they arrive at the disk. The process having the shortest burst time starts to
get executed first, even if the shortest burst time arrives the current burst is removed from
the execution process and the job having the shortest burst time gets allocated in the CPU
cycle.

Non-Preemptive SJF

These algorithms schedule processes in the order in which the shortest job is done
first. It has a minimum average waiting time.

There are 3 factors to consider while solving SJF, they are

1. BURST Time
2. Average waiting time
3. Average turnaround time

Important Steps

 First of all, all the processes are sorted according to the arrival time.
 After that, the process having minimum arrival and burst time is selected.
 Then that process gets executed and all the process that arrives waits in a queue
while the first process is in the execution state, and after completion again the
process with minimum arrival and burst time is selected for the next execution.
 Completion Time: Time at which process completes its execution.

 Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time

 Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time

EXAMPLE

Arrival Burst
Process
Time Time

P1 0 5

P2 0 4

P3 0 12

P4 0 7

GANTT CHART

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
Sjf.c
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);

printf("nEnter Burst Time:n");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}

//sorting of burst times


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=(float)total/n;
total=0;

printf("nProcesst Burst Time tWaiting TimetTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("np%dtt %dtt %dttt%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=(float)total/n;
printf("nnAverage Waiting Time=%f",avg_wt);
printf("nAverage Turnaround Time=%fn",avg_tat);
}

OUTPUT
In the above program, we calculate the average waiting and average turn around
times of the jobs. We first ask the user to enter the number of processes and store it in n.
We then accept the burst times from the user. It is stored in the bt array.

After this, the burst times are sorted in the next section so the shortest one can be executed
first. Here selection sort is used to sort the array of burst time bt.

Waiting time of the first element is zero, the remaining waiting time is calculated by
using two for loop that runs from 1 to in that controls the outer loop and the inner loop is
controlled by another for loop that runs from j=0 to j<i. Inside the loop, the waiting time is
calculated by adding the burst time to the waiting time.

1 for(i=1;i<n;i++)
2 {
3 wt[i]=0;
4 for(j=0;j<i;j++)
5 wt[i]+=bt[j];
6 total+=wt[i];
7 }
Total is the addition of all the waiting time together. The average waiting time is
calculated:

avg_wt=(float)total/n;

and it is printed.

Next, the turnaround time is calculated by adding the burst time and the waiting time

1 for(i=0;i<n;i++)
2 {
3 tat[i]=bt[i]+wt[i];
4 total+=tat[i];
5 printf("np%dtt %dtt %dttt%d",p[i],bt[i],wt[i],tat[i]);
6 }
Again, here the for loop is used. And the total variable here holds the total
turnaround time. After this the average turnaround time is calculated. This is how Non-
Preemptive scheduling takes place
2.3 ROUND ROBIN SCHEDULING ALGORITTHM

Round Robin is a CPU scheduling algorithm where each process is assigned a fixed time
slot in a cyclic way. It is basically the preemptive version of First come First Serve CPU
Scheduling algorithm.
 Round Robin CPU Algorithm generally focuses on Time Sharing technique.
 The period of time for which a process or job is allowed to run in a pre-emptive
method is called time quantum.
 Each process or job present in the ready queue is assigned the CPU for that time
quantum, if the execution of the process is completed during that time then the process
will end else the process will go back to the waiting table and wait for its next turn to
complete the execution.

EXAMPLE

In the following example, there are six processes named as P1, P2, P3, P4, P5 and P6. Their
arrival time and burst time are given below in the table. The time quantum of the system is
4 units.
Process ID Arrival Time Burst Time

1 0 5

2 1 6

3 2 3

4 3 1

5 4 5

6 6 4

According to the algorithm, we have to maintain the ready queue and the Gantt chart.
The structure of both the data structures will be changed after every scheduling.
Ready Queue:

Initially, at time 0,process P1 arrives which will be scheduled for the time slice 4 units.
Hence in the ready queue, there will be only one process P1 at starting with CPU burst time
5 units.

P1

GANTT chart

The P1 will be executed for 4 units first.

Ready Queue

Meanwhile the execution of P1, four more processes P2, P3, P4 and P5 arrives in the
ready queue. P1 has not completed yet, it needs another 1 unit of time hence it will also be
added back to the ready queue.

P2 P3 P4 P5 P1

6 3 1 5 1

GANTT chart

After P1, P2 will be executed for 4 units of time which is shown in the Gantt chart.

Ready Queue

During the execution of P2, one more process P6 is arrived in the ready queue. Since
P2 has not completed yet hence, P2 will also be added back to the ready queue with the
remaining burst time 2 units.
P3 P4 P5 P1 P6 P2

3 1 5 1 4 2

GANTT chart

After P1 and P2, P3 will get executed for 3 units of time since its CPU burst time is only 3
seconds.

Ready Queue

Since P3 has been completed, hence it will be terminated and not be added to the ready
queue. The next process will be executed is P4.

P4 P5 P1 P6 P2

1 5 1 4 2

GANTT chart

After, P1, P2 and P3, P4 will get executed. Its burst time is only 1 unit which is lesser then

the time quantum hence it will be completed.


Ready Queue

The next process in the ready queue is P5 with 5 units of burst time. Since P4 is completed
hence it will not be added back to the queue.

P5 P1 P6 P2

5 1 4 2

GANTT chart

P5 will be executed for the whole time slice because it requires 5 units of burst time which
is higher than the time slice.

Ready Queue

P5 has not been completed yet; it will be added back to the queue with the remaining burst
time of 1 unit.

P1 P6 P2 P5

1 4 2 1

GANTT Chart

The process P1 will be given the next turn to complete its execution. Since it only requires
1 unit of burst time hence it will be completed.
Ready Queue

P1 is completed and will not be added back to the ready queue. The next process P6
requires only 4 units of burst time and it will be executed next.

P6 P2 P5

4 2 1

GANTT chart

P6 will be executed for 4 units of time till completion.

Ready Queue

Since P6 is completed, hence it will not be added again to the queue. There are only two
processes present in the ready queue. The Next process P2 requires only 2 units of time.

P2 P5

2 1

GANTT Chart

P2 will get executed again, since it only requires only 2 units of time hence this will be
completed.
Ready Queue

Now, the only available process in the queue is P5 which requires 1 unit of burst time.
Since the time slice is of 4 units hence it will be completed in the next burst.

P5

GANTT chart

P5 will get executed till completion.

The completion time, Turnaround time and waiting time will be calculated as shown in the
table below.

As, we know,

1. Turn Around Time = Completion Time - Arrival Time


2. Waiting Time = Turn Around Time - Burst Time
Process Arrival Burst Completion Turn Waiting
ID Time Time Time Around Time
Time

1 0 5 17 17 12

2 1 6 23 22 16

3 2 3 11 9 6

4 3 1 12 9 8

5 4 5 24 20 15

6 6 4 21 15 11

Avg Waiting Time = (12+16+6+8+15+11)/6 = 76/6 units

Program
rr.c

#include<stdio.h>
#include<conio.h>
void main()
{
// initlialize the variable name
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];
float avg_wt, avg_tat;
printf(" Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP; // Assign the number of process to variable y

// Use for loop to enter the details of the process like Arrival time and the Burst Time
for(i=0; i<NOP; i++)
{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t"); // Accept arrival time
scanf("%d", &at[i]);
printf(" \nBurst time is: \t"); // Accept the Burst time
scanf("%d", &bt[i]);
temp[i] = bt[i]; // store the burst time in temp array
}
// Accept the Time qunat
printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);
// Display the process No, burst time, Turn Around Time and the waiting time
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0) // define the conditions
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--; //decrement the process no.
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-
at[i]bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
// represents the average waiting time and Turn Around time
avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat);
getch();
}

OUTPUT
2.4 PRIORITY SCHEDULING ALGORITHM

In Priority Scheduling,
Out of all the available processes, CPU is assigned to the process having the highest

priority.
 In case of a tie, it is broken by FCFS Scheduling.
Priority Scheduling can be used in both preemptive and non-preemptive mode.
Note-01:
 The waiting time for the process having the highest priority will always be zero in
preemptive mode.
 The waiting time for the process having the highest priority may not be zero in non-
preemptive mode.

Note-02:
Priority scheduling in preemptive and non-preemptive mode behaves exactly same under
following conditions-
 The arrival time of all the processes is same
 All the processes become available

EXAMPLE:
Consider the set of 5 processes whose arrival time and burst time are given below

Process Id Arrival time Burst time Priority

P1 0 4 2

P2 1 3 3

P3 2 1 4

P4 3 5 5

P5 4 2 5
If the CPU scheduling policy is priority non-preemptive, calculate the average waiting time
and average turn around time. (Higher number represents higher priority)

Solution-

Gantt Chart-

Now, we know-
 Turn Around time = Exit time – Arrival time
 Waiting time = Turn Around time – Burst time

Also read- Various Times of Process

Process Turn Around


Exit time Waiting time
Id time

P1 4 4–0=4 4–4=0

P2 15 15 – 1 = 14 14 – 3 = 11

P3 12 12 – 2 = 10 10 – 1 = 9

P4 9 9–3=6 6–5=1

P5 11 11 – 4 = 7 7–2=5
Now,
 Average Turn Around time = (4 + 14 + 10 + 6 + 7) / 5 = 41 / 5 = 8.2 unit
 Average waiting time = (0 + 11 + 9 + 1 + 5) / 5 = 26 / 5 = 5.2 unit

PROGRAM

Priority.c

#include<stdio.h>
main()
{
int p[20],bt[20],pri[20], wt[20],tat[20],i, k, n, temp;
float wtavg, tatavg;
clrscr();
printf("Enter the number of processes --- ");
scanf("%d",&n);

for(i=0;i<n;i++)
{
p[i] = i;
printf("Enter the Burst Time & Priority of Process %d --- ",i);
scanf("%d %d",&bt[i], &pri[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(pri[i] > pri[k])
{
temp=p[i]; p[i]=p[k]; p[k]=temp;

temp=bt[i]; bt[i]=bt[k]; bt[k]=temp;


temp=pri[i]; pri[i]=pri[k]; pri[k]=temp;

}
wtavg = wt[0] = 0;
tatavg = tat[0] = bt[0];

for(i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = tat[i-1] + bt[i];
wtavg = wtavg + wt[i]; tatavg =
tatavg + tat[i];
}
printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\tTURNAROUND
TIME");
for(i=0;i<n;i++)
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d ",p[i],pri[i],bt[i],wt[i],tat[i]);
printf("\nAverage Waiting Time is --- %f",wtavg/n);
printf("\nAverage Turnaround Time is --%f",tatavg/n);
getch();
}

OUTPUT

You might also like