NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <stdio.h>
#include <stdlib.h>

#define MAX 100

typedef struct
{
int pid;
int burst_time;
int waiting_time;
int turnaround_time;
} Process;

void print_table(Process p[], int n);
void print_gantt_chart(Process p[], int n);

int main()
{
Process p[MAX];
int i, j, n;
int sum_waiting_time = 0, sum_turnaround_time;
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;
}

// calculate waiting time and turnaround time
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;
}

// calculate sum of waiting time and sum of turnaround time
for(i=0; i<n; i++) {
sum_waiting_time += p[i].waiting_time;
sum_turnaround_time += p[i].turnaround_time;
}

// print table
puts(""); // Empty line
print_table(p, n);
puts(""); // Empty Line
printf("Total Waiting Time : %-2dn", sum_waiting_time);
printf("Average Waiting Time : %-2.2lfn", (double)sum_waiting_time / (double) n);
printf("Total Turnaround Time : %-2dn", sum_turnaround_time);
printf("Average Turnaround Time : %-2.2lfn", (double)sum_turnaround_time / (double) n);

// print Gantt chart
puts(""); // Empty line
puts(" GANTT CHART ");
puts(" *********** ");
print_gantt_chart(p, n);
return 0;
}


void print_table(Process p[], int n)
{
int i;

puts("+-----+------------+--------------+-----------------+");
puts("| PID | Burst Time | Waiting Time | Turnaround Time |");
puts("+-----+------------+--------------+-----------------+");

for(i=0; i<n; i++) {
printf("| %2d | %2d | %2d | %2d |n"
, p[i].pid, p[i].burst_time, p[i].waiting_time, p[i].turnaround_time );
puts("+-----+------------+--------------+-----------------+");
}

}


void print_gantt_chart(Process p[], int n)
{
int i, j;
// print top bar
printf(" ");
for(i=0; i<n; i++) {
for(j=0; j<p[i].burst_time; j++) printf("--");
printf(" ");
}
printf("n|");

// printing process id in the middle
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 ");
// printing bottom bar
for(i=0; i<n; i++) {
for(j=0; j<p[i].burst_time; j++) printf("--");
printf(" ");
}
printf("n");

// printing the time line
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"); // backspace : remove 1 space
printf("%d", p[i].turnaround_time);

}
printf("n");

}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.