Lab 1:
TITLE: To be familiar with task scheduling algorithms
THEORY
(Write about task scheduling algorithms along with features of each)
PROGRAMS
(Write the program code and then note down the observation and output of each program)
OUTPUT
DISCUSSION
CONCLUSION
Conclude your Lab 1 results
Note: You can attach the screenshots of your program and output but remaining sections must be
handwritten (if possible, in A4 size paper or in normal copy)
1. FIRST COME FIRST SERVE SCHEDULING
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
struct process{
int pid;
int bt;
int wt,tt;
}p[10];
int main()
{
int i,n,totwt,tottt,avg1,avg2;
printf("enter the no of process\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p[i].pid=i;
printf("enter the burst time ");
scanf("%d",&p[i].bt);
}
p[1].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n)
{
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
i ++;
}
i=1;
totwt=tottt=0;
printf("\n processid\t bt\t wt\t tt\n");
while(i<=n)
{
printf("\n\t%d\t%d\t%d\t%d",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
}
avg1=totwt/n;
avg2=tottt/n;
printf("\navg1=%d\t avg2=%d\t",avg1,avg2);
return 0;
}
2. SHORTEST JOB FIRST
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
struct process
{
int pid,bt,wt,tt;
}p[10],temp;
int main()
{
int i,j,n,totwt,tottt;
float avg1,avg2;
printf("\nEnter the number of process:\t");
scanf("%d",&n);
for(i=1;i<=n;i++) {
p[i].pid=i;
printf("\nEnter the burst time:\t");
scanf("%d",&p[i].bt);
}
for(i=1;i<n;i++) {
for(j=i+1;j<=n;j++) {
if(p[i].bt>p[j].bt) {
[Link]=p[i].pid;
p[i].pid=p[j].pid;
p[j].pid=[Link];
[Link]=p[i].bt;p[i].bt=p[j].bt;
p[j].bt=[Link];
}}}
p[1].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n){
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
i++;
}
i=1;
totwt=tottt=0;
printf("\nProcess id \tbt \twt \ttt");
while(i<=n){
printf("\n\t%d \t%d \t%d \t%d\n",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
}
avg1=(float)totwt/n;
avg2=(float)tottt/n;
printf("\nAVG1=%f\t AVG2=%f",avg1,avg2);
return 0;
}
3. PRIORITY SCHEDULING
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
struct process
{
int pid;
int bt;
int wt;
int tt;
int prior;
}
p[10],temp;
int main()
{
int i,j,n,totwt,tottt;
float arg1,arg2;
printf("enter the number of process");
scanf("%d",&n);
for(i=1;i<=n;i++) {
p[i].pid=i;
printf("enter the burst time");
scanf("%d",&p[i].bt);
printf("\n enter the priority");
scanf("%d",&p[i].prior);
}
for(i=1;i<n;i++) {
for(j=i+1;j<=n;j++) {
if(p[i].prior>p[j].prior)
{
[Link]=p[i].pid;
p[i].pid=p[j].pid;
p[j].pid=[Link];
[Link]=p[i].bt;
p[i].bt=p[j].bt;
p[j].bt=[Link];
[Link]=p[i].prior;
p[i].prior=p[j].prior;
p[j].prior=[Link];
}}}
p[i].wt=0;
p[1].tt=p[1].bt+p[1].wt;
i=2;
while(i<=n)
{
p[i].wt=p[i-1].bt+p[i-1].wt;
p[i].tt=p[i].bt+p[i].wt;
i++;
}
i=1;
totwt=tottt=0;
printf("\n process to \t bt \t wt \t tt");
while(i<=n)
{
printf("\n%d\t\t %d\t %d\t %d\t",p[i].pid,p[i].bt,p[i].wt,p[i].tt);
totwt=p[i].wt+totwt;
tottt=p[i].tt+tottt;
i++;
}
arg1=(float)totwt/n;
arg2=(float)tottt/n;
printf("\n arg1=%f \t arg2=%f\t",arg1,arg2);
return 0;
}
4. ROUND ROBIN SCHEDULING
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
struct process
{
int pid,bt,tt,wt;
};
int main()
{
struct process x[10],p[30];
int i,j,k,tot=0,m,n;
float wttime=0.0,tottime=0.0,a1,a2;
printf("\nEnter the number of process:\t");
scanf("%d",&n);
for(i=1;i<=n;i++){
x[i].pid=i;
printf("\nEnter the Burst Time:\t");
scanf("%d",&x[i].bt);
tot=tot+x[i].bt;
}
printf("\nTotal Burst Time:\t%d",tot);
p[0].tt=0;
k=1;
printf("\nEnter the Time Slice:\t");
scanf("%d",&m);
for(j=1;j<=tot;j++) {
for(i=1;i<=n;i++) {
if(x[i].bt !=0) {
p[k].pid=i;
if(x[i].bt-m<0) {
p[k].wt=p[k-1].tt;
p[k].bt=x[i].bt;
p[k].tt=p[k].wt+x[i].bt;
x[i].bt=0;
k++;
}
else
{
p[k].wt=p[k-1].tt;
p[k].tt=p[k].wt+m;
x[i].bt=x[i].bt-m;
k++;
}}}}
printf("\nProcess id \twt \ttt");
for(i=1;i<k;i++){
printf("\n\t%d \t%d \t%d",p[i].pid,p[i].wt,p[i].tt);
wttime=wttime+p[i].wt;
tottime=tottime+p[i].tt;
a1=wttime/n;
a2=tottime/n;
printf("\n\nAverage Waiting Time:\t%f",a1);
printf("\n\nAverage TurnAround Time:\t%f",a2);
return 0;
}