Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Napišite program koji simulira First-Come, First-Served (FCFS) algoritam za raspoređivanje.
- Naredba za kreiranje datoteke: touch zad1.c
- Naredba za pokretanje uređivača teksta: nano zad1.c
- Kod:
- #include<stdio.h>
- #include<stdlib.h>
- void waiting_time(int *process, int n, int *bt, int *wt){
- wt[0] = 0;
- int i;
- for(i = 1; i < n; i++){
- wt[i] = bt[i-1] + wt[i-1];
- }
- }
- void turnaround_time(int *process, int n, int *bt, int *wt, int *tat){
- int i;
- for(i = 0; i < n; i++){
- tat[i] = bt[i] + wt[i];
- }
- }
- void average_time(int *process, int n, int *bt){
- int wt[n], tat[n], total_wt = 0, total_tat = 0;
- waiting_time(process, n, bt, wt);
- turnaround_time(process, n, bt, wt, tat);
- printf("Process Burst Waiting Turnaround\n ");
- int i;
- for(i = 0; i < n; i++){
- total_wt = total_wt + wt[i];
- total_tat = total_tat + tat[i];
- printf("%d", (i+1));
- printf("\t %d", bt[i]);
- printf("\t %d", wt[i]);
- printf("\t %d\n", tat[i]);
- }
- int s = (float)total_wt / (float)n;
- int t = (float)total_tat / (float)n;
- printf("Average waiting time = %d\n", s);
- printf("Average turnaround time = %d\n", t);
- }
- int main(){
- int process[] = {1, 2, 3, 4};
- int n = sizeof process / sizeof process[0];
- int burst_time[] = {2, 1, 5, 8};
- average_time(process, n, burst_time);
- return 0;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////
- Naredba za prevođenje datoteke: gcc zad1.c -o zad1
- Naredba za pokretanje datoteke: ./zad1
- Izlaz:
- Process Burst Waiting Turnaround
- 1 2 0 2
- 2 1 2 3
- 3 5 3 8
- 4 8 8 16
- Average waiting time = 3
- Average turnaround time = 7
- ////////////////////////////////////////////////////////////////////////////////////////
- 2. Napišite program koji simulira Shortest Job First (SJF) algoritam za raspoređivanje.
- Naredba za kreiranje datoteke: touch zad2.c
- Naredba za pokretanje uređivača teksta: nano zad2.c
- Kod:
- #include<stdio.h>
- #include<stdlib.h>
- void main(){
- int bt[20], wt[20], tat[20], p[20];
- int i, j, n, pos, temp;
- int total = 0;
- float avg_wt, avg_tat;
- printf("Enter number of process:");
- scanf("%d", &n);
- printf("\nEnter Burst time:\n");
- for(i = 0; i < n; i++){
- printf("p%d: ", i+1);
- scanf("%d", &bt[i]);
- p[i]=i +1;
- }
- for(i = 0; i < n; i++){
- pos = i;
- for(j=i+1;j<n;j++){
- if(bt[j]<bt[pos])
- pos=j;
- }
- temp=bt[i];
- bt[i]=bt[pos];
- bt[pos]=temp;
- temp=p[i];
- p[i]=p[pos];
- }
- wt[0]=0;
- for(i=1;i<n;i++){
- wt[i]=0;
- for(j=0;j<i;j++)
- wt[i]+=bt[j];
- total+=wt[i];
- }
- avg_wt=(float)total/n;
- total=0;
- printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
- for(i=0;i<n;i++){
- tat[i]=bt[i]+wt[i];
- total+=tat[i];
- printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i$
- }
- avg_tat=(float)total/n;
- printf("\n\nAverage Waiting Time=%f",avg_wt);
- printf("\nAverage Turnaround Time=%f\n",avg_tat);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- Naredba za prevođenje datoteke: gcc zad2.c -o zad2
- Naredba za pokretanje datoteke: ./zad2
- Izlaz:
- Enter number of process:5
- Enter Burst time:
- p1: 3
- p2: 2
- p3: 5
- p4: 1
- p5: 7
- Process Burst Time Waiting Time Turnaround Time
- p4 1 0 1
- p2 2 1 3
- p1 3 3 6
- p3 5 6 11
- p5 7 11 18
- Average Waiting Time=4.200000
- Average Turnaround Time=7.800000
- //////////////////////////////////////////////////////////////////////////////////////////////
- 3. Napišite program koji simulira Round Robin (RR) algoritam za raspoređivanje.
- Naredba za kreiranje datoteke: touch zad3.c
- Naredba za pokretanje uređivača teksta: nano zad3.c
- Kod:
- #include<stdio.h>
- #include<stdlib.h>
- int main(){
- int count,j,n,time,remain,flag=0,time_quantum;
- int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
- printf("Enter Total Process:\t ");
- scanf("%d",&n);
- remain=n;
- for(count=0;count<n;count++){
- printf("Enter Arrival Time and Burst Time for Process Process Number %d :",$%d :",count+1);
- scanf("%d",&at[count]);
- scanf("%d",&bt[count]);
- rt[count]=bt[count];
- }
- printf("Enter Time Quantum:\t");
- scanf("%d",&time_quantum);
- printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
- for(time=0,count=0;remain!=0;){
- if(rt[count]<=time_quantum && rt[count]>0){
- time+=rt[count];
- rt[count]=0;
- flag=1;
- }
- else if(rt[count]>0){
- rt[count]-=time_quantum;
- time+=time_quantum;
- }
- if(rt[count]==0 && flag==1){
- remain--;
- printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
- wait_time+=time-at[count]-bt[count];
- turnaround_time+=time-at[count];
- flag=0;
- }
- if(count==n-1)
- count=0;
- else if(at[count+1]<=time)
- count++;
- else
- count=0;
- }
- printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
- printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
- return 0;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- Naredba za prevođenje datoteke: gcc zad3.c -o zad3
- Naredba za pokretanje datoteke: ./zad3
- Izlaz:
- student61@linux:~/lv5$ ./zad3
- Enter Total Process: 4
- Enter Arrival Time and Burst Time for Process Process Number 1 :3 4
- Enter Arrival Time and Burst Time for Process Process Number 2 :1 6
- Enter Arrival Time and Burst Time for Process Process Number 3 :3 5
- Enter Arrival Time and Burst Time for Process Process Number 4 :2 6
- Enter Time Quantum: 3
- Process |Turnaround Time|Waiting Time
- P[1] | 10 | 6
- P[2] | 15 | 9
- P[3] | 15 | 10
- P[4] | 19 | 13
- Average Waiting Time= 9.500000
- Avg Turnaround Time = 14.750000student61@linux:~/lv5$
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Add Comment
Please, Sign In to add comment