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

OS Programs

The document contains code implementations of various scheduling algorithms: FCFS, Round Robin, SJF, preemptive priority, non-preemptive priority, and Banker's algorithm. It provides the code, sample input/output, and descriptions for each algorithm.

Uploaded by

Om Vaghela
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)
63 views

OS Programs

The document contains code implementations of various scheduling algorithms: FCFS, Round Robin, SJF, preemptive priority, non-preemptive priority, and Banker's algorithm. It provides the code, sample input/output, and descriptions for each algorithm.

Uploaded by

Om Vaghela
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/ 22

Name : Om Vaghela

Roll No.: 16BCP060

1. FCFS

Code :

#include<stdio.h>

int main(){

int bt[10]={0},at[10]={0},tat[10]={0},wt[10]={0},ct[10]={0};
int n,sum=0;
float totalTAT=0,totalWT=0;

printf("Enter number of processes : ");


scanf("%d",&n);

printf("Enter arrival time and burst time for each process\n\n");

for(int i=1;i<=n;i++)
{

printf("Arrival time of process[%d] : ",i);


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

printf("Burst time of process[%d] : ",i);


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

printf("\n");
}

for(int j=1;j<=n;j++)
{
sum+=bt[j];
ct[j]+=sum;
}

for(int k=1;k<=n;k++)
{
tat[k]=ct[k]-at[k];
totalTAT+=tat[k];
}
for(int k=1;k<=n;k++)
{
wt[k]=tat[k]-bt[k];
if(wt[k] < 0) wt[k] = 0;
totalWT+=wt[k];
}

printf("Solution: \n\n");
printf("P#\t AT\t BT\t CT\t TAT\t WT\t\n\n");

for(int i=1;i<=n;i++)
{
printf("P%d\t %d\t %d\t %d\t %d\t %d\n",i,at[i],bt[i],ct[i],tat[i],wt[i]);
}

printf("\n\nAverage Turnaround Time = %f\n",totalTAT/n);


printf("Average WT = %f\n\n",totalWT/n);

return 0;
}

Output :

Enter number of processes : 4


Enter arrival time and burst time for each process

Arrival time of process[1] : 0


Burst time of process[1] : 4

Arrival time of process[2] : 1


Burst time of process[2] : 3

Arrival time of process[3] : 2


Burst time of process[3] : 1

Arrival time of process[4] : 3


Burst time of process[4] : 2

Solution:

P# AT BT CT TAT WT

P1 0 4 4 4 0
P2 1 3 7 6 3
P3 2 1 8 6 5
P4 3 2 10 7 5

Average Turnaround Time = 5.750000


Average WT = 3.250000

Process returned 0 (0x0) execution time : 21.735 s

2. Round Robin

Code :

#include<stdio.h>

int main()
{
int i, limit, total = 0, x, counter = 0, time_quantum;
int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];
float average_wait_time, average_turnaround_time;
printf("Enter Total Number of Processes: ");
scanf("%d", &limit);
x = limit;
for(i = 0; i < limit; i++)
{
printf("Enter Details of Process[%d]\n", i + 1);

printf("Arrival Time: ");

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

printf("Burst Time: ");

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

temp[i] = burst_time[i];
}

printf("Enter Time Quantum : ");


scanf("%d", &time_quantum);
printf("P_ID\t\tBT\tTAT\tWT\n");
for(total = 0, i = 0; x != 0;)
{
if(temp[i] <= time_quantum && temp[i] > 0)
{
total = total + temp[i];
temp[i] = 0;
counter = 1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - time_quantum;
total = total + time_quantum;
}
if(temp[i] == 0 && counter == 1)
{
x--;
printf("Process[%d]\t%d\t%d\t%d\n", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - b
rst_time[i]);
wait_time = wait_time + total - arrival_time[i] - burst_time[i];
turnaround_time = turnaround_time + total - arrival_time[i];
counter = 0;
}
if(i == limit - 1)
{
i = 0;
}
else if(arrival_time[i + 1] <= total)
{
i++;
}
else
{
i = 0;
}
}

average_wait_time = wait_time * 1.0 / limit;


average_turnaround_time = turnaround_time * 1.0 / limit;
printf("Average Waiting Time: %f\n", average_wait_time);
printf("Avg Turnaround Time: %f", average_turnaround_time);
return 0;
}

