Question

C language show all work

1. [50 pts] Given the following set of processes, with arrival times, priorities, and the length of the CPU burst in ms: Priority Arrival Time Burst time Process P1 P2 P3 P4 4. 3 2 0 0 10 (Note: lower number means higher priority, processes P1, P2, P3, and P4 arrive at the same time, in the given order). a. Draw a Gantt chart showing a FCFS scheduling algorithm. b. Draw a Gantt chart showing a Priority (non-preemptive) scheduling algorithm. c. Draw a Gantt chart showing a SJF (non-preemptive) scheduling algorithm. d. Draw a Gantt chart showing a Round Robin Scheduling algorithm with quantum of 2ms

0 0
Add a comment Improve this question Transcribed image text
Answer #1

fcfs:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 100
typedef struct
{
    int pid;
    int burst_time;
    int waiting_time;
    int turnaround_time;
} Process;
void print_gantt_chart(Process p[], int n);
int main()
{
    Process p[MAX];
    int i, j, n;
     int sum_turnaround_time;
     clrscr();
    printf("Enter total number of process: ");
    scanf("%d", &n);
    printf("Enter burst time for each process:\n");
    for(i=0; i<n; i++)
    {
p[i].pid = i+1;
printf("P[%d] : ", i+1);
scanf("%d", &p[i].burst_time);
p[i].waiting_time = p[i].turnaround_time = 0;
      }
p[0].turnaround_time = p[0].burst_time;
    for(i=1; i<n; i++)
   {
p[i].waiting_time = p[i-1].waiting_time + p[i-1].burst_time;
p[i].turnaround_time = p[i].waiting_time + p[i].burst_time;
    }
    puts(""); // Empty line
    puts("          GANTT CHART          ");
    puts("          ***********          ");
    print_gantt_chart(p, n);
    getch();
    return 0;
}
void print_gantt_chart(Process p[], int n)
{
    int i, j;
     printf(" ");
    for(i=0; i<n; i++) {
        for(j=0; j<p[i].burst_time; j++) printf("--");
        printf(" ");
    }
    printf("\n|");
   
    for(i=0; i<n; i++) {
        for(j=0; j<p[i].burst_time - 1; j++) printf(" ");
        printf("P%d", p[i].pid);
        for(j=0; j<p[i].burst_time - 1; j++) printf(" ");
        printf("|");
    }
    printf("\n ");
   
    for(i=0; i<n; i++) {
        for(j=0; j<p[i].burst_time; j++) printf("--");
        printf(" ");
    }
    printf("\n");
  
    printf("0");
    for(i=0; i<n; i++) {
        for(j=0; j<p[i].burst_time; j++) printf(" ");
        if(p[i].turnaround_time > 9) printf("\b");
        printf("%d", p[i].turnaround_time);
    }
    printf("\n");
}

