1+ #include <stdio.h>
2+ #include <stdlib.h>
3+ #define MAX_PROCESSES 10
4+ typedef struct
5+ {
6+ int pid ; // process ID
7+ int burst_time ; // burst time (in milliseconds)
8+ int priority ; // priority (higher number means higher priority)
9+ int waiting_time ; // waiting time (in milliseconds)
10+ int turnaround_time ; // turnaround time (in milliseconds)
11+ } Process ;
12+ int main ()
13+ {
14+ Process processes [MAX_PROCESSES ];
15+ int num_processes , i , j ;
16+ printf ("Enter the number of processes: " );
17+ scanf ("%d" , & num_processes );
18+ for (i = 0 ; i < num_processes ; i ++ ) {
19+ printf ("\nEnter the burst time for process %d (in milliseconds): " , i + 1 );
20+ scanf ("%d" , & processes [i ].burst_time );
21+ printf ("Enter the priority for process %d (higher number means higher priority): " , i + 1 );
22+ scanf ("%d" , & processes [i ].priority );
23+ processes [i ].pid = i + 1 ;
24+ }
25+ for (i = 0 ; i < num_processes - 1 ; i ++ )
26+ {
27+ for (j = i + 1 ; j < num_processes ; j ++ )
28+ {
29+ if (processes [i ].priority < processes [j ].priority )
30+ {
31+ Process temp = processes [i ];
32+ processes [i ] = processes [j ];
33+ processes [j ] = temp ;
34+ }
35+ }
36+ }
37+ int total_waiting_time = 0 , total_turnaround_time = 0 ;
38+ for (i = 0 ; i < num_processes ; i ++ )
39+ {
40+ if (i == 0 )
41+ {
42+ processes [i ].waiting_time = 0 ;
43+ }
44+ else
45+ {
46+ processes [i ].waiting_time = processes [i - 1 ].turnaround_time ;
47+ }
48+ processes [i ].turnaround_time = processes [i ].waiting_time + processes [i ].burst_time ;
49+ total_waiting_time += processes [i ].waiting_time ;
50+ total_turnaround_time += processes [i ].turnaround_time ;
51+ }
52+ printf ("\nPID\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n" );
53+ for (i = 0 ; i < num_processes ; i ++ )
54+ {
55+ printf ("%d\t%d ms\t\t%d\t\t%d ms\t\t%d ms\n" , processes [i ].pid , processes [i ].burst_time , processes [i ].priority , processes [i ].waiting_time , processes [i ].turnaround_time );
56+ }
57+ printf ("\nAverage waiting time: %.2f ms" , (float )total_waiting_time /num_processes );
58+ printf ("\nAverage turnaround time: %.2f ms\n" ,
59+ (float )total_turnaround_time /num_processes );
60+ return 0 ;
61+ }
0 commit comments