6/recent/ticker-posts

OS Round Robin 2

Write a C Program to compute the waiting time and turnaround time for each processes using the Round Robin Algorithm, given names of two process and their burst times. Also compute and print the average waiting time, average turnaround time and Gantt Chart. Assume that the process arrive at the same time t0.


Input and 
Output format :

Refer Sample Input and Output for inputs and formatting specifications.

Use printf("%s%60d%60d%40d\n",p1.name, p1.cbt,p1.wt, p1.tat"); to display the details in the specified format.[same for process 2]

Use  "%s%40s%30s%29s" to print the table header in  the specified format and use 100 hyphens.\
Display the floating point numbers corrected to two decimal places.
 


Sample Input & Output
[Note:All the  Text in Bold corresponds to the input and the rest correspond to the output]


Enter the name of process 1
A
Enter the burst time of process 1
3
Enter the name of process 2
B
Enter the burst time of process 2
4
Enter the time slice
2
Process Details

Process Name           CPU Burst Time           Waiting Time            Turnaround Time

A                                       3                                 2                                 5

B                                       4                                 3                                 7

Average waiting time is 2.50
Average turnaround time is 6.00
----------------------------------------------------------------------------------------------------
| A | A | B | B | A | B | B |
----------------------------------------------------------------------------------------------------
0 1 2 3 4 5 6 7


Answer:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
void FindWaitingTime(char process[],int n,int bt[],int wt[],int quantum,char gant[]){
    int reb_bt[n];
    int i=0;
    for(i=0;i<n;i++){
    reb_bt[i]=bt[i];
    }
    int t=0,index=0;
    while(1){
        bool done=true;
        for(i=0;i<n;i++)
        {
            if(reb_bt[i]>0)
            {
                done=false;
                if(reb_bt[i]>quantum){
                    t +=quantum;
                    reb_bt[i]-=quantum;
                    for(int f=0;f<quantum;f++)
                    {
                        gant[index++]=process[i];
                    }
                }
                else
                {
                    t=t+reb_bt[i];
                    wt[i]=t-bt[i];
                    for(int f=0;f<reb_bt[i];f++)
                    {
                        gant[index++]=process[i];
                    }
                    reb_bt[i]=0;
                }
            }
        }
        if(done==true)
        {
            break;
        }
    }
}
void findTurnAroundTime(char process[],int n,int bt[],int wt[],int tat[]){
    int i;
    for(i=0;i<n;i++)
    {
        tat[i]=bt[i]+wt[i];
    }
}
void findAvgTime(char process[],int n,int bt[],int quantum){
    int wt[n],tat[n],total_wt=0,total_tat=0;
    char gant[bt[0]+bt[1]+1];
    FindWaitingTime(process,n,bt,wt,quantum,gant);
    findTurnAroundTime(process,n,bt,wt,tat);
    printf("Process Details\n");
    printf("%s%40s%30s%30s\n","Process Name","CPU Burst Time","Waiting Time","Turnaround Time");
    int j=0;
    while(j!=2){
        total_wt=total_wt+wt[j];
        total_tat=total_tat+tat[j];
        printf("%c%60d%60d%40d\n",process[j],bt[j],wt[j],tat[j]);
        j+=1;
    }
    printf("Average waiting time is %0.2f\n",((float)total_wt/(float)n));
    printf("Average turnaround time is %0.2f\n",((float)total_tat/(float)n));
    int k;
    int total=bt[0]+bt[1];
    printf("----------------------------------------------------------------------------------------------------");
    for(k=0;k<total;k++)
    {
        printf("| %c ",gant[k]);
    }
    printf("|\n");
    printf("----------------------------------------------------------------------------------------------------");
    for(k=0;k<=total;k++){
        printf("%d ",k);
    }
}
int main(){
    char pname[2];
    int btime[2];
    int tslice=0;
    int i=0;
    for(i=0;i<2;i++)
    {
        printf("Enter the name of process %d\n",i+1);
        scanf("%s",&pname[i]);
        printf("Enter the burst time of process %d\n",i+1);
        scanf("%d",&btime[i]);
    }
    printf("Enter the time slice\n");
    scanf("%d",&tslice);
    findAvgTime(pname,2,btime,tslice);
    return 0;
}


Post a Comment

0 Comments