fork download
  1. #include <stdio.h>
  2.  
  3. // Structure to represent a process
  4. struct process {
  5. int pid; // Process ID
  6. int arrivaltime; // Arrival time of the process
  7. int bursttime; // Burst time (execution time) of the process
  8. int waitingtime; // Waiting time for the process
  9. int turnaroundtime; // Turnaround time for the process
  10. };
  11.  
  12. // Function to calculate the waiting time for each process
  13. void calculatewaitingtime(struct process p[], int n) {
  14. p[0].waitingtime = 0; // First process has no waiting time
  15. for (int i = 1; i < n; i++) {
  16. p[i].waitingtime = p[i - 1].waitingtime + p[i - 1].bursttime; // Cumulative waiting time
  17. }
  18. }
  19.  
  20. // Function to calculate the turnaround time for each process
  21. void calculateturnaroundtime(struct process p[], int n) {
  22. for (int i = 0; i < n; i++) {
  23. p[i].turnaroundtime = p[i].bursttime + p[i].waitingtime; // Turnaround time = Burst time + Waiting time
  24. }
  25. }
  26.  
  27. // Function to display process details and calculate averages
  28. void displayprocessdetails(struct process p[], int n) {
  29. float totalwaitingtime = 0, totalturnaroundtime = 0;
  30.  
  31. printf("Process\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
  32. for (int i = 0; i < n; i++) {
  33. totalwaitingtime += p[i].waitingtime; // Accumulate total waiting time
  34. totalturnaroundtime += p[i].turnaroundtime; // Accumulate total turnaround time
  35. printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].arrivaltime, p[i].bursttime, p[i].waitingtime, p[i].turnaroundtime);
  36. }
  37.  
  38. printf("\nAverage Waiting Time: %.2f\n", totalwaitingtime / n); // Calculate and print average waiting time
  39. printf("Average Turnaround Time: %.2f\n", totalturnaroundtime / n); // Calculate and print average turnaround time
  40. }
  41.  
  42. int main() {
  43. int n;
  44.  
  45. // Input the number of processes
  46. printf("Enter number of processes: ");
  47. scanf("%d", &n);
  48.  
  49. struct process p[n]; // Array of processes
  50.  
  51. // Input arrival time and burst time for each process
  52. for (int i = 0; i < n; i++) {
  53. p[i].pid = i + 1; // Assign process ID
  54. printf("Enter arrival time and burst time for process P%d: ", i + 1);
  55. scanf("%d %d", &p[i].arrivaltime, &p[i].bursttime);
  56. }
  57.  
  58. // Calculate waiting and turnaround times
  59. calculatewaitingtime(p, n);
  60. calculateturnaroundtime(p, n);
  61.  
  62. // Display the process details and averages
  63. displayprocessdetails(p, n);
  64.  
  65. return 0; // Exit the program
  66. }
  67.  
  68.  
  69.  
Runtime error #stdin #stdout #stderr 0.01s 5260KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
*** stack smashing detected ***: <unknown> terminated