6/recent/ticker-posts

OS First Come First Serve 1

 

Given the process names, their burst times and their arrival order, write a C Program to compute the waiting time and turnaround time of 2 processes using the FCFS Algorithm and display them. The names of the processes are of type character, burst time and arrival order are of the type integers.

 

Input and Output format:

Refer Sample Input and Output for inputs and formatting specifications.

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

Use  "%s%40s%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
10
Enter the arrival order of process 1
2
Enter the name of process 2
B
Enter the burst time of process 2
20
Enter the arrival order of process 2
1

Process Details

Process Name        CPU Burst Time       Arrival Order       Waiting Time          Turnaround Time
B                                     20                       1                          0                            20
A                                     10                       2                          20                          30


Answer:

#include<stdio.h>
int main()
{
    struct process{
        char dname;
        int dwait,dturn,darrival,dburst,temp,dtat;
        float awt,atat;
    }p[2];
    char tempc;
    for(int i=0;i<2;i++)
    {
        printf("Enter the name of process %d\n",i+1);
        scanf("%s",&p[i].dname);
        printf("Enter the burst time of process %d\n",i+1);
        scanf("%d",&p[i].dburst);
        printf("Enter the arrival order of process %d\n",i+1);
        scanf("%d",&p[i].darrival);
    }
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<2-i-1;j++)
        {
            if(p[j].darrival>p[j+1].darrival)
            {
                p[i].temp=p[j].darrival;
                p[j].darrival=p[j+1].darrival;
                p[j+1].darrival=p[i].temp;
                
                tempc=p[j].dname;
                p[j].dname=p[j+1].dname;
                p[j+1].dname=tempc;
                
                p[i].temp=p[j].dburst;
                p[j].dburst=p[j+1].dburst;
                p[j+1].dburst=p[i].temp;
                
            }
        }
    }
    printf("Process Details\n");
    printf("%s%40s%40s%30s%30s\n","Process Name","CPU Burst Time","Arrival Order","Waiting Time","Turnaround Time");
    for(int i=0;i<2;i++)
    {
        p[i].dwait=0;
        p[i].dturn=0;
        for(int j=0;j<i;j++)
        {
            p[i].dwait=p[i].dwait+p[j].dburst;
        }
        p[i].dturn=p[i].dwait+p[i].dburst;
        p[i].awt=p[i].awt+p[i].dwait;
        p[i].atat=p[i].atat+p[i].dturn;
        printf("%s%60d%60d%40d%40d\n",&p[i].dname,p[i].dburst,p[i].darrival,p[i].dwait,p[i].dturn);
    }
    return 0;
}

Post a Comment

0 Comments