Output :

Enter Total Number of Processes: 4


Enter Details of Process[1]
Arrival Time: 0
Burst Time: 4
Enter Details of Process[2]
Arrival Time: 1
Burst Time: 5
Enter Details of Process[3]
Arrival Time: 2
Burst Time: 2
Enter Details of Process[4]
Arrival Time: 3
Burst Time: 1
Enter Time Quantum : 2
P_ID BT TAT WT
Process[3] 2 4 2
Process[4] 1 4 3
Process[1] 4 9 5
Process[2] 5 11 6
Average Waiting Time: 4.000000
Avg Turnaround Time: 7.000000
Process returned 0 (0x0) execution time : 20.473 s

3. SJF

Code :

#include <iostream>
#include <algorithm>
using namespace std;

int ab;

typedef struct schedule


{
string pro_id;
int at,bt,ct,ta,wt;

}schedule;

bool compare(schedule a,schedule b)


{
return a.at < b.at;

bool compare2(schedule a,schedule b)


{
return a.bt < b.bt && a.at <= ab;

int main()
{
schedule pro[10];

int n,i,j;
cout<<"Enter the number of schedule: ";
cin>>n;
cout<<"Enter the schedule id, arrival time, burst time : "<<endl;

for(i=0;i<n;i++)
{
cin>>pro[i].pro_id;
cin>>pro[i].at;
cin>>pro[i].bt;
}

sort(pro,pro+n,compare);

pro[0].ct=pro[0].bt+pro[0].at;
pro[0].ta=pro[0].ct-pro[0].at;
pro[0].wt=pro[0].ta-pro[0].bt;

for(i=1;i<n;i++)
{
ab=pro[i-1].ct;
sort(pro+i,pro+n,compare2);
if(pro[i-1].ct<pro[i].at)
{
pro[i].ct=pro[i-1].ct+pro[i].bt+(pro[i].at-pro[i-1].ct);
}
else
{

pro[i].ct=pro[i-1].ct+pro[i].bt;

}
pro[i].ta=pro[i].ct-pro[i].at;
pro[i].wt=pro[i].ta-pro[i].bt;
}

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

cout<<pro[i].pro_id<<"\t"<<pro[i].at<<"\t"<<pro[i].bt
<<"\t"<<pro[i].ct<<"\t"<<pro[i].ta<<"\t"<<pro[i].wt;

cout<<endl;
}

return 0;
}
Output :

Enter the number of schedule: 4


Enter the schedule id, arrival time, burst time :
117
225
331
442
1 1 7 8 7 0
3 3 1 9 6 5
4 4 2 11 7 5
2 2 5 16 14 9

Process returned 0 (0x0) execution time : 22.969 s

4. Pre-emptive Priority

Code :

#include<bits/stdc++.h>
using namespace std;

int main(){

int n;
cout<<"Enter number of processes : "; cin>>n;

int p[n],at[n],bt[n],ct[n],tat[n],wt[n],flag[n],rem[n],pr[n];
int i,j;
cout<<"Enter Each Process Details separated by space : \n";
cout<<"<Process id> <Arrival time> <Burst Time:> <Priority (1->highest) > \n";
for(i=0;i<n;i++){
cin>>p[i]>>at[i]>>bt[i]>>pr[i];
flag[i]=0;
rem[i]=bt[i];
}

int c=n,t=0,min,mp;
cout<<"\n\nProcess execution sequence( 1 time pulse at a time )(-1 represent stall pulses ) : \n";
for(t=0;c>0;t++){

min = (int)INFINITY;
mp=-1;
for(i=0;i<n;i++){
if(pr[i]<min && flag[i]==0 && at[i]<=t){
min=pr[i];
mp=i;
}
}

cout<<p[mp]<<"\t";
if(mp!=-1){
rem[mp]--;
if(rem[mp]==0){
ct[mp]=t+1;
tat[mp]=ct[mp]-at[mp];
wt[mp]=tat[mp]-bt[mp];
flag[mp]=1;
c--;
}
}
}

float avgtat=0,avgwt=0;
cout<<"\n\nP.No.\tA.T.\tPriority\tB.T.\tC.T.\tT.A.T.\tW.T.\n";
for(i=0;i<n;i++){
cout<<p[i]<<"\t"<<at[i]<<"\t"<<pr[i]<<"\t\t"<<bt[i]<<"\t"<<ct[i]<<"\t"<<tat[i]<<"\t"<<wt[i]<<"\n";
avgtat+=tat[i];
avgwt+=wt[i];
}

avgtat/=n;
avgwt/=n;
cout<<"\n\nAverage T.A.T. = "<<avgtat;
cout<<"\nAverage W.T. = "<<avgwt<<endl;

return 0;
}

Output :

Enter number of processes : 7


Enter Each Process Details separated by space :
<Process id> <Arrival time> <Burst Time:> <Priority (1->highest) >
1042
2124
3236
4 3 5 10
5418
6 5 4 12
7669
Process execution sequence( 1 time pulse at a time )(-1 represent stall pulses ) :
1 1 1 1 2 2 3 3 3 5 7 7 7 7 7 7 4 4 4 4 4
6 6 6 6

P.No. A.T. Priority B.T. C.T. T.A.T. W.T.


1 0 2 4 4 4 0
2 1 4 2 6 5 3
3 2 6 3 9 7 4
4 3 10 5 21 18 13
5 4 8 1 10 6 5
6 5 12 4 25 20 16
7 6 9 6 16 10 4

Average T.A.T. = 10
Average W.T. = 6.42857

Process returned 0 (0x0) execution time : 43.311 s

5. Non-Preemptive Priority

Code :

#include<bits/stdc++.h>
using namespace std;

int main(){

int n;
cout<<"Enter number of processes : "; cin>>n;

int p[n],at[n],bt[n],ct[n],tat[n],wt[n],flag[n],pr[n];
int i,j;
cout<<"Enter Each Process Details separated by space : \n";
cout<<"<Process id> <Arrival time> <Burst Time> <Priority (1->highest) > \n";
for(i=0;i<n;i++){
cin>>p[i]>>at[i]>>bt[i]>>pr[i];
flag[i]=0;
}

int c=n,t=0,min,mp;
while(c!=0){

min = (int)INFINITY;
mp=-1;
for(i=0;i<n;i++){
if(pr[i]<min && flag[i]==0 && at[i]<=t){
min=pr[i];
mp=i;
}
}
if(mp!=-1){
ct[mp]=t+bt[mp];
t+=bt[mp];
tat[mp]=ct[mp]-at[mp];
wt[mp]=tat[mp]-bt[mp];
flag[mp]=1;
c--;
}
else t++;
}

float avgtat=0,avgwt=0;
cout<<"P.No.\tA.T.\tPriority\tB.T.\tC.T.\tT.A.T.\tW.T.\n";
for(i=0;i<n;i++){
cout<<p[i]<<"\t"<<at[i]<<"\t"<<pr[i]<<"\t\t"<<bt[i]<<"\t"<<ct[i]<<"\t"<<tat[i]<<"\t"<<wt[i]<<"\n";
avgtat+=tat[i];
avgwt+=wt[i];
}

avgtat/=n;
avgwt/=n;
cout<<"\n\nAverage T.A.T. = "<<avgtat;
cout<<"\nAverage W.T. = "<<avgwt<<endl;
return 0;
}

Output :

Enter number of processes : 7


Enter Each Process Details separated by space :
<Process id> <Arrival time> <Burst Time> <Priority (1->highest) >
1042
2124
3236
4 3 5 10
5418
6 5 4 12
7669
P.No. A.T. Priority B.T. C.T. T.A.T. W.T.
1 0 2 4 4 4 0
2 1 4 2 6 5 3
3 2 6 3 9 7 4
4 3 10 5 21 18 13
5 4 8 1 10 6 5
6 5 12 4 25 20 16
7 6 9 6 16 10 4

Average T.A.T. = 10
Average W.T. = 6.42857

Process returned 0 (0x0) execution time : 41.682 s

6. Banker's Algorithm

Code :

#include<stdio.h>

int main()
{
int n = 3;
int m = 3;

// allocation matrix

int alloc[3][3] = { {2,0,1},


{0,1,1},
{1,0,2} };

// max matrix

int max[3][3] = { {3,2,1},


{2,2,2},
{2,1,2} };

// available vector

int avail[3] = {4,3,5};

int f[n], ans[n], ind = 0;


for(int k = 0; k < n; k++) {
f[k] = 0;
}
int need[n][m];
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
int y = 0;
for(int k = 0; k < 5; k++) {
for(int i = 0; i < n; i++) {
if(f[i] == 0) {

int flag = 0;
for(int j = 0; j < m; j++) {
if(need[i][j] > avail[j]){
flag = 1;
break;
}
}

if(flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}

printf("Following is the SAFE Sequence\n");


for(int i = 0; i < n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);

return (0);

Output :

Following is the SAFE Sequence


P0 -> P1 -> P2
Process returned 0 (0x0) execution time : 1.376 s

7. LRU

Code :
#include<stdio.h>

int findLRU(int time[], int n){


int i, minimum = time[0], pos = 0;

for(i = 1; i < n; ++i){


if(time[i] < minimum){
minimum = time[i];
pos = i;
}
}

return pos;
}

int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j, pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);

printf("Enter number of pages: ");


scanf("%d", &no_of_pages);

printf("Enter reference string: ");

for(i = 0; i < no_of_pages; ++i){


scanf("%d", &pages[i]);
}

for(i = 0; i < no_of_frames; ++i){


frames[i] = -1;
}

for(i = 0; i < no_of_pages; ++i){


flag1 = flag2 = 0;

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


if(frames[j] == pages[i]){
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}

if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}

if(flag2 == 0){
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}

printf("\n");

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


printf("%d\t", frames[j]);
}
}

printf("\n\nTotal Page Faults = %d", faults);

return 0;
}

Output :

Enter number of frames: 3


Enter number of pages: 6
Enter reference string: 5 7 5 6 7 3

5 -1 -1
5 7 -1
5 7 -1
5 7 6
5 7 6
3 7 6

Total Page Faults = 4


Process returned 0 (0x0) execution time : 16.006 s

8. SSTF
Code :

#include<bits/stdc++.h>
using namespace std;
int main(){
int i,j,k,n,m,sum=0,x,y,h;
cout<<"Enter the size of disk\n";
cin>>m;
cout<<"Enter number of requests\n";
cin>>n;
cout<<"Enter the requests\n";
vector <int> a(n),b;
map <int,int> mp;
for(i=0;i<n;i++){
cin>>a[i];
mp[a[i]]++;
}
for(i=0;i<n;i++){
if(a[i]>m){
cout<<"Error, Unknown position "<<a[i]<<"\n";
return 0;
}
}
cout<<"Enter the head position\n";
cin>>h;
int temp=h;
int ele;
b.push_back(h);
int count=0;
while(count<n){

int diff=999999;

for(auto q:mp){
if(abs(q.first-temp)<diff){
ele=q.first;
diff=abs(q.first-temp);
}
}
//deleting the element that has the least
//difference from the map
mp[ele]--;
if(mp[ele]==0){
mp.erase(ele);
}
//adding that element to our output array.
b.push_back(ele);
temp=ele;
count++;
}
cout<<b[0];
temp=b[0];
for(i=1;i<b.size();i++){
cout<<" -> "<<b[i];
sum+=abs(b[i]-temp);
temp=b[i];
}
cout<<'\n';
cout<<"Total head movements = "<< sum<<'\n';
cout<<"Average head movement = "<<(float)sum/n<<'\n';
return 0;
}

Output :

Enter the size of disk


200
Enter number of requests
8
Enter the requests
176
79
34
60
92
11
41
114
Enter the head position
50
50 -> 41 -> 34 -> 11 -> 60 -> 79 -> 92 -> 114 -> 176
Total head movements = 204
Average head movement = 25.5

Process returned 0 (0x0) execution time : 27.462 s

9. Threading

Code :

package threading;

class Fib implements Runnable


{
long a,b,c,n;
Fib()
{
a=c=n=0;
b=1;
}
public void run()
{
while(n++<75)
{
System.out.println(n+"th" +" Fib no: = "+a);
c=a+b;
a=b;
b=c;
try
{
if(n==50)
{
System.out.println("Thread fibonacci is put into sleep.");
Thread.sleep(500);
}

}
catch(InterruptedException e)
{
System.out.println("Error : " + e);
}
}
}
}

public class MyFib {

public static void main(String[] args) {


Thread ct=Thread.currentThread();
System.out.println("Main thread name : "+ct.getName());
Fib f=new Fib();
Thread fib=new Thread(f,"fibonacci");
fib.start();
System.out.println("Thread "+ fib.getName() + " started.");
}

10. SRTF

Code :
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

typedef struct proccess


{
int at,bt,ct,ta,wt,btt;
string pro_id;

}Schedule;

bool compare(Schedule a,Schedule b)


{
return a.at<b.at;
}
bool compare2(Schedule a,Schedule b)
{
return a.bt<b.bt;
}

int main()
{
Schedule pro[10];
int n,i,j,pcom;

cout<<"Enter the number of Process: ";


cin>>n;

cout<<"Enter the Process id, arrival time, burst time : ";

for(i=0;i<n;i++)
{
cin>>pro[i].pro_id;
cin>>pro[i].at;
cin>>pro[i].bt;
pro[i].btt=pro[i].bt;
}

sort(pro,pro+n,compare);

i=0;
pcom=0;
while(pcom<n)
{
for(j=0;j<n;j++)
{
if(pro[j].at>i)
break;
}
sort(pro,pro+j,compare2);

if(j>0)
{

for(j=0;j<n;j++)
{
if(pro[j].bt!=0)
break;
}
if(pro[j].at>i)

{
i=pro[j].at;

}
pro[j].ct=i+1;
pro[j].bt--;
}
i++;
pcom=0;
for(j=0;j<n;j++)
{
if(pro[j].bt==0)
pcom++;
}
}

cout<<"P_ID\tAtime\tBtime\tCtime\tTtime\tWtime\n";

for(i=0;i<n;i++)
{
pro[i].ta=pro[i].ct-pro[i].at;
pro[i].wt=pro[i].ta-pro[i].btt;

cout<<pro[i].pro_id<<"\t"<<pro[i].at<<"\t"<<pro[i].btt<<"\t"<<pro[i].ct<<"\t"<<pro[i].ta<<"\t"<<pro[i].wt;
cout<<endl;
}
return 0;
}

Output :

Enter the number of Process: 6


Enter the Process id, arrival time, burst time : 1 0 7
215
323
431
542
651
P_ID Atime Btime Ctime Ttime Wtime
4 3 1 4 1 0
3 2 3 6 4 1
6 5 1 7 2 1
5 4 2 9 5 3
2 1 5 13 12 7
1 0 7 19 19 12

Process returned 0 (0x0) execution time : 21.859 s

11. CSCAN

Code :

#include<bits/stdc++.h>
using namespace std;
int main(){
int i,j,k,n,m,sum=0,x,y,h;
cout<<"Enter the size of disk\n";
cin>>m;
cout<<"Enter number of requests\n";
cin>>n;
cout<<"Enter the requests\n";
vector <int> a(n),b;
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n;i++){
if(a[i]>m){
cout<<"Error, Unknown position "<<a[i]<<"\n";
return 0;
}
}
cout<<"Enter the head position\n";
cin>>h;
int temp=h;
a.push_back(h);
a.push_back(m);
sort(a.begin(),a.end());
for(i=0;i<a.size();i++){
if(h==a[i])
break;
}
k=i;
if(k<n/2){
for(i=k;i<a.size();i++){
b.push_back(a[i]);
}
for(i=k-1;i>=0;i--){
b.push_back(a[i]);
}
}
else{
for(i=k;i>=0;i--){
b.push_back(a[i]);
}
for(i=k+1;i<a.size();i++){
b.push_back(a[i]);
}
}
temp=b[0];
cout<<temp;
for(i=1;i<b.size();i++){
cout<<" -> "<<b[i];
sum+=abs(b[i]-temp);
temp=b[i];
}
cout<<'\n';
cout<<"Total head movements = "<< sum<<'\n';
cout<<"Average head movement = "<<(float)sum/n<<'\n';
return 0;
}

Output :

Enter the size of disk


200
Enter number of requests
8
Enter the requests
176
79
34
60
92
11
41
114
Enter the head position
50
50 -> 60 -> 79 -> 92 -> 114 -> 176 -> 200 -> 41 -> 34 -> 11
Total head movements = 339
Average head movement = 42.375

Process returned 0 (0x0) execution time : 26.285 s

You might also like