0% found this document useful (0 votes)
57 views15 pages

OS PRactical Prep

The document contains code for implementing different scheduling algorithms in C including FCFS, SJF (preemptive and non-preemptive), and Priority scheduling (preemptive and non-preemptive). It includes functions to calculate waiting times, turnaround times, and averages for each algorithm. The code takes process details like arrival time and burst time as input and outputs waiting times and turnaround times for each process.
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)
57 views15 pages

OS PRactical Prep

The document contains code for implementing different scheduling algorithms in C including FCFS, SJF (preemptive and non-preemptive), and Priority scheduling (preemptive and non-preemptive). It includes functions to calculate waiting times, turnaround times, and averages for each algorithm. The code takes process details like arrival time and burst time as input and outputs waiting times and turnaround times for each process.
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/ 15

FCFS Non Preemptive code:-

#include<stdio.h>
int main()
{
int AT[10],BT[10],WT[10],TT[10],n;
int burst=0,cmpl_T;
float Avg_WT,Avg_TT,Total=0;
printf("Enter number of the process\n");
scanf("%d",&n);
printf("Enter Arrival time and Burst time of the process\n");
printf("AT\tBT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d",&AT[i],&BT[i]);
}

// Logic for calculating Waiting time


for(int i=0;i<n;i++)
{
if(i==0)
WT[i]=AT[i];
else
WT[i]=burst-AT[i];
burst+=BT[i];
Total+=WT[i];
}
Avg_WT=Total/n;

// Logic for calculating Turn around time


cmpl_T=0;
Total=0;
for(int i=0;i<n;i++)
{
cmpl_T+=BT[i];
TT[i]=cmpl_T-AT[i];
Total+=TT[i];
}
Avg_TT=Total/n;

// printing of outputs

printf("Process ,Waiting_time ,TurnA_time\n");


for(int i=0;i<n;i++)
{
printf("%d\t\t%d\t\t%d\n",i+1,WT[i],TT[i]);
}
printf("Average waiting time is : %f\n",Avg_WT);
printf("Average turn around time is : %f\n",Avg_TT);
return 0;
}

SJF Non Preemptive code:-

#include<stdio.h>
struct process
{
int id,WT,AT,BT,TAT;
};
struct process a[10];

// function for swapping


void swap(int *b,int *c)
{
int tem;
tem=*c;
*c=*b;
*b=tem;
}

//Driver function
int main()
{
int n,check_ar=0;
int Cmp_time=0;
float Total_WT=0,Total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of process \n");
scanf("%d",&n);
printf("Enter the Arrival time and Burst time of the process\n");
printf("AT BT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].AT,&a[i].BT);
a[i].id=i+1;
// here we are checking that arrival time
// of the process are same or different
if(i==0)
check_ar=a[i].AT;
if(check_ar!=a[i].AT )
check_ar=1;

}
// if process are arrived at the different time
// then sort the process on the basis of AT
if(check_ar!=0)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j].AT>a[j+1].AT)
{
swap(&a[j].id,&a[j+1].id);
swap(&a[j].AT,&a[j+1].AT);
swap(&a[j].BT,&a[j+1].BT);
}
}
}
}

// logic of SJF non preemptive algo


// if all the process are arrived at different time
if(check_ar!=0)
{
a[0].WT=a[0].AT;
a[0].TAT=a[0].BT-a[0].AT;
Cmp_time=a[0].TAT;
Total_WT=Total_WT+a[0].WT;
Total_TAT=Total_TAT+a[0].TAT;
for(int i=1;i<n;i++)
{
int min=a[i].BT;
for(int j=i+1;j<n;j++)
{
if(min>a[j].BT && a[j].AT<=Cmp_time)
{
min=a[j].BT;
swap(&a[i].id,&a[j].id);
swap(&a[i].AT,&a[j].AT);
swap(&a[i].BT,&a[j].BT);
}
}
a[i].WT=Cmp_time-a[i].AT;
Total_WT=Total_WT+a[i].WT;
// completion time of the process
Cmp_time=Cmp_time+a[i].BT;

// Turn Around Time of the process


// compl-Arival
a[i].TAT=Cmp_time-a[i].AT;
Total_TAT=Total_TAT+a[i].TAT;

}
}

// if all the process are arrived at same time


