amarek

OS LV5

Nov 22nd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.96 KB | None | 0 0
  1. 1. Napišite program koji simulira First-Come, First-Served (FCFS) algoritam za raspoređivanje.
  2.  
  3. Naredba za kreiranje datoteke: touch zad1.c
  4. Naredba za pokretanje uređivača teksta: nano zad1.c
  5. Kod:
  6.  
  7. #include<stdio.h>
  8. #include<stdlib.h>
  9.  
  10. void waiting_time(int *process, int n, int *bt, int *wt){
  11.         wt[0] = 0;
  12.         int i;
  13.         for(i = 1; i < n; i++){
  14.                 wt[i] = bt[i-1] + wt[i-1];
  15.         }
  16. }
  17.  
  18. void turnaround_time(int *process, int n, int *bt, int *wt, int *tat){
  19.         int i;
  20.         for(i = 0; i < n; i++){
  21.                 tat[i] = bt[i] + wt[i];
  22.         }
  23. }
  24.  
  25. void average_time(int *process, int n, int *bt){
  26.         int wt[n], tat[n], total_wt = 0, total_tat = 0;
  27.  
  28.         waiting_time(process, n, bt, wt);
  29.         turnaround_time(process, n, bt, wt, tat);
  30.         printf("Process    Burst    Waiting    Turnaround\n ");
  31.  
  32.         int i;
  33.         for(i = 0; i < n; i++){
  34.                 total_wt = total_wt + wt[i];
  35.                 total_tat = total_tat + tat[i];
  36.                 printf("%d", (i+1));
  37.                 printf("\t      %d", bt[i]);
  38.                 printf("\t      %d", wt[i]);
  39.                 printf("\t           %d\n", tat[i]);
  40.         }
  41.         int s = (float)total_wt / (float)n;
  42.         int t = (float)total_tat / (float)n;
  43.  
  44.         printf("Average waiting time = %d\n", s);
  45.         printf("Average turnaround time = %d\n", t);
  46. }
  47.  
  48. int main(){
  49.         int process[] = {1, 2, 3, 4};
  50.         int n = sizeof process / sizeof process[0];
  51.         int burst_time[] = {2, 1, 5, 8};
  52.         average_time(process, n, burst_time);
  53.         return 0;
  54. }
  55. ///////////////////////////////////////////////////////////////////////////////////////////
  56.  
  57. Naredba za prevođenje datoteke: gcc zad1.c -o zad1
  58. Naredba za pokretanje datoteke: ./zad1
  59. Izlaz:
  60.  
  61. Process    Burst    Waiting    Turnaround
  62.  1            2       0            2
  63. 2             1       2            3
  64. 3             5       3            8
  65. 4             8       8            16
  66. Average waiting time = 3
  67. Average turnaround time = 7
  68. ////////////////////////////////////////////////////////////////////////////////////////
  69.  
  70. 2. Napišite program koji simulira Shortest Job First (SJF) algoritam za raspoređivanje.
  71.  
  72. Naredba za kreiranje datoteke: touch zad2.c
  73. Naredba za pokretanje uređivača teksta: nano zad2.c
  74. Kod:
  75.  
  76. #include<stdio.h>
  77. #include<stdlib.h>
  78.  
  79. void main(){
  80.         int bt[20], wt[20], tat[20], p[20];
  81.         int i, j, n, pos, temp;
  82.         int total = 0;
  83.  
  84.         float avg_wt, avg_tat;
  85.  
  86.         printf("Enter number of process:");
  87.         scanf("%d", &n);
  88.         printf("\nEnter Burst time:\n");
  89.         for(i = 0; i < n; i++){
  90.                 printf("p%d: ", i+1);
  91.                 scanf("%d", &bt[i]);
  92.                 p[i]=i +1;
  93.         }
  94.         for(i = 0; i < n; i++){
  95.                 pos = i;
  96.                
  97.                 for(j=i+1;j<n;j++){
  98.                     if(bt[j]<bt[pos])
  99.                         pos=j;
  100.                 }
  101.  
  102.                 temp=bt[i];
  103.                 bt[i]=bt[pos];
  104.                 bt[pos]=temp;
  105.                 temp=p[i];
  106.                 p[i]=p[pos];
  107.         }
  108.  
  109.         wt[0]=0;
  110.         for(i=1;i<n;i++){
  111.                 wt[i]=0;
  112.                 for(j=0;j<i;j++)
  113.                         wt[i]+=bt[j];
  114.                 total+=wt[i];
  115.         }
  116.         avg_wt=(float)total/n;
  117.         total=0;
  118.         printf("\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time");
  119.         for(i=0;i<n;i++){
  120.                 tat[i]=bt[i]+wt[i];
  121.                 total+=tat[i];
  122.                 printf("\np%d\t\t  %d\t\t    %d\t\t\t%d",p[i],bt[i],wt[i],tat[i$
  123.         }
  124.  
  125.         avg_tat=(float)total/n;
  126.         printf("\n\nAverage Waiting Time=%f",avg_wt);
  127.         printf("\nAverage Turnaround Time=%f\n",avg_tat);
  128. }
  129. ////////////////////////////////////////////////////////////////////////////////////////////////
  130.  
  131. Naredba za prevođenje datoteke: gcc zad2.c -o zad2
  132. Naredba za pokretanje datoteke: ./zad2
  133. Izlaz:
  134. Enter number of process:5
  135.  
  136. Enter Burst time:
  137. p1: 3
  138. p2: 2
  139. p3: 5
  140. p4: 1
  141. p5: 7
  142.  
  143. Process     Burst Time          Waiting Time    Turnaround Time
  144. p4                1                 0                   1
  145. p2                2                 1                   3
  146. p1                3                 3                   6
  147. p3                5                 6                   11
  148. p5                7                 11                  18
  149.  
  150. Average Waiting Time=4.200000
  151. Average Turnaround Time=7.800000
  152. //////////////////////////////////////////////////////////////////////////////////////////////
  153.  
  154. 3. Napišite program koji simulira Round Robin (RR) algoritam za raspoređivanje.
  155. Naredba za kreiranje datoteke: touch zad3.c
  156. Naredba za pokretanje uređivača teksta: nano zad3.c
  157. Kod:
  158.  
  159. #include<stdio.h>
  160. #include<stdlib.h>
  161.  
  162. int main(){
  163.   int count,j,n,time,remain,flag=0,time_quantum;
  164.   int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
  165.   printf("Enter Total Process:\t ");
  166.   scanf("%d",&n);
  167.   remain=n;
  168.  
  169.   for(count=0;count<n;count++){
  170.     printf("Enter Arrival Time and Burst Time for Process Process Number %d :",$%d :",count+1);
  171.  
  172.    scanf("%d",&at[count]);
  173.    scanf("%d",&bt[count]);
  174.    rt[count]=bt[count];
  175.  }
  176.  printf("Enter Time Quantum:\t");
  177.  scanf("%d",&time_quantum);
  178.  printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
  179.  
  180.  for(time=0,count=0;remain!=0;){
  181.    if(rt[count]<=time_quantum && rt[count]>0){
  182.      time+=rt[count];
  183.      rt[count]=0;
  184.      flag=1;
  185.     }
  186.    else if(rt[count]>0){
  187.      rt[count]-=time_quantum;
  188.      time+=time_quantum;
  189.     }
  190.    if(rt[count]==0 && flag==1){
  191.      remain--;
  192.      printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
  193.      wait_time+=time-at[count]-bt[count];
  194.       turnaround_time+=time-at[count];
  195.      flag=0;
  196.    }
  197.    if(count==n-1)
  198.      count=0;
  199.     else if(at[count+1]<=time)
  200.      count++;
  201.    else
  202.      count=0;
  203.  }
  204.  printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
  205.  printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
  206.  
  207.  return 0;
  208. }
  209.  
  210. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  211.  
  212. Naredba za prevođenje datoteke: gcc zad3.c -o zad3
  213. Naredba za pokretanje datoteke: ./zad3
  214. Izlaz:
  215.  
  216. student61@linux:~/lv5$ ./zad3
  217. Enter Total Process:     4
  218. Enter Arrival Time and Burst Time for Process Process Number 1 :3 4
  219. Enter Arrival Time and Burst Time for Process Process Number 2 :1 6
  220. Enter Arrival Time and Burst Time for Process Process Number 3 :3 5
  221. Enter Arrival Time and Burst Time for Process Process Number 4 :2 6
  222. Enter Time Quantum:     3
  223.  
  224.  
  225. Process |Turnaround Time|Waiting Time
  226.  
  227. P[1]    |       10      |       6
  228. P[2]    |       15      |       9
  229. P[3]    |       15      |       10
  230. P[4]    |       19      |       13
  231.  
  232. Average Waiting Time= 9.500000
  233. Avg Turnaround Time = 14.750000student61@linux:~/lv5$
  234. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Add Comment
Please, Sign In to add comment