Assignment : 3
Set A:
i) Write the program to simulate FCFS CPU-scheduling. The arrival time and first CPU-
Burst for different n number of processes should be input to the algorithm. Assume that
the fixed IO waiting time (2 units). The next CPU-burst should be generated randomly.
The output should give Gantt chart, turnaround time and waiting time for each process.
Also find the average waiting time and turnaround time.
CODE:
👇
- #include<stdio.h>
- #include<stdlib.h>
- struct proc_info
- {
- int atime;
- int cpub;
- int pid;
- } proc[20], temp;
- struct ganttchart
- {
- int stime;
- int pid;
- int etime;
- } gchart[20];
- int i,j,ctime,n,gi=0,wtime[20],ttime[20];
- void getprocs();
- void start();
- void dispgchart();
- void addgchart(int);
- void disptimes();
- int main()
- {
- getprocs();
- ctime=0;
- start();
- dispgchart();
- disptimes();
- }
- void getprocs()
- {
- printf("\nHow many processes:");
- scanf("%d",&n);
- printf("\nPID\tATIME\tCPUB\n");
- for(i=1; i<=n; i++)
- {
- scanf("%d%d%d",&proc[i].pid, &proc[i].atime, &proc[i].cpub);
- }
- for(i=1; i<=n; i++)
- {
- for(j=i+1; j<=n; j++)
- {
- if(proc[i].atime > proc[j].atime)
- {
- temp = proc[i];
- proc[i] = proc[j];
- proc[j] = temp;
- }
- }
- }
- for(i=1; i<=n; i++)
- {
- printf("\n%d\t%d\t%d",proc[i].pid, proc[i].atime, proc[i].cpub);
- }
- }//getprocs
- void start()
- {
- int pid;
- for(i=1; i<=n; i++)
- {
- wtime[i] = ctime-proc[i].atime;
- addgchart(proc[i].pid);
- while(proc[i].cpub != 0)
- {
- ctime++;
- proc[i].cpub--;
- if(proc[i].cpub==0)//proc finished
- {
- printf("\nProc %d completed at time %d", proc[i].pid,ctime);
- ttime[i] = ctime-proc[i].atime;
- }//if
- }
- }
- gchart[gi].etime = ctime;
- }//start
- void addgchart(int pid)
- {
- gchart[++gi].pid = pid;
- gchart[gi].stime = ctime;
- gchart[gi-i].etime = gchart[gi].stime;
- }
- void dispgchart()
- {
- printf("\n");
- for(i=1; i<=n; i++)
- printf("|--");
- printf("|\n");
- for(i=1; i<=n; i++)
- printf("| %d", gchart[i].pid);
- printf("|\n");
- for(i=1; i<=gi; i++)
- printf("|--");
- printf("|\n");
- for(i=1; i<=gi; i++)
- printf("%d ",gchart[i].stime);
- printf("%d\t",gchart[gi].etime);
- }
- void disptimes()
- {
- int sumwt=0, sumtt=0,pid;
- printf("\n\n**Waiting Time**");
- printf("\nPID\tWtimes");
- for(i=1; i<=n; i++)
- {
- printf("\n%d\t%d",proc[i].pid, wtime[i]);
- sumwt += wtime[i];
- }
- printf("\nAverage: %.2f", (float)sumwt/n);
- printf("\n\n**Turnaround Time**");
- printf("\nPID\tttime");
- for(i=1; i<=n;i++)
- {
- printf("\n%d\t%d",i,ttime[i]);
- sumtt+=ttime[i];
- }
- printf("\nAverage: %.2f", (float)sumtt/n);
- printf("\n");
- }
ii) Write the program to simulate Non-preemptive Shortest Job First (SJF) -scheduling. The
arrival time and first CPU-burst for different n number of processes should be input to the
algorithm. Assume the fixed IO waiting time (2 units). The next CPU-burst should be
generated randomly. The output should give Gantt chart, turnaround time and waiting
time for each process. Also find the average waiting time and turnaround time.
CODE:
👇
- #include<stdio.h>
- #include<stdlib.h>
- struct proc_info
- {
- int atime;
- int cpub;
- int pid;
- } proc[20], temp;
- struct ganttchart
- {
- int stime;
- int pid;
- int etime;
- } gchart[20];
- int i,j,ctime,n,gi=0,wtime[20],ttime[20];
- void getprocs();
- void start();
- void dispgchart();
- void addgchart(int);
- void disptimes();
- int main()
- {
- getprocs();
- ctime=0;
- start();
- dispgchart();
- disptimes();
- }
- void getprocs()
- {
- printf("\nHow many processes:");
- scanf("%d",&n);
- printf("\nPID\tATIME\tCPUB\n");
- for(i=1; i<=n; i++)
- {
- scanf("%d%d%d",&proc[i].pid, &proc[i].atime, &proc[i].cpub);
- }
- for(i=1; i<=n; i++)
- {
- for(j=i+1; j<=n; j++)
- {
- if(proc[i].cpub > proc[j].cpub)
- {
- temp = proc[i];
- proc[i] = proc[j];
- proc[j] = temp;
- }
- }
- }
- for(i=1; i<=n; i++)
- {
- printf("\n%d\t%d\t%d",proc[i].pid, proc[i].atime, proc[i].cpub);
- }
- }//getprocs
- void start()
- {
- int pid;
- for(i=1; i<=n; i++)
- {
- wtime[i] = ctime-proc[i].atime;
- addgchart(proc[i].pid);
- while(proc[i].cpub != 0)
- {
- ctime++;
- proc[i].cpub--;
- if(proc[i].cpub==0)//proc finished
- {
- printf("\nProc %d completed at time %d", proc[i].pid,ctime);
- ttime[i] = ctime-proc[i].atime;
- }//if
- }
- }
- gchart[gi].etime = ctime;
- }//start
- void addgchart(int pid)
- {
- gchart[++gi].pid = pid;
- gchart[gi].stime = ctime;
- gchart[gi-i].etime = gchart[gi].stime;
- }
- void dispgchart()
- {
- printf("\n");
- for(i=1; i<=n; i++)
- printf("|--");
- printf("|\n");
- for(i=1; i<=n; i++)
- printf("| %d", gchart[i].pid);
- printf("|\n");
- for(i=1; i<=gi; i++)
- printf("|--");
- printf("|\n");
- for(i=1; i<=gi; i++)
- printf("%d ",gchart[i].stime);
- printf("%d\t",gchart[gi].etime);
- }
- void disptimes()
- {
- int sumwt=0, sumtt=0,pid;
- printf("\n\n**Waiting Time**");
- printf("\nPID\tWtimes");
- for(i=1; i<=n; i++)
- {
- printf("\n%d\t%d",proc[i].pid, wtime[i]);
- sumwt += wtime[i];
- }
- printf("\nAverage: %.2f", (float)sumwt/n);
- printf("\n\n**Turnaround Time**");
- printf("\nPID\tttime");
- for(i=1; i<=n;i++)
- {
- printf("\n%d\t%d",i,ttime[i]);
- sumtt+=ttime[i];
- }
- printf("\nAverage: %.2f", (float)sumtt/n);
- printf("\n");
- }
Set B:
ii) Write the program to simulate Non-preemptive Priority scheduling. The arrival time and
first CPU-burst and priority for different n number of processes should be input to the
algorithm. Assume the fixed IO waiting time (2 units). The next CPU-burst should be
generated randomly. The output should give Gantt chart, turnaround time and waiting
time for each process. Also find the average waiting time and turnaround time.
CODE:
👇
- #include<stdio.h>
- #include<stdlib.h>
- struct proc_info
- {
- int atime;
- int cpub;
- int pid;
- int priority;
- } proc[20],temp;
- struct ganttchart
- {
- int stime;
- int pid;
- int etime;
- }gchart[20];
- int i,j,n,ctime,gi=0,wtime[20],ttime[20];
- void getprocs();
- void start();
- void dispgchart();
- void addgchart(int);
- void disptimes();
- int finished();
- int main()
- {
- // randomize();
- getprocs();
- ctime=0; //set current time to 0
- start();
- dispgchart();
- disptimes();
- }
- void getprocs()
- {
- printf("\n How many processes :");
- scanf("%d",&n);
- printf("\n PID\tATIME \tCPUB \tPRIORITY\n");
- for(i=1;i<=n; i++)
- {
- scanf("%d%d%d%d",&proc[i].pid,&proc[i].atime,&proc[i].cpub,&proc[i].priority);
- }
- for(i=1;i<=n;i++)
- {
- for(j=i+1;j<=n;j++)
- {
- if(proc[i].priority<proc[j].priority)
- {
- temp=proc[i];
- proc[i]=proc[j];
- proc[j]=temp;
- }
- }
- }
- for(i=1;i<=n;i++)
- {
- //printf("%d\t",i);
- printf("\n %d\t%d\t%d\t%d",proc[i].pid,proc[i].atime,proc[i].cpub,proc[i].priority);
- }
- }//get proc
- void start()
- {
- int pid;
- while(!finished())
- {
- for(i=1;i<=n;i++)
- {
- if(proc[i].atime<=ctime&&proc[i].cpub!=0)
- {
- wtime[i]=ctime-proc[i].atime;
- addgchart(proc[i].pid);
- while(proc[i].cpub!=0)
- {
- ctime++; //increament current time
- proc[i].cpub--;
- if(proc[i].cpub==0) //proc finished
- {
- printf("\n Proc %d completed at time %d..",proc[i].pid,ctime);
- ttime[i]=ctime-proc[i].atime;
- break;
- }
- }
- }
- }
- }
- gchart[gi].etime=ctime;
- }//start
- void addgchart(int pid)
- {
- gchart[++gi].pid=pid;
- gchart[gi].stime=ctime;
- gchart[gi-1].etime=gchart[gi].stime;
- }
- int finished()
- {
- for(i=1;i<=n; i++)
- {
- if(proc[i].cpub!=0)
- return 0;
- }
- return 1;
- }
- void dispgchart()
- {
- printf("\n");
- for(i=1; i<=gi; i++)
- printf("|-----");
- printf("|\n");
- for(i=1;i<=gi;i++)
- printf("| %d ",gchart[i].pid);
- printf("|\n");
- for(i=1; i<=gi; i++)
- printf("|-----");
- printf("|\n");
- for(i=1;i<=gi;i++)
- printf("| %d ",gchart[i].stime);
- printf("%d\n",gchart[gi].etime);
- }
- void disptimes()
- {
- int sumwt=0, sumtt=0,pid;
- printf("\n***Waiting time***");
- printf("\nPID\tWtime");
- for(i=1; i<=n; i++)
- {
- printf("\n%d\t%d",proc[i].pid,wtime[i]);
- sumwt+=wtime[i];
- }
- printf("\nAverage:%.2f",(float)sumwt/n);
- printf("\n***Turnaround Time***");
- printf("\nPID\t ttime");
- for(i=1; i<=n; i++)
- {
- printf("\n%d\t%d",i,ttime[i]);
- sumtt+=ttime[i];
- }
- printf("\nAverage:%.2f",(float)sumtt/n);
- }
Set C
ii. Write the program to simulate Round Robin (RR) scheduling. The arrival time and first
CPU-burst for different n number of processes should be input to the algorithm. Also
give the time quantum as input. Assume the fixed IO waiting time (2 units). The next
CPU-burst should be generated randomly. The output should give Gantt chart, turnaround
time and waiting time for each process. Also find the average waiting time and
turnaround time.
CODE:
👇
- #include<stdio.h>
- #include<stdlib.h>
- struct proc_info
- {
- int atime;
- int cpub;
- int pid;
- } proc[20], temp;
- struct ganttchart
- {
- int stime;
- int pid;
- int etime;
- } gchart[20];
- int i,j,ctime,n,gi=0,wtime[20],ttime[20],quantum, cnt;
- void getprocs();
- void start();
- void dispgchart();
- void addgchart(int);
- void disptimes();
- int finished();
- int main()
- {
- getprocs();
- ctime=0;
- start();
- dispgchart();
- disptimes();
- }
- void getprocs()
- {
- printf("\nHow many processes:");
- scanf("%d",&n);
- printf("\nPID\tATIME\tCPUB\n");
- for(i=1; i<=n; i++)
- {
- scanf("%d%d%d",&proc[i].pid, &proc[i].atime, &proc[i].cpub);
- }
- printf("\nEnter Time Quantum\t");
- scanf("%d",&quantum);
- for(i=1; i<=n; i++)
- {
- printf("\n%d\t%d\t%d",proc[i].pid, proc[i].atime, proc[i].cpub);
- }
- }//getprocs
- void start()
- {
- int pid;
- while(!finished())
- {
- for(i=1; i<=n; i++)
- {
- cnt=0;
- if(proc[i].atime <= ctime && proc[i].cpub!=0)
- {
- //wtime[i] = ctime-proc[i].atime;
- wtime[i] = wtime[i]+(ctime-proc[i].atime);
- addgchart(proc[i].pid);
- while(cnt < quantum)
- {
- ctime++;
- proc[i].cpub--;
- cnt++;
- if(proc[i].cpub==0)//proc finished
- {
- printf("\nProc %d completed at time %d", proc[i].pid,ctime);
- ttime[i] = ctime-proc[i].atime;
- }//if
- }//while
- }//if
- }//for
- }//while
- gchart[gi].etime = ctime;
- }//start
- int finished()
- {
- for(i=1; i<=n; i++)
- {
- if(proc[i].cpub != 0)
- return 0;
- }
- return 1;
- }
- void addgchart(int pid)
- {
- gchart[++gi].pid = pid;
- gchart[gi].stime = ctime;
- gchart[gi-i].etime = gchart[gi].stime;
- }
- void dispgchart()
- {
- printf("\n");
- for(i=1; i<=n; i++)
- printf("|--");
- printf("|\n");
- for(i=1; i<=n; i++)
- printf("| %d", gchart[i].pid);
- printf("|\n");
- for(i=1; i<=gi; i++)
- printf("|--");
- printf("|\n");
- for(i=1; i<=gi; i++)
- printf("%d ",gchart[i].stime);
- printf("%d\t",gchart[gi].etime);
- }
- void disptimes()
- {
- int sumwt=0, sumtt=0,pid;
- printf("\n\n**Waiting Time**");
- printf("\nPID\tWtimes");
- for(i=1; i<=n; i++)
- {
- printf("\n%d\t%d",proc[i].pid, wtime[i]);
- sumwt += wtime[i];
- }
- printf("\nAverage: %.2f", (float)sumwt/n);
- printf("\n\n**Turnaround Time**");
- printf("\nPID\tttime");
- for(i=1; i<=n;i++)
- {
- printf("\n%d\t%d",i,ttime[i]);
- sumtt+=ttime[i];
- }
- printf("\nAverage: %.2f", (float)sumtt/n);
- printf("\n");
- }
Comments
Post a Comment