else
{
for(int i=0;i<n;i++)
{
int min=a[i].BT;
for(int j=i+1;j<n;j++)
{
if(min>a[j].BT && a[j].AT<=Cmp_time)
{
min=a[j].BT;
swap(&a[i].id,&a[j].id);
swap(&a[i].AT,&a[j].AT);
swap(&a[i].BT,&a[j].BT);
}

}
a[i].WT=Cmp_time-a[i].AT;

// completion time of the process


Cmp_time=Cmp_time+a[i].BT;

// Turn Around Time of the process


// compl-Arrival
a[i].TAT=Cmp_time-a[i].AT;
Total_WT=Total_WT+a[i].WT;
Total_TAT=Total_TAT+a[i].TAT;

}
}

Avg_WT=Total_WT/n;
Avg_TAT=Total_TAT/n;

// Printing of the results


printf("The process are\n");
printf("ID WT TAT\n");
for(int i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",a[i].id,a[i].WT,a[i].TAT);
}

printf("Avg waiting time is:- %f\n",Avg_WT);


printf("Avg turn around time is:- %f",Avg_TAT);
return 0;
}

SJF Preemptive code:-

#include<stdio.h>
struct process
{
int WT,AT,BT,TAT;
};

struct process a[10];

int main()
{
int n,temp[10];
int count=0,t=0,short_P;
float total_WT=0, total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of the process\n");
scanf("%d",&n);
printf("Enter the arrival time and burst time of the process\n");
printf("AT WT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].AT,&a[i].BT);

// copying the burst time in


// a temp array for the further use
// in calculation of WT
temp[i]=a[i].BT;
}

// we initialize the burst time


// of a process with the maximum
a[9].BT=10000;
// loop will be execute until all the process
// complete so we use count!= number of
// the process
for(t=0;count!=n;t++)
{
// for finding min burst
// it is useful
short_P=9;
for(int i=0;i<n;i++)
{
if(a[i].BT<a[short_P].BT && (a[i].AT<=t && a[i].BT>0))
{
short_P=i;
}

a[short_P].BT=a[short_P].BT-1;

// if any process is completed


if(a[short_P].BT==0)
{
// one process complete
count++;
a[short_P].WT=t+1-a[short_P].AT-temp[short_P];
a[short_P].TAT=t+1-a[short_P].AT;

// total calculation
total_WT=total_WT+a[short_P].WT;
total_TAT=total_TAT+a[short_P].TAT;
}

}
Avg_WT=total_WT/n;
Avg_TAT=total_TAT/n;

// printing of the answer


printf("Id WT TAT\n");
for(int i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,a[i].WT,a[i].TAT);
}
printf("Avg waiting time of the process is %f\n",Avg_WT);
printf("Avg turn around time of the process %f\n",Avg_TAT);

Priority Scheduling Non-Preemptive code:-

#include<stdio.h>
struct process
{
int id,WT,AT,BT,TAT,PR;
};
struct process a[10];

// function for swapping


void swap(int *b,int *c)
{
int tem;
tem=*c;
*c=*b;
*b=tem;
}

//Driver function
int main()
{
int n,check_ar=0;
int Cmp_time=0;
float Total_WT=0,Total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of process \n");
scanf("%d",&n);
printf("Enter the Arrival time , Burst time and priority of the process\n");
printf("AT BT PR\n");
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&a[i].AT,&a[i].BT,&a[i].PR);
a[i].id=i+1;
// here we are checking that arrival time
// of the process are same or different
if(i==0)
check_ar=a[i].AT;

if(check_ar!=a[i].AT )
check_ar=1;

}
// if process are arrived at the different time
// then sort the process on the basis of AT
if(check_ar!=0)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j].AT>a[j+1].AT)
{
swap(&a[j].id,&a[j+1].id);
swap(&a[j].AT,&a[j+1].AT);
swap(&a[j].BT,&a[j+1].BT);
swap(&a[j].PR,&a[j+1].PR);
}
}
}
}

// logic of Priority scheduling ( non preemptive) algo


