NightRaven97

FCFS

Jan 30th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. #include<stdio.h>
  2. struct process
  3. {
  4.         char pname[30];
  5.         int btime,wtime,tatime;
  6. }arr[50];
  7. int main()
  8. {
  9.         int n,i,ttatime=0,twtime=0,index;
  10.         printf("Enter the number of processes:");
  11.         scanf("%d",&n);
  12.         for(i=1;i<=n;i++)
  13.         {
  14.                 index=i-1;
  15.                 printf("Enter process #%d name:",i);
  16.                 scanf("%s",&arr[index].pname);
  17.                 printf("Enter burst time:");
  18.                 scanf("%d",&arr[index].btime);
  19.                 if(i==1)
  20.                 {
  21.                         arr[index].wtime=0;
  22.                         arr[index].tatime=arr[index].btime;
  23.                 }
  24.                 else
  25.                 {
  26.                         arr[index].wtime=arr[index-1].tatime;
  27.                         arr[index].tatime=arr[index].wtime+arr[index].btime;
  28.                 }
  29.                 ttatime+=arr[index].tatime;
  30.                 twtime+=arr[index].wtime;
  31.         }
  32.         for(i=1;i<=n;i++)
  33.         {
  34.                 index=i-1;
  35.                 printf("PROCESS NAME:%s\nBURST TIME:%d\nWAITING TIME:%d\nTURNAROUND TIME:%d\n\n",arr[index].pname,arr[index].btime,arr[index].wtime,arr[index].tatime);
  36.         }
  37.         printf("Average waiting time:%f\nAverage turnaround time:%f\n",(float)twtime/n,(float)ttatime/n);
  38.         return 0;
  39. }
  40. /*
  41. Output:
  42. [cse2a42@localhost ~]$ cc fcfs.c
  43. [cse2a42@localhost ~]$ ./a.out
  44. Enter the number of processes:3
  45. Enter process #1 name:A
  46. Enter burst time:5
  47. Enter process #2 name:B
  48. Enter burst time:10
  49. Enter process #3 name:C
  50. Enter burst time:15
  51. PROCESS NAME:A
  52. BURST TIME:5
  53. WAITING TIME:0
  54. TURNAROUND TIME:5
  55.  
  56. PROCESS NAME:B
  57. BURST TIME:10
  58. WAITING TIME:5
  59. TURNAROUND TIME:15
  60.  
  61. PROCESS NAME:C
  62. BURST TIME:15
  63. WAITING TIME:15
  64. TURNAROUND TIME:30
  65.  
  66. Average waiting time:6.666667
  67. Average turnaround time:16.666667
  68.  
  69.  
  70. */
Add Comment
Please, Sign In to add comment