0% found this document useful (0 votes)
158 views7 pages

Os Lab Assignment-1: FCFS Process Scheduling

The document contains code snippets demonstrating different process scheduling algorithms including FCFS, priority scheduling, shortest job first scheduling, preemptive SJF, and round robin scheduling. It also includes examples of process creation and handling zombie and orphan processes.

Uploaded by

mee
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)
158 views7 pages

Os Lab Assignment-1: FCFS Process Scheduling

The document contains code snippets demonstrating different process scheduling algorithms including FCFS, priority scheduling, shortest job first scheduling, preemptive SJF, and round robin scheduling. It also includes examples of process creation and handling zombie and orphan processes.

Uploaded by

mee
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/ 7

Os Lab Assignment-1

FCFS Process Scheduling:

#include<stdio.h> int main(){

int n, i, j, bursttimearr[50];

int waitingtimearr[50];

int completiontimearr[50];

intarrivtime[50];

int ttimearr[50];

int avgwait= 0,avgttime=0;

scanf("%d",&n);

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

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

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

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

}
waitingtimearr[0]=0;

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

waitingtimearr[i]=0;

for(j=0;j<i;j++) waitingtimearr[i]+=bursttimearr[j];

}
for(i=0;i<n;i++)
{
completiontimearr[i]=bursttimearr[i]+waitingtimearr[i];
ttimearr[i]=completiontimearr[i]-arrivtime[i]; avgwait+=waitingtimearr[i];

avgttime+=ttimearr[i];

avgwait=avgwait/i; avgttime=avgttime/i; printf("%d",avgttime); printf("%d",avgwait);

Priority Scheduling:
#include<stdio.h> int main(){

int
i,j,n,bursttime[50],p[50],prior[50],waitingtime[50],turnartime[50],tot=0,avgturnartime,avgwaitingti
me,temp,pos; scanf("%d",&n);

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

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

p[i]=i+1;

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

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

{
if(prior[j]<prior[pos])

pos=j;

temp=prior[i]; prior[i]=prior[pos]; prior[pos]=temp; temp=bursttime[i];


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

waitingtime[0]=0;

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

waitingtime[i]=0; for(j=0;j<i;j++)

waitingtime[i]+=bursttime[j];

tot+=waitingtime[i];
}
avgwaitingtime=tot/n;

tot=0;

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

turnartime[i]=bursttime[i]+waitingtime[i]; tot+=turnartime;

avgturnartime=tot/n; printf("%d",avgturnartime); printf("%d",avgwaitingtime); return 0;


}

Shortest Job First Scheduling:


#include<stdio.h> int main()

int n,j,temp,temp1,temp2,pr[10],b[10],t[10],w[10],p[10],i,at[10] float att=0,awt=0; for(i=0;i<10;i++)


{ b[i]=0;w[i]=0;

scanf("%d",&n); for(i=0;i<n:i++)

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

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

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

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

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

{ if(b[i]>b[j])

{ temp=b[i]; temp1=p[i];
b[i]=b[j]; p[i]=p[j]; b[j]=temp;
p[j]=temp1;

}
w[0]=0; for(i=0;i<n;i++) w[i+1]=w[i]+b[i]; for(i=0;i<n;i++)

{
t[i]=w[i]+b[i]; awt=awt+w[i]; att=att+t[i];

awt=awt/n; att=att/n; for(i=0;i<n;i++) printf(" %f\n",att); printf("%f\n",awt); return 0;

Preemptive SJF(SRTN):
#include<stdio.h> int main()
{

int i,n,t,sm,count=0,arr_time[50],b_time[50],temp[50]; float avg_wttime,avg_tutime; double


wt_time=0,tu_time=0,end; scanf("%d",&n); for(i=0;i<n;i++){ scanf("%d",&arr_time[i]);
scanf("%d",&b_time[i]);

b_time[9]=9999; for(t=0;count!=n;t++)
{

sm=9; for(i=0;i<n;i++){ if(arr_time[i]<=t && b_time[i]<b_time[sm] && b_time[i]>0)

{ sm=i;

}
b_time[sm]--; if(b_time[sm]==0)
{

count++; end=t+1;

wt_time=wt_time+end-arr_time[sm]-temp[sm]; tu_time=tu_time+end-arr_time[sm];
}

}
avg_wttime=wt_time/n;

avg_tutime=tu_time/n; printf("%.2f",avg_wttime); printf("%.2f",avg_tutime);

Round Robin:
#include<stdio.h> int main()

int i,total = 0, n,x, count = 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;
scanf("%d", &n); scanf("%d", &time_quantum);

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


{

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


burst_time[i];

for(total = 0, i= 0; x != 0;)

if(temp[i] <= time_quantum && temp[i] > 0)

total = total + temp[i]; temp[i] = 0; count = 1;

}
else if(temp[i] > 0)

temp[i] = temp[i] - time_quantum; total = total + time_quantum;

if(temp[i] == 0 && count == 1)


{ x--;

wait_time = wait_time + total - arrival_time[i] - burst_time[i];

count = 0;

} if(i == n- 1)
{ i = 0;

else if(arrival_time[i + 1] <= total)

{ i++; } else { i = 0;

average_wait_time = wait_time * 1.0 / n; printf("%.2f", average_wait_time); return 0;

Process Creation:
#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int main()
{

pid_t ret; printf("The process id is - %d",getpid()); if(ret<0)


{

printf("\nfork failure");
}

else if(ret==0)

printf("\nChild process- %d",getpid()); char *ar[]={"Hi","Welcome",NULL}; execv(ar[0],ar);

else

wait(); printf("\nParent process- %d",getpid());


}

}
Zombie & Orphan Process:
#include<stdlib.h>

#include<stdio.h>

#include<sys/types.h>
#include<unistd.h>
int main(){
pid_t child_pid=fork();

if (child_pid>0)

sleep(50);

else

exit(0);

return 0;

int pid=fork();
if(pid>0)
printf("In parent processs");
else if(pid==0)
{

sleep(30); printf("In child process");

You might also like