// if all the process are arrived at different time
if(check_ar!=0)
{
a[0].WT=a[0].AT;
a[0].TAT=a[0].BT-a[0].AT;
// cmp_time for completion time
Cmp_time=a[0].TAT;
Total_WT=Total_WT+a[0].WT;
Total_TAT=Total_TAT+a[0].TAT;
for(int i=1;i<n;i++)
{
int min=a[i].PR;
for(int j=i+1;j<n;j++)
{
if(min>a[j].PR && a[j].AT<=Cmp_time)
{
min=a[j].PR;
swap(&a[i].id,&a[j].id);
swap(&a[i].AT,&a[j].AT);
swap(&a[i].BT,&a[j].BT);
swap(&a[i].PR,&a[j].PR);

}
a[i].WT=Cmp_time-a[i].AT;
Total_WT=Total_WT+a[i].WT;
// completion time of the process
Cmp_time=Cmp_time+a[i].BT;

// Turn Around Time of the process


// compl-Arival
a[i].TAT=Cmp_time-a[i].AT;
Total_TAT=Total_TAT+a[i].TAT;

}
}

// if all the process are arrived at same time


else
{
for(int i=0;i<n;i++)
{
int min=a[i].PR;
for(int j=i+1;j<n;j++)
{
if(min>a[j].PR && a[j].AT<=Cmp_time)
{
min=a[j].PR;
swap(&a[i].id,&a[j].id);
swap(&a[i].AT,&a[j].AT);
swap(&a[i].BT,&a[j].BT);
swap(&a[i].PR,&a[j].PR);
}

}
a[i].WT=Cmp_time-a[i].AT;
// completion time of the process
Cmp_time=Cmp_time+a[i].BT;

// Turn Around Time of the process


// compl-Arrival
a[i].TAT=Cmp_time-a[i].AT;
Total_WT=Total_WT+a[i].WT;
Total_TAT=Total_TAT+a[i].TAT;

Avg_WT=Total_WT/n;
Avg_TAT=Total_TAT/n;

// Printing of the results


printf("The process are\n");
printf("ID WT TAT\n");
for(int i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",a[i].id,a[i].WT,a[i].TAT);
}

printf("Avg waiting time is: %f\n",Avg_WT);


printf("Avg turn around time is: %f",Avg_TAT);
return 0;
}

Priority Scheduling Preemptive code:-

#include<stdio.h>
struct process
{
int WT,AT,BT,TAT,PT;
};

struct process a[10];

int main()
{
int n,temp[10],t,count=0,short_p;
float total_WT=0,total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of the process\n");
scanf("%d",&n);
printf("Enter the arrival time , burst time and priority of the process\n");
printf("AT BT PT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&a[i].AT,&a[i].BT,&a[i].PT);

// copying the burst time in


// a temp array fot futher use
temp[i]=a[i].BT;
}

// we initialize the burst time


// of a process with maximum
a[9].PT=10000;

for(t=0;count!=n;t++)
{
short_p=9;
for(int i=0;i<n;i++)
{
if(a[short_p].PT>a[i].PT && a[i].AT<=t && a[i].BT>0)
{
short_p=i;
}
}

a[short_p].BT=a[short_p].BT-1;

// if any process is completed


if(a[short_p].BT==0)
{
// one process is completed
// so count increases by 1
count++;
a[short_p].WT=t+1-a[short_p].AT-temp[short_p];
a[short_p].TAT=t+1-a[short_p].AT;

// total calculation
total_WT=total_WT+a[short_p].WT;
total_TAT=total_TAT+a[short_p].TAT;

}
}

Avg_WT=total_WT/n;
Avg_TAT=total_TAT/n;

// printing of the answer


printf("ID WT TAT\n");
for(int i=0;i<n;i++)
{
printf("%d %d\t%d\n",i+1,a[i].WT,a[i].TAT);
}

printf("Avg waiting time of the process is %f\n",Avg_WT);


printf("Avg turn around time of the process is %f\n",Avg_TAT);

return 0;
}

Round Robin code:-

#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();
}
fork() system call code:-

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

int main() {
int n;
cout<<"Enter number of elements in array: ";
cin>>n;
vector<int> arr(n); //Used for creating an array
cout<<"Enter array elements: ";
for(int i = 0;i < n;i++) cin>>arr[i];
int p = fork();
if(p > 0) {
//asc code
for(int i = 0;i < n;i++) {
for(int j = i+1;j < n;j++) {
if(arr[i] > arr[j])
swap(arr[i],arr[j]);
}
}
for(int i = 0;i < n;i++) {
cout<<arr[i]<<" Printed by pid: "<<getpid()<<" with ppid: "<<getppid()<<endl;
sleep(1);
}
sleep(100); // for making child process a zombie process
wait(NULL); // For waiting until child terminates
cout<<"Parent Terminating"<<endl;
}
else if(p == 0) {
//desc code
for(int i = 0;i < n;i++) {
for(int j = i+1;j < n;j++) {
if(arr[i] < arr[j])
swap(arr[i],arr[j]);
}
}
for(int i = 0;i < n;i++) {
cout<<"\t\t"<<arr[i]<<" Printed by pid: "<<getpid()<<" with ppid: "<<getppid()<<endl;
sleep(2);
}
cout<<"Child Terminating"<<endl;
}
}

You might also like