6/recent/ticker-posts

OS Shortest Job First 2

 Write a C Program to compute the waiting time and turnaround time of 2 processes using SJF algorithm given the process names, their burst times and arrival times. Also compute and print the Gantt Chart, average waiting time and average turnaround time. The names of processes are of type character array,burst time and arrival time are of 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 and also use 94 hyphens '-'.

Display the floating point numbers corrected to two decimal places.

 


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
20
Enter the arrival order of process 1
1
Enter the name of process 2
B
Enter the burst time of process 2
10
Enter the arrival order of process 2
2
Process Details
Process Name    CPU Burst Time        Arrival Order            Waiting Time    Turnaround Time
B                                10                               2                             0                             10
A                                 20                              1                             10                           30
Average waiting time is 5.00
Average turnaround time is 20.00
----------------------------------------------------------------------------------------------
| B | A |
----------------------------------------------------------------------------------------------
0 10 30


Answer:

#include<stdio.h>
int main()
{
    struct process
    {
        char name;
        int waitt,turnt,arrt,bt,temp,tat;
    }p[2];
    float awt,atat;
    char tempc;
    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 arrival order of process %d\n",i+1);
        scanf("%d",&p[i].arrt);
    }
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<2-i-1;j++)
        {
            if(p[j].bt>p[j+1].bt)
            {
                p[i].temp=p[j].bt;
                p[j].bt=p[j+1].bt;
                p[j+1].bt=p[i].temp;
                
                tempc=p[j].name;
                p[j].name=p[j+1].name;
                p[j+1].name=tempc;
                
                p[i].temp=p[j].arrt;
                p[j].arrt=p[j+1].arrt;
                p[j+1].arrt=p[i].temp;
            }
        }
    }
    printf("Process Details\n");
    printf("%s%40s%40s%30s%30s","Process Name","CPU Burst Time","Arrival Order","waiting Time","Turnaround Time\n");
    for(int i=0;i<2;i++)
    {
        p[i].waitt=0;
        p[i].turnt=0;
        for(int j=0;j<i;j++)
        {
            p[i].waitt=p[i].waitt+p[j].bt;
        }
        p[i].turnt=p[i].waitt+p[i].bt;
        awt=awt+p[i].waitt;
        atat=atat+p[i].turnt;
        printf("%s%60d%60d%40d%40d\n",&p[i].name,p[i].bt,p[i].arrt,p[i].waitt,p[i].turnt);
    }
    awt=awt/2;
    atat=atat/2;
    printf("Average waiting time is %0.2f\n",awt);
    printf("Average turnaround time is %0.2f\n",atat);
    printf("----------------------------------------------------------------------------------------------\n");
    printf("| %c | %c |\n",p[0].name,p[1].name);
    printf("----------------------------------------------------------------------------------------------\n");
    printf("0 %d %d\n",p[0].turnt,p[1].turnt);
}


Post a Comment

0 Comments