Enter total number of process 4 Enter burst time for each process: PL11: 4 PI21 1 PI31: 2 P[41: 10 GANTT CHART P4 7 17

sjf:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX_PROCESS 100
struct process {
    int pid;
    int burst_time;
    int waiting_time;
};
typedef struct process Process;
double average_waiting_time;
int total_waiting_time;
void sort_process_by_burst_time(Process p[], int n);
void calculate_waiting_time(Process p[], int n);
void print_gantt_chart(Process p[], int n);
int main()
{
    Process p[MAX_PROCESS];
    int n, i, j;
    clrscr();
    puts("SHORTEST JOB FIRST SCHEDULING ALGORITHM");
    puts("=======================================");
    printf("Enter total process: ");
    scanf("%d", &n);
    printf("Enter burst time for each process:\n");
    for(i=0; i<n; i++) {
printf("P[%d]: ", i+1);
scanf("%d", &p[i].burst_time);
p[i].pid = i+1;
    }
    sort_process_by_burst_time(p, n);
    calculate_waiting_time(p, n);
    average_waiting_time = (double) ( (double)total_waiting_time / (double) n );
    puts("");
    printf("Gantt Chart:\n");
    print_gantt_chart(p, n);
    getch();
    return 0;
}
void sort_process_by_burst_time(Process p[], int n)
{
    int i, j;
    Process temp;
    for(i=0; i<n-1; i++) {
        for(j=0; j<n-1-i; j++) {
            if(p[j].burst_time > p[j+1].burst_time) {
                temp = p[j];
                p[j] = p[j+1];
                p[j+1] = temp;
            }
        }
    }
}
void calculate_waiting_time(Process p[], int n)
{
    int i;
    total_waiting_time = 0;
    p[0].waiting_time = 0;
    for(i=1; i<n; i++) {
        p[i].waiting_time = p[i-1].waiting_time + p[i-1].burst_time;
        total_waiting_time += p[i].waiting_time;
    }
}
void print_gantt_chart(Process p[], int n)
{
    int i, j;
    int last = p[n-1].burst_time + ( n== 1 ? 0 : p[n-1].waiting_time);
    printf(" ");
    for(i=0; i<n; i++) {
        for(j=0; j<p[i].burst_time; j++) printf("--");
        printf(" ");
    }
    printf("\n|");
    for(i=0; i<n; i++) {
        for(j=0; j<p[i].burst_time-1; j++) printf(" ");
        printf("p%d", p[i].pid);
        for(j=0; j<p[i].burst_time-1; j++) printf(" ");
        printf("|");
    }
    printf("\n ");
    for(i=0; i<n; i++) {
        for(j=0; j<p[i].burst_time; j++) printf("--");
        printf(" ");
    }
    printf("\n");
    int minus = 0;
    for(i=0; i<n; i++) {
        if(p[i].waiting_time>9) printf(" ");
        printf("%d", p[i].waiting_time);
        if(p[i+1].waiting_time>9){
          minus = 1;
        }
        if(i+1 == n ) if (last>9) minus = 1;
        for(j=0; j<p[i].burst_time-minus; j++) printf(" ");
    }
    if(last>9) printf(" ");
    printf("%d\n", last);
}


SHORTEST JOB FIRST SCHEDUL ING ALGORITHM Enter totalprocess: Enter burst time for each process: PL11: 4 PI21: 1 P131: 2 PI41:round robin:

#include<stdio.h>
#include<conio.h>
struct process
{
int at,bt,wt,tat,st,ft,flag,id,tbt;
}p[10],temp;
int n,t,save_et[10],save_id[10],turn,btsum;
float awt,atat;
void read();
void print();
void rndrbn();
void fifoq();
main()
{
int ch;
do
{
printf("\nEnter choice:1.RR 3.exit.ENTER UR CHOICE\n");
scanf("%d",&ch);
switch(ch)
{
case 1:read();
fifoq();
rndrbn();
print();
break;
case 3:
return 0;
default:
printf("invalid choice");
}
}while(1);
}
void read()
{
int i;
printf("Enter no of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter burst time of process p%d : ",(i+1));
scanf("%d%",&p[i].bt);
p[i].id=i+1;
p[i].wt=p[i].flag=0;
p[i].tbt=p[i].bt;
btsum+=p[i].bt;
}
printf("Enter time quantum : ");
scanf("%d",&t);
}
void fifoq()
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(p[j].at>p[j+1].at)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
}
void rndrbn()
{
//int tmp;
int cnt=n;
int i=0;
int et=0;
int sum=0;
float twt=0;
float ttat=0;
while(cnt!=0)
{
if((p[i].bt)>t)
{
et=t;
p[i].bt-=t;
}
else
{
et=p[i].bt;
p[i].bt=0;
}
p[i].st=sum;
if((p[i].flag)==0)
{
p[i].wt=p[i].st-p[i].at;
p[i].flag++;
}
else
p[i].wt=p[i].wt+(p[i].st-p[i].ft);
sum=sum+et;
p[i].ft=sum;
save_et[turn]=et;
save_id[turn++]=p[i].id;
if((p[i].bt)==0)
{
cnt--;
}
do
{
i=(i+1)%n;
}while((p[i].bt)==0 && cnt!=0);
}
for(i=0;i<n;i++)
{
p[i].tat=p[i].wt+p[i].tbt;
twt+=p[i].wt;
ttat+=p[i].tat;
}
awt=twt/n;
atat=ttat/n;
}
void print()
{
int i;
for(i=0;i<=btsum;i++)
printf("-");
printf("|");
for(i=0;i<turn;i++)
printf("%*d|",-(save_et[i]-1),save_id[i]);
printf("");
for(i=0;i<=btsum;i++)
printf("-");
printf("");
btsum=0;
}

Enter choice:1.RR 3.exit.ENTER UR CHOICE Enter no of processes:4 Enter burst time of process p1 Enter burst time of process 2

