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

Os Practical File 1

The document contains source code for implementing Round Robin Scheduling in C. It includes functions for calculating waiting time, turnaround time, and average waiting and turnaround times. The code takes processes, burst times, and time quantum as inputs. It uses a while loop to iterate through processes and allocate the quantum time before calculating waiting times, turnaround times, and average times which are printed as output.

Uploaded by

Aditya Bansal
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)
421 views

Os Practical File 1

The document contains source code for implementing Round Robin Scheduling in C. It includes functions for calculating waiting time, turnaround time, and average waiting and turnaround times. The code takes processes, burst times, and time quantum as inputs. It uses a while loop to iterate through processes and allocate the quantum time before calculating waiting times, turnaround times, and average times which are printed as output.

Uploaded by

Aditya Bansal
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/ 24

PRACTICAL FILE / TERM WORK

on
OPERATING SYSTEMS LAB
(PCS-506)
2022-23

SUBMITTED TO: SUBMITTED BY:

MRS.HIMADR
I VAIDYA
NEHA
KUMARI

Assistant Professor SECTION- ‘I’

GEHU, Dehradun UNIV. ROLL NO- 2018515

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


GRAPHIC ERA HILL UNIVERSITY, DEHRADUN

TABLE OF CONTENTS

S. NO PROGRAM NAME SIGN


1. Implementation of basic commands such as i) ls
and ii) cp command in C program.

2. Implementation of fork () command in different


ways.

3. Write a program to Print odd and even numbers


in Linux.

4. Implementation of First Come First Serve


Scheduling in C.

5. Implementation of Shortest Job First Search


(Non- Premptive) Scheduling in C.

6. Implementation of Shortest Remaining Job First


(Premptive) Scheduling in C.

7. Implementation of Longest Job First Search


(Non-Premptive) Scheduling in C.

8. Implementation of Longest Remaining Time First


(Premptive) Scheduling in C.

9. Implementation of Round Robin Scheduling in C.


10. Implementation of Priority Based (Premptive)
Scheduling in C.

11. Implementation of Priority Based (Non-


Premptive) Scheduling in C.

PROGRAM – 1
OBJECTIVE - Implementation of basic commands such as i) ls and ii) cp
command in C program.

SOURCE CODE –

#include<sys/wait.h>

#include<unistd.h>

int main () {

int a[2];

pipe(a);

if (! fork()) {

close(1);

dup(a[1]);

close(a[0]);

execlp(“ls”, “ls”, NULL);}

else {

close(0);

dup(a[0]);

close(a[1]);

execlp(“wc”, “wc”, NULL);}}

OUTPUT –
PROGRAM – 2
OBJECTIVE - Implementation of fork () command in different ways.

SOURCE CODE –

1. #include<stdio.h>

#include<sys/types.h>

#include<unistd.h>

int main () {

fork();

printf(“Hello World!\n”);

return 0;

2. #include<stdio.h>

#include<sys/types.h>

#include<unistd.h>

void forkexample(){

if(fork()==0)

printf(“Hello from Child!\n”);

else

printf(“Hello from Parent!\n”);}

int main () {
forkexample();

return 0;

OUTPUT –
PROGRAM – 3

OBJECTIVE - Write a program to Print odd and even numbers in Linux.

SOURCE CODE –

# Take user input


echo "Enter a number"
read n
echo "Even Numbers - "
i=1
# -le means less than or equal to
while [ $i -le $n ]
do
# arithmetic operations is performed with 'expr'

rs=`expr $i % 2`

if [ $rs == 0 ]

then

echo " $i"

# end of if loop

Fi

# incrementing i by one

((++i))
# end of while loop
done

# Now printing odd numbers


echo "Odd Numbers - "
i=1
while [ $i -le $n ]
do
rs=`expr $i % 2`
if [ $rs != 0 ]
then
echo " $i"
fi

((++i))
done

OUTPUT –
PROGRAM – 4

OBJECTIVE - Implementation of First Come First Serve Scheduling in C.

SOURCE CODE –
#include<stdio.h>
int main(){
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("nEnter Process Burst Time");
for(i=0;i<n;i++) {
printf("P[%d]:",i+1);
scanf("%d",&bt[i]); }
wt[0]=0;
for(i=1;i<n;i++) {
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j]; }
printf("\nProcess\t\tBurst Time\t\tWaiting Time\t\tTurnaround Time\n");
for(i=0;i<n;i++){
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d\n",i+1,bt[i],wt[i],tat[i]); }
avwt/=i;
avtat/=i;
printf("nnAverage Waiting Time:%d\n",avwt);
printf("nAverage Turnaround Time:%d",avtat);
return 0; }

OUTPUT –
PROGRAM – 5

OBJECTIVE - Implementation of Shortest Job First Search (Non- Premptive)


Scheduling in C.
SOURCE CODE –
#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; }
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 BurstTime tWaitingTime tTurnaroundTime”);
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("nAverage Waiting Time=%f",avg_wt);
printf("Avg Turnaround Time=%fn",avg_tat); }

OUTPUT –
PROGRAM – 6

OBJECTIVE - Implementation of Shortest Remaining Job First (Premptive)


Scheduling in C.
SOURCE CODE –
#include <stdio.h>
int main () {
int arrival_time[10], burst_time[10], temp[10];
int i, smallest, count = 0, time, limit;
double wait_time = 0, turnaround_time = 0, end;
float average_waiting_time, average_turnaround_time;
printf("Enter the Total Number of Processes:\n");
scanf("%d", &limit);
printf("Enter Details of %d Processes", limit);
for(i = 0; i < limit; i++) {
printf("Enter Arrival Time:");
scanf("%d", &arrival_time[i]);
printf("Enter Burst Time:");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i]; }
burst_time[9] = 9999;
for(time = 0; count != limit; time++) {
smallest = 9;
for(i = 0; i < limit; i++) {
if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_time[i] > 0) {
smallest = I;}}
burst_time[smallest]--;
if(burst_time[smallest] == 0){
count++;
end = time + 1;
wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];
turnaround_time = turnaround_time + end - arrival_time[smallest]; } }
average_waiting_time = wait_time / limit;
average_turnaround_time = turnaround_time / limit;
printf("Average Waiting Time:%lf\n", average_waiting_time);
printf("Average Turnaround Time%lf\n", average_turnaround_time);
return 0; }

OUTPUT –
PROGRAM - 7
OBJECTIVE - Implementation of Longest Job First Search (Non-Premptive)
Scheduling in C.
SOURCE CODE –
#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\n:");
scanf("%d",&n);
printf("Enter Burst Time:\n");
for(i=0;i<n;i++) {
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; }
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("Process\t\tBurst Time\t\tWaiting Time\t\tTurnaround Time\n");
for(i=0;i<n;i++) {
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("p%d\t\t%d\t\t%d\t\t%d\n",p[i],bt[i],wt[i],tat[i]);}
avg_tat=(float)total/n;
printf("Average Waiting Time=%f\n",avg_wt);
printf("Average Turnaround Time=%f\n",avg_tat);}

OUTPUT –
PROGRAM - 8
OBJECTIVE - Implementation of Longest Remaining Time First (Premptive)
Scheduling in C.
SOURCE CODE –
#include <stdio.h>
int main () {
int arrival_time[10], burst_time[10], temp[10];
int i, smallest, count = 0, time, limit;
double wait_time = 0, turnaround_time = 0, end;
float average_waiting_time, average_turnaround_time;
printf("Enter the Total Number of Processes:");
scanf("%d", &limit);
printf("Enter Details of %d Processes", limit);
for(i = 0; i < limit; i++){
printf("Enter Arrival Time:");
scanf("%d", &arrival_time[i]);
printf("Enter Burst Time:");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i]; }
burst_time[9] = 0;

for(time = 0; count != limit; time++){

smallest = 0;

for(i = 0; i >limit; i++){

if(arrival_time[i]>=time&&burst_time[i]>burst_time[smallest]&&burst_time[i]>0){

smallest = i;}}

burst_time[smallest]--;
if(burst_time[smallest] == 0){
count++;
end = time + 1;
wait_time = wait_time - end - arrival_time[smallest] - temp[smallest];
turnaround_time = turnaround_time + end - arrival_time[smallest]; } }
average_waiting_time = wait_time / limit;
average_turnaround_time = turnaround_time / limit;
printf("Average Waiting Time:%f\n", average_waiting_time);
printf("Average Turnaround Time:%f\n", average_turnaround_time);
return 0; }

OUTPUT –
PROGRAM – 9
OBJECTIVE - Implementation of Round Robin Scheduling in C.
SOURCE CODE –

#include <stdio.h>
int turnarroundtime(int processes[],int n,int bt[],int wt[],int tat[]) {
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];return 1;}
int waitingtime(int processes[],int n,int bt[],int wt[],int quantum) {
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
int t = 0;
while (1) {
bool done = true;
for (int i = 0 ; i < n; i++) {
if (rem_bt[i] > 0) {
done = false;
if (rem_bt[i] > quantum) {
t += quantum;
rem_bt[i] -= quantum; }
else {
t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0; } } }
if (done == true) break; }
return 1; }
int findavgTime(int processes[], int n, int bt[], int quantum) {
int wt[n], tat[n], total_wt = 0, total_tat = 0;
waitingtime(processes, n, bt, wt, quantum);
turnarroundtime(processes, n, bt, wt, tat);
printf("Processes Burst Time Waiting Time turnaround time\n");
for (int i=0; i<n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf("\t%d\t\t\t%d\t\t\t%d\t\t\t%d\n",i+1, bt[i], wt[i], tat[i]); }
printf("Average waiting time = %f", (float)total_wt / (float)n);
printf("\nAverage turnaround time = %f\n", (float)total_tat / (float)n);
return 1;}
int main() {
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
int burst_time[] = {8, 2, 7};
int quantum = 2;
findavgTime(processes, n, burst_time, quantum);
return 0; }

OUTPUT –
PROGRAM – 10
OBJECTIVE - Implementation of Priority Based (Premptive) Scheduling in C.
SOURCE CODE –

#include<stdio.h>
int main () {
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:\n");
scanf("%d",&n);
printf("Enter Burst Time and Priority\n");
for(i=0;i<n;i++){
printf("P[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; }

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

pos=i;

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

if(pr[j]<pr[pos])

pos=j; }

temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
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=total/n;
total=0;
printf("\nProcess\t\tBurst Time\t\tWaiting Time\t\tTurnaround Time");
for(i=0;i<n;i++){
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\nP[%d]\t\t%d\t\t\t%d\t\t\t%d\n",p[i],bt[i],wt[i],tat[i]); }
avg_tat=total/n;
printf("Average Waiting Time=%d\n",avg_wt);
printf("Average Turnaround Time=%d",avg_tat);
return 0; }

OUTPUT –
PROGRAM – 11
OBJECTIVE - Implementation of Priority Based (Non- Premptive) Scheduling
in C.

SOURCE CODE –

#include <stdio.h>

void swap(int *a,int *b){

int temp=*a; *a=*b;

*b=temp;}

int main(){

int n;

printf("Enter Number of Processes: ");

scanf("%d",&n);

int b[n],p[n],index[n];

for(int i=0;i<n;i++){
printf("Enter Burst Time and Priority Value for Process %d: ",i+1);

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

index[i]=i+1; }

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

int a=p[i],m=i;

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

if(p[j] > a) {

a=p[j];

m=j;} }

swap(&p[i], &p[m]);

swap(&b[i], &b[m]);

swap(&index[i],&index[m]); }

int t=0;

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

t+=b[i]; }

printf("\nProcess Id\t\tBurst Time\t\tWaiting Time\t\tTurnAround Time\n");


int wait_time=0; for(int i=0;i<n;i++){
printf("P%d\t\t%d\t\t%d\t\t%d\n",index[i],b[i],wait_time,wait_time + b[i]);
wait_time += b[i]; }
return 0; }
OUTPUT –

You might also like