6/recent/ticker-posts

OS Round Robin 1

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. 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.


Sample Input and 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


Answer:

#include<stdio.h>
#include<stdbool.h>
int main()
{
    struct process{
        char name;
        int bt,wt,tat,rembt;
    }p[2];
    int ts;
    for(int i=0;i<2;i++)
    {
        printf("Enter the name of process %d\n",i+1);
        scanf("%s",&p[i].name);
        printf("Enter the burst time of process %d\n",i+1);
        scanf("%d",&p[i].bt);
    }
    printf("Enter the time slice\n");
    scanf("%d",&ts);
    printf("Process Details\n");
    printf("%s%40s%30s%30s","Process Name","CPU Burst Time","Waiting Time","Turnaround Time\n");
    for(int i=0;i<2;i++)
    {
        p[i].rembt=p[i].bt;
    }
    int t=0;
    while(1){
        bool done=true;
        for(int i=0;i<2;i++)
        {
            if(p[i].rembt>0){
                done=false;
                if(p[i].rembt>ts)
                {
                    t+=ts;
                    p[i].rembt-=ts;
                }
                else
                {
                    t=t+p[i].rembt;
                    p[i].wt=t-p[i].bt;
                    p[i].rembt=0;
                }
            }
        }
        if(done==true)
        {
            break;
        }
    }
    for(int i=0;i<2;i++)
    {
        p[i].tat=p[i].bt+p[i].wt;
    }
    for(int i=0;i<2;i++)
    {
        printf("%c%60d%60d%40d\n",p[i].name,p[i].bt,p[i].wt,p[i].tat);
    }
    return 0;
}

Post a Comment

0 Comments