Add a comment
Know the answer?
Add Answer to:
C language show all work 1. [50 pts] Given the following set of processes, with arrival...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Opearting system 5. [27pts.] Given the following set of processes, with arrival times, priorities, and the...

    Opearting system 5. [27pts.] Given the following set of processes, with arrival times, priorities, and the length of the CPU burst in ms: Process Arrival Time Burst time Priority P1 P2 P3 2 10 P4 (Note: a lower number means higher priority; processes P2- P4 arrive at the same time, in the given order) a. Draw a Gantt chart showing a FCFS scheduling algorithm. b. Draw a Gantt chart showing a non-preemptive Priority scheduling algorithm. Draw a Gantt chart showing...

  • 5. [27pts.] Given the following set of processes, with arrival times, priorities, and the length of...

    5. [27pts.] Given the following set of processes, with arrival times, priorities, and the length of the CPU burst in ms: Priority Burst time Arrival Time Process 3 P1 4 P2 2 10 P3 P4 (Note: a lower number means higher priority) Draw a Gantt chart showing a FCFS scheduling algorithm. a. b. Draw a Gantt chart showing a non-preemptive Priority scheduling algorithm. c. Draw a Gantt chart showing a Round Robin Scheduling algorithm with quantum of 4ms d. Compute...

  • Please answer the following question in C++ language Consider the following set of processes, with the...

    Please answer the following question in C++ language Consider the following set of processes, with the length of the CPU burst time given in milliseconds: Process            Burst Time      Priority P1. 7 5 P2 2 4 P3 11 3 P4 9 1 P5 5 3 The processes are assumed to have arrived in the order P1,P2, P3, P4, P5, all at time 0. a. Draw four Gantt charts that illustrate the execution of these processes using the following scheduling algorithms: FCFS, SJF, nonpreemptive...

  • Consider the following set of processes, with the length of the CPU-burst time given in milliseconds:

    Consider the following set of processes, with the length of the CPU-burst time given in milliseconds:Processburst TimePriorityP1103P211P323P414P552For each of the scheduling algorithms, FCFS, Shortest-Job-First (SJF, non-preemptive), Priority (smaller priority number implies higher scheduling priority), and RR (quantum = 1) do the following.Draw a Gantt chart to show how these processes would be scheduled.Give the turnaround time (total time from the first arrival into ready state until CPU-burst is completed) of each process.Give the waiting time (total time spent in the Ready state) of each process.Give...

  • Consider the following set of processes, with the length of CPU burst in milliseconds.

    Consider the following set of processes, with the length of CPU burst in milliseconds. Process PI P2 P3 P4 P5 Arrival time 00 02 03 06 30 Burst time 10 12 14 16 05 Draw a Gantt chart that illustrates the execution of these processes using the preemptive shorte st job first (SJF) algorithm. Hence find the average waiting time. Draw a Gantt chart that illustrate the execution of these processes using preemptive priority scheduling algorithm. Given priority of each...

  • Cpu scheduling

    Consider the following set of processes, with the length of the CPU burst times given in milliseconds:a. Draw four Gantt charts illustrating the execution of the processes using FCFS, Preemptive SJF, a non-preemptive priority, and a RR (quantum=2) scheduling. (30 pts)Note: for the RR consider that the arriving time is 0 for all processesb. What is the average waiting time of each process for of the above scheduling algorithms? (10 pts)P1 8 2 0 P2 5 36P3 1 1 8...

  • Consider the following set of processes, with the length of the CPU-burst time given in milliseconds:...

    Consider the following set of processes, with the length of the CPU-burst time given in milliseconds: Process            Burst Time      Priority P1                    10                    3 P2                    1                     1 P3                    2                     3 P4                    1                     4 P5                    5                     2 The processes are assumed to have arrived in the order P1, P2, P3, P4, P5, all at time 0. a. Draw four Gantt charts illustrating the execution of these processes using FCFS, SJF (SPN), a...

  • Given the following set of processes with corresponding execution times (in ms), arrival times and priority...

    Given the following set of processes with corresponding execution times (in ms), arrival times and priority (1 – highest).  For each scheduling algorithm: Construct a table showing which process is active and for how long until all processes are completely serviced (as done in class). Calculate the average waiting time and turnaround time. Process ID Burst (ms) Arrival time P1 9 0 P2 12 0 P3 3 0 P4 30 0 P5 20 0 P6 10 0 First Come First Serve...

  • Consider the following set of processes, with the length of the CPU burst given in milliseconds:

    Consider the following set of processes, with the length of the CPU burst given in milliseconds:ProcessBurst TimePriorityP1 54P231P312P472P543The processes are assumed to have arrived in the order P1 , P2 , P3 , P4 , P5 , all at time 0. a. Draw four Gantt charts that illustrate the execution of these processes using the following scheduling algorithms: FCFS, SJF, nonpreemptive priority (a larger priority number implies a higher priority), and RR (quantum = 2). b. What is the turnaround time of each...

  • QUESTION 1 Consider the following set of processes, with the length of the CPU burst time...

    QUESTION 1 Consider the following set of processes, with the length of the CPU burst time given in milliseconds: Process            Burst Time      Priority P1                    7                      5 P2                    2                      4 P3                    11                    3 P4                    9                      1 P5                    5                      3 The processes are assumed to have arrived in the order P1,P2, P3, P4, P5, all at time 0. a. Draw four Gantt charts that illustrate the execution of these processes using the following scheduling algorithms: FCFS, SJF, nonpreemptive priority (a smaller priority number implies a higher priority), and RR (quantum = 2). b. What...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT