CPU Scheduling - OS Practical Assignment 2022


 CPU Scheduling 

What is CPU Scheduling?


CPU Scheduling is one of the important tasks performed by the operating system. In

multiprogramming operating systems many processes are loaded into memory for execution and

these processes are sharing the CPU and other resources of computer system.

Scheduler is a program that decides which program will execute next at the CPU or which

program will be loaded into memory for execution.

CPU scheduler is a program module of an operating system that selects the process to execute

next at CPU out of the processes that are in memory. This scheduler is also called as Short term

scheduler or CPU Scheduler.


There are 3 types of schedulers as

1) Short Term Scheduler

2) Long Term Scheduler

3) Medium Term Scheduler


Assignment 3 - CPU Scheduling


SET A


1) Write the program to simulate FCFS CPU-scheduling. The arrival time and first CPU-

burst for different n number of processes should be input to the algorithm. Assume that

the fixed IO waiting time (2 units). The next CPU-burst should be generated randomly.

The output should give Gantt chart, turnaround time and waiting time for each process.

Also find the average waiting time and turnaround time.


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

typedef struct process_info
{
 char pname[20];
 int at,bt,ct,bt1;
 struct process_info *next;
}NODE;

int n;
NODE *first,*last;

void accept_info()
{
 NODE *p;
 int i;

 printf("Enter no.of process:");
 scanf("%d",&n);

 for(i=0;i<n;i++)
 {
  p = (NODE*)malloc(sizeof(NODE));

  printf("Enter process name:");
  scanf("%s",p->pname);

  printf("Enter arrival time:");
  scanf("%d",&p->at);

  printf("Enter first CPU burst time:");
  scanf("%d",&p->bt);

  p->bt1 = p->bt;
  
  p->next = NULL;

  if(first==NULL)
   first=p;
  else
   last->next=p;

  last = p;
 }
}

void print_output()
{
 NODE *p;
 float avg_tat=0,avg_wt=0;

 printf("pname\tat\tbt\tct\ttat\twt\n");

 p = first;
 while(p!=NULL)
 {
  int tat = p->ct-p->at;
  int wt = tat-p->bt;
  
  avg_tat+=tat;
  avg_wt+=wt;

  printf("%s\t%d\t%d\t%d\t%d\t%d\n",
   p->pname,p->at,p->bt,p->ct,tat,wt);
  
  p=p->next;
 }

 printf("Avg TAT=%f\tAvg WT=%f\n",
   avg_tat/n,avg_wt/n);
}

void print_input()
{
 NODE *p;

 p = first;
 
 printf("pname\tat\tbt\n");
 while(p!=NULL)
 {
  printf("%s\t%d\t%d\n",
   p->pname,p->at,p->bt1);
  p = p->next;
 }
}

void sort()
{
 NODE *p,*q;
 int t;
 char name[20];

 p = first;
 while(p->next!=NULL)
 {
  q=p->next;
  while(q!=NULL)
  {
   if(p->at > q->at)
   {
    strcpy(name,p->pname);
    strcpy(p->pname,q->pname);
    strcpy(q->pname,name);

    t = p->at;
    p->at = q->at;
    q->at = t;
    
    t = p->bt;
    p->bt = q->bt;
    q->bt = t;

    t = p->ct;
    p->ct = q->ct;
    q->ct = t;

    t = p->bt1;
    p->bt1 = q->bt1;
    q->bt1 = t;
   }

   q=q->next;
  }
 
  p=p->next;
 }
}

int time;

NODE * get_fcfs()
{
 NODE *p;

 p = first;
 while(p!=NULL)
 {
  if(p->at<=time && p->bt1!=0)
   return p;

  p=p->next;
 }

 return NULL;
}

struct gantt_chart
{
 int start;
 char pname[30];
 int end;
}s[100],s1[100];

int k;

void fcfs()
{
 int prev=0,n1=0;
 NODE *p;

 while(n1!=n)
 {
  p = get_fcfs();

  if(p==NULL)
  {
   time++;
   s[k].start = prev;
   strcpy(s[k].pname,"*");
   s[k].end = time;

   prev = time;
   k++;
  }
  else
  {
   time+=p->bt1;
   s[k].start = prev;
   strcpy(s[k].pname, p->pname);
   s[k].end = time;

   prev = time;
   k++;

   p->ct = time;
   p->bt1 = 0;

   n1++;
  }

  print_input(); 
  sort();
 }
}

void print_gantt_chart()
{
 int i,j,m;

 s1[0] = s[0];
 
 for(i=1,j=0;i<k;i++)
 {
  if(strcmp(s[i].pname,s1[j].pname)==0)
   s1[j].end = s[i].end;
  else
   s1[++j] = s[i];
 }

 printf("%d",s1[0].start);
 for(i=0;i<=j;i++)
 {
  m = (s1[i].end - s1[i].start);

  for(k=0;k<m/2;k++)
   printf("-");

  printf("%s",s1[i].pname);

  for(k=0;k<(m+1)/2;k++)
   printf("-");

  printf("%d",s1[i].end);
 }
}

int main()
{
 accept_info();
 sort();
 fcfs();
 print_output();
 print_gantt_chart();

 return 0;
}

Output:


Enter no.of process:5
Enter process name:p1
Enter arrival time:3
Enter first CPU burst time:4
Enter process name:p2
Enter arrival time:5
Enter first CPU burst time:3
Enter process name:p3
Enter arrival time:0
Enter first CPU burst time:2
Enter process name:p4
Enter arrival time:5
Enter first CPU burst time:1
Enter process name:p5
Enter arrival time:4
Enter first CPU burst time:3
pname	at	bt
p3	0	0
p1	3	4
p5	4	3
p4	5	1
p2	5	3
pname	at	bt
p3	0	0
p1	3	4
p5	4	3
p4	5	1
p2	5	3
pname	at	bt
p3	0	0
p1	3	0
p5	4	3
p4	5	1
p2	5	3
pname	at	bt
p3	0	0
p1	3	0
p5	4	0
p4	5	1
p2	5	3
pname	at	bt
p3	0	0
p1	3	0
p5	4	0
p4	5	0
p2	5	3
pname	at	bt
p3	0	0
p1	3	0
p5	4	0
p4	5	0
p2	5	0
pname	at	bt	ct	tat	wt
p3	0	2	2	2	0
p1	3	4	7	4	0
p5	4	3	10	6	3
p4	5	1	11	6	5
p2	5	3	14	9	6

Avg TAT=5.400000	Avg WT=2.800000

0-p3-2*-3--p1--7-p5--10p4-11-p2--14

Note : Modify this program if your output is coming different.

Explaination :

To calculate the average waiting time using the FCFS algorithm first the waiting time of the first
process is kept zero and the waiting time of the second process is the burst time of the first
process and the waiting time of the third process is the sum of the burst times of the first and the
second process and so on. After calculating all the waiting times the average waiting time is
calculated as the average of all the waiting times. FCFS mainly said that first come first serve the
algorithm which came first will be served first.

First Come First Serve Scheduling (FCFS) : In this algorithm the order in which process enters the ready queue, in same order they will execute on the CPU. This algorithm is simple to implement but it may be worst for several times.

ALGORITHM:

Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process name and the burst time

wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}

Step 4: Set the waiting of the first process as  ̳ 0‘and its burst time as its turnaround time

Step 5: for each process in the Ready Q calculate
a) Waiting time (n) = waiting time (n-1) + Burst time (n-1)
b) Turnaround time (n) = waiting time (n) +Burst time (n)

Step 6: Calculate
a) Average waiting time = Total waiting Time / Number of process
b) Average Turnaround time = Total Turnaround Time / Number of process

Step 7: Stop the process



2) Write the program to simulate Non-preemptive Shortest Job First (SJF) -scheduling. The
arrival time and first CPU-burst for different n number of processes should be input to the
algorithm. Assume the fixed IO waiting time (2 units). The next CPU-burst should be
generated randomly. The output should give Gantt chart, turnaround time and waiting
time for each process. Also find the average waiting time and turnaround time.



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

typedef struct process_info
{
 char pname[20];
 int at,bt,ct,bt1;
 struct process_info *next;
}NODE;

int n;
NODE *first,*last;

void accept_info()
{
 NODE *p;
 int i;

 printf("Enter no.of process:");
 scanf("%d",&n);

 for(i=0;i<n;i++)
 {
  p = (NODE*)malloc(sizeof(NODE));

  printf("Enter process name:");
  scanf("%s",p->pname);

  printf("Enter arrival time:");
  scanf("%d",&p->at);

  printf("Enter first CPU burst time:");
  scanf("%d",&p->bt);

  
  p->bt1 = p->bt;
  p->next = NULL;

  if(first==NULL)
   first=p;
  else
   last->next=p;

  last = p;
 }
}

void print_output()
{
 NODE *p;
 float avg_tat=0,avg_wt=0;

 printf("pname\tat\tbt\tct\ttat\twt\n");

 p = first;
 while(p!=NULL)
 {
  int tat = p->ct-p->at;
  int wt = tat-p->bt;
  
  avg_tat+=tat;
  avg_wt+=wt;

  printf("%s\t%d\t%d\t%d\t%d\t%d\n",
   p->pname,p->at,p->bt,p->ct,tat,wt);
  
  p=p->next;
 }

 printf("Avg TAT=%f\tAvg WT=%f\n",
   avg_tat/n,avg_wt/n);
}

void print_input()
{
 NODE *p;

 p = first;
 
 printf("pname\tat\tbt\n");
 while(p!=NULL)
 {
  printf("%s\t%d\t%d\n",
   p->pname,p->at,p->bt1);
  p = p->next;
 }
}

void sort()
{
 NODE *p,*q;
 int t;
 char name[20];

 p = first;
 while(p->next!=NULL)
 {
  q=p->next;
  while(q!=NULL)
  {
   if(p->at > q->at)
   {
    strcpy(name,p->pname);
    strcpy(p->pname,q->pname);
    strcpy(q->pname,name);

    t = p->at;
    p->at = q->at;
    q->at = t;
    
    t = p->bt;
    p->bt = q->bt;
    q->bt = t;

    t = p->ct;
    p->ct = q->ct;
    q->ct = t;

    t = p->bt1;
    p->bt1 = q->bt1;
    q->bt1 = t;
   
   }

   q=q->next;
  }
 
  p=p->next;
 }
}

int time;

NODE * get_sjf()
{
 NODE *p,*min_p=NULL;
 int min=9999;

 p = first;
 while(p!=NULL)
 {
  if(p->at<=time && p->bt1!=0 &&
   p->bt1<min)
  {
   min = p->bt1;
   min_p = p;
  }
  p=p->next;
 }

 return min_p;
}

struct gantt_chart
{
 int start;
 char pname[30];
 int end;
}s[100],s1[100];

int k;

void sjfnp()
{
 int prev=0,n1=0;
 NODE *p;

 while(n1!=n)
 {
  p = get_sjf();

  if(p==NULL)
  {
   time++;
   s[k].start = prev;
   strcpy(s[k].pname,"*");
   s[k].end = time;

   prev = time;
   k++;
  }
  else
  {
   time+=p->bt1;
   s[k].start = prev;
   strcpy(s[k].pname, p->pname);
   s[k].end = time;

   prev = time;
   k++;

   p->ct = time;
   p->bt1 = 0;

   n1++;
  }

  print_input(); 
  sort();
 }
}

void print_gantt_chart()
{
 int i,j,m;

 s1[0] = s[0];
 
 for(i=1,j=0;i<k;i++)
 {
  if(strcmp(s[i].pname,s1[j].pname)==0)
   s1[j].end = s[i].end;
  else
   s1[++j] = s[i];
 }

 printf("%d",s1[0].start);
 for(i=0;i<=j;i++)
 {
  m = (s1[i].end - s1[i].start);

  for(k=0;k<m/2;k++)
   printf("-");

  printf("%s",s1[i].pname);

  for(k=0;k<(m+1)/2;k++)
   printf("-");

  printf("%d",s1[i].end);
 }
}

int main()
{
 accept_info();
 sort();
 sjfnp();
 print_output();
 print_gantt_chart();

 return 0;
}

Output:


Enter no.of process:4
Enter process name:p1
Enter arrival time:1
Enter first CPU burst time:5
Enter process name:p2
Enter arrival time:0
Enter first CPU burst time:7
Enter process name:p3
Enter arrival time:3
Enter first CPU burst time:3
Enter process name:p4
Enter arrival time:2
Enter first CPU burst time:10
pname	at	bt
p2	0	0
p1	1	5
p4	2	10
p3	3	3
pname	at	bt
p2	0	0
p1	1	5
p4	2	10
p3	3	0
pname	at	bt
p2	0	0
p1	1	0
p4	2	10
p3	3	0
pname	at	bt
p2	0	0
p1	1	0
p4	2	0
p3	3	0
pname	at	bt	ct	tat	wt
p2	0	7	7	7	0
p1	1	5	15	14	9
p4	2	10	25	23	13
p3	3	3	10	7	4

Avg TAT=12.750000	Avg WT=6.500000

0---p2----7-p3--10--p1---15-----p4-----25

Note : Modify this program if your output is coming different.

Explaination :

To calculate the average waiting time in the shortest job first algorithm the sorting of
the process based on their burst time in ascending order then calculate the waiting time of
each process as the sum of the bursting times of all the process previous or before to that
process.

Shortest Job First (SJF):

In this algorithm the jobs will execute at the CPU according their next CPU-Burst time. The job
in a ready queue which has shortest next CPU burst will execute next at the CPU. This algorithm
can be preemptive or non-preemptive.

ALGORITHM:

Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept the CPU
burst time

wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
}
wtavg = wt[i]/n;
tatavg = tat[i]/n;

Step 4: Start the Ready Q according the shortest Burst time by sorting according to
lowest to highest burst time.

Step 5: Set the waiting time of the first process as  "0"and its turnaround time as its burst
time.

Step 6: Sort the processes names based on their Burt time

Step 7: For each process in the ready queue,
Calculate
a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)
b) Turnaround time (n)= waiting time(n)+Burst time(n)

Step 8: Calculate
c) Average waiting time = Total waiting Time / Number of process
d) Average Turnaround time = Total Turnaround Time /Number of process

Step 9: Stop the process.


SET B

1) Write the program to simulate Preemptive Shortest Job First (SJF) -scheduling. The
arrival time and first CPU-burst for different n number of processes should be input to the
algorithm. Assume the fixed IO waiting time (2 units). The next CPU-burst should be
generated randomly. The output should give Gantt chart, turnaround time and waiting
time for each process. Also find the average waiting time and turnaround time.


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

typedef struct process_info
{
 char pname[20];
 int at,bt,ct,bt1;
 struct process_info *next;
}NODE;

int n;
NODE *first,*last;

void accept_info()
{
 NODE *p;
 int i;

 printf("Enter no.of process:");
 scanf("%d",&n);

 for(i=0;i<n;i++)
 {
  p = (NODE*)malloc(sizeof(NODE));

  printf("Enter process name:");
  scanf("%s",p->pname);

  printf("Enter arrival time:");
  scanf("%d",&p->at);

  printf("Enter first CPU burst time:");
  scanf("%d",&p->bt);

  p->bt1 = p->bt;
  
  p->next = NULL;

  if(first==NULL)
   first=p;
  else
   last->next=p;

  last = p;
 }
}

void print_output()
{
 NODE *p;
 float avg_tat=0,avg_wt=0;

 printf("pname\tat\tbt\tct\ttat\twt\n");

 p = first;
 while(p!=NULL)
 {
  int tat = p->ct-p->at;
  int wt = tat-p->bt;
  
  avg_tat+=tat;
  avg_wt+=wt;

  printf("%s\t%d\t%d\t%d\t%d\t%d\n",
   p->pname,p->at,p->bt,p->ct,tat,wt);
  
  p=p->next;
 }

 printf("Avg TAT=%f\tAvg WT=%f\n",
   avg_tat/n,avg_wt/n);
}

void print_input()
{
 NODE *p;

 p = first;
 
 printf("pname\tat\tbt\n");
 while(p!=NULL)
 {
  printf("%s\t%d\t%d\n",
   p->pname,p->at,p->bt1);
  p = p->next;
 }
}

void sort()
{
 NODE *p,*q;
 int t;
 char name[20];

 p = first;
 while(p->next!=NULL)
 {
  q=p->next;
  while(q!=NULL)
  {
   if(p->at > q->at)
   {
    strcpy(name,p->pname);
    strcpy(p->pname,q->pname);
    strcpy(q->pname,name);

    t = p->at;
    p->at = q->at;
    q->at = t;
    
    t = p->bt;
    p->bt = q->bt;
    q->bt = t;

    t = p->ct;
    p->ct = q->ct;
    q->ct = t;

    t = p->bt1;
    p->bt1 = q->bt1;
    q->bt1 = t;
   }

   q=q->next;
  }
 
  p=p->next;
 }
}

int time;

NODE * get_sjf()
{
 NODE *p,*min_p=NULL;
 int min=9999;

 p = first;
 while(p!=NULL)
 {
  if(p->at<=time && p->bt1!=0 &&
   p->bt1<min)
  {
   min = p->bt1;
   min_p = p;
  }
  p=p->next;
 }

 return min_p;
}

struct gantt_chart
{
 int start;
 char pname[30];
 int end;
}s[100],s1[100];

int k;

void sjfp()
{
 int prev=0,n1=0;
 NODE *p;

 while(n1!=n)
 {
  p = get_sjf();

  if(p==NULL)
  {
   time++;
   s[k].start = prev;
   strcpy(s[k].pname,"*");
   s[k].end = time;

   prev = time;
   k++;
  }
  else
  {
   time++;
   s[k].start = prev;
   strcpy(s[k].pname, p->pname);
   s[k].end = time;

   prev = time;
   k++;

   p->ct = time;
   p->bt1--;

   if(p->bt1==0)
    n1++;
  }

  print_input(); 
  sort();
 }
}

void print_gantt_chart()
{
 int i,j,m;

 s1[0] = s[0];
 
 for(i=1,j=0;i<k;i++)
 {
  if(strcmp(s[i].pname,s1[j].pname)==0)
   s1[j].end = s[i].end;
  else
   s1[++j] = s[i];
 }

 printf("%d",s1[0].start);
 for(i=0;i<=j;i++)
 {
  m = (s1[i].end - s1[i].start);

  for(k=0;k<m/2;k++)
   printf("-");

  printf("%s",s1[i].pname);

  for(k=0;k<(m+1)/2;k++)
   printf("-");

  printf("%d",s1[i].end);
 }
}

int main()
{
 accept_info();
 sort();
 sjfp();
 print_output();
 print_gantt_chart();

 return 0;
}

Output:

Enter no.of process:4
Enter process name:p1
Enter arrival time:1
Enter first CPU burst time:5
Enter process name:p2
Enter arrival time:0
Enter first CPU burst time:7
Enter process name:p3
Enter arrival time:3
Enter first CPU burst time:3
Enter process name:p3
Enter arrival time:2
Enter first CPU burst time:10
pname	at	bt
p2	0	6
p1	1	5
p3	2	10
p3	3	3
pname	at	bt
p2	0	6
p1	1	4
p3	2	10
p3	3	3
pname	at	bt
p2	0	6
p1	1	3
p3	2	10
p3	3	3
pname	at	bt
p2	0	6
p1	1	2
p3	2	10
p3	3	3
pname	at	bt
p2	0	6
p1	1	1
p3	2	10
p3	3	3
pname	at	bt
p2	0	6
p1	1	0
p3	2	10
p3	3	3
pname	at	bt
p2	0	6
p1	1	0
p3	2	10
p3	3	2
pname	at	bt
p2	0	6
p1	1	0
p3	2	10
p3	3	1
pname	at	bt
p2	0	6
p1	1	0
p3	2	10
p3	3	0
pname	at	bt
p2	0	5
p1	1	0
p3	2	10
p3	3	0
pname	at	bt
p2	0	4
p1	1	0
p3	2	10
p3	3	0
pname	at	bt
p2	0	3
p1	1	0
p3	2	10
p3	3	0
pname	at	bt
p2	0	2
p1	1	0
p3	2	10
p3	3	0
pname	at	bt
p2	0	1
p1	1	0
p3	2	10
p3	3	0
pname	at	bt
p2	0	0
p1	1	0
p3	2	10
p3	3	0
pname	at	bt
p2	0	0
p1	1	0
p3	2	9
p3	3	0
pname	at	bt
p2	0	0
p1	1	0
p3	2	8
p3	3	0
pname	at	bt
p2	0	0
p1	1	0
p3	2	7
p3	3	0
pname	at	bt
p2	0	0
p1	1	0
p3	2	6
p3	3	0
pname	at	bt
p2	0	0
p1	1	0
p3	2	5
p3	3	0
pname	at	bt
p2	0	0
p1	1	0
p3	2	4
p3	3	0
pname	at	bt
p2	0	0
p1	1	0
p3	2	3
p3	3	0
pname	at	bt
p2	0	0
p1	1	0
p3	2	2
p3	3	0
pname	at	bt
p2	0	0
p1	1	0
p3	2	1
p3	3	0
pname	at	bt
p2	0	0
p1	1	0
p3	2	0
p3	3	0
pname	at	bt	ct	tat	wt
p2	0	7	15	15	8
p1	1	5	6	5	0
p3	2	10	25	23	13
p3	3	3	9	6	3
Avg TAT=12.250000	Avg WT=6.000000
0p2-1--p1---6-p3--9---p2---15-----p3-----25

Explaination :

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

typedef struct process_info
{
 char pname[20];
 int at,bt,ct,bt1,p;
 struct process_info *next;
}NODE;

int n;
NODE *first,*last;

void accept_info()
{
 NODE *p;
 int i;

 printf("Enter no.of process:");
 scanf("%d",&n);

 for(i=0;i<n;i++)
 {
  p = (NODE*)malloc(sizeof(NODE));

  printf("Enter process name:");
  scanf("%s",p->pname);

  printf("Enter arrival time:");
  scanf("%d",&p->at);

  printf("Enter first CPU burst time:");
  scanf("%d",&p->bt);

  printf("Enter priority:");
  scanf("%d",&p->p);

  p->bt1 = p->bt;
  
  p->next = NULL;

  if(first==NULL)
   first=p;
  else
   last->next=p;

  last = p;
 }
}

void print_output()
{
 NODE *p;
 float avg_tat=0,avg_wt=0;

 printf("pname\tat\tbt\tp\ttct\ttat\twt\n");

 p = first;
 while(p!=NULL)
 {
  int tat = p->ct-p->at;
  int wt = tat-p->bt;
  
  avg_tat+=tat;
  avg_wt+=wt;

  printf("%s\t%d\t%d\t%d\t%d\t%d\t%d\n",
   p->pname,p->at,p->bt,p->p,p->ct,tat,wt);
  
  p=p->next;
 }

 printf("Avg TAT=%f\tAvg WT=%f\n",
   avg_tat/n,avg_wt/n);
}

void print_input()
{
 NODE *p;

 p = first;
 
 printf("pname\tat\tbt\tp\n");
 while(p!=NULL)
 {
  printf("%s\t%d\t%d\t%d\n",
   p->pname,p->at,p->bt1,p->p);
  p = p->next;
 }
}

void sort()
{
 NODE *p,*q;
 int t;
 char name[20];

 p = first;
 while(p->next!=NULL)
 {
  q=p->next;
  while(q!=NULL)
  {
   if(p->at > q->at)
   {
    strcpy(name,p->pname);
    strcpy(p->pname,q->pname);
    strcpy(q->pname,name);

    t = p->at;
    p->at = q->at;
    q->at = t;
    
    t = p->bt;
    p->bt = q->bt;
    q->bt = t;

    t = p->ct;
    p->ct = q->ct;
    q->ct = t;

    t = p->bt1;
    p->bt1 = q->bt1;
    q->bt1 = t;
   
    t = p->p;
    p->p = q->p;
    q->p = t;
   }

   q=q->next;
  }
 
  p=p->next;
 }
}

int time;

NODE * get_p()
{
 NODE *p,*min_p=NULL;
 int min=9999;

 p = first;
 while(p!=NULL)
 {
  if(p->at<=time && p->bt1!=0 &&
   p->p<min)
  {
   min = p->p;
   min_p = p;
  }
  p=p->next;
 }

 return min_p;
}

struct gantt_chart
{
 int start;
 char pname[30];
 int end;
}s[100],s1[100];

int k;

void pnp()
{
 int prev=0,n1=0;
 NODE *p;

 while(n1!=n)
 {
  p = get_p();

  if(p==NULL)
  {
   time++;
   s[k].start = prev;
   strcpy(s[k].pname,"*");
   s[k].end = time;

   prev = time;
   k++;
  }
  else
  {
   time+=p->bt1;
   s[k].start = prev;
   strcpy(s[k].pname, p->pname);
   s[k].end = time;

   prev = time;
   k++;

   p->ct = time;
   p->bt1 = 0;

   n1++;
  }

  print_input(); 
  sort();
 }
}

void print_gantt_chart()
{
 int i,j,m;

 s1[0] = s[0];
 
 for(i=1,j=0;i<k;i++)
 {
  if(strcmp(s[i].pname,s1[j].pname)==0)
   s1[j].end = s[i].end;
  else
   s1[++j] = s[i];
 }

 printf("%d",s1[0].start);
 for(i=0;i<=j;i++)
 {
  m = (s1[i].end - s1[i].start);

  for(k=0;k<m/2;k++)
   printf("-");

  printf("%s",s1[i].pname);

  for(k=0;k<(m+1)/2;k++)
   printf("-");

  printf("%d",s1[i].end);
 }
}

int main()
{
 accept_info();
 sort();
 pnp();
 print_output();
 print_gantt_chart();

 return 0;
}

Output :


Enter no.of process:4
Enter process name:p1
Enter arrival time:0
Enter first CPU burst time:8
Enter priority:4
Enter process name:p2
Enter arrival time:1
Enter first CPU burst time:6
Enter priority:6
Enter process name:p3
Enter arrival time:3
Enter first CPU burst time:7
Enter priority:3
Enter process name:p4
Enter arrival time:3
Enter first CPU burst time:9
Enter priority:1
pname	at	bt	p
p1	0	0	4
p2	1	6	6
p3	3	7	3
p4	3	9	1
pname	at	bt	p
p1	0	0	4
p2	1	6	6
p3	3	7	3
p4	3	0	1
pname	at	bt	p
p1	0	0	4
p2	1	6	6
p3	3	0	3
p4	3	0	1
pname	at	bt	p
p1	0	0	4
p2	1	0	6
p3	3	0	3
p4	3	0	1
pname	at	bt	p	tct	tat	wt
p1	0	8	4	8	8	0
p2	1	6	6	30	29	23
p3	3	7	3	24	21	14
p4	3	9	1	17	14	5
Avg TAT=18.000000	Avg WT=10.500000
0----p1----8----p4-----17---p3----24---p2---30

Explaination :

Priority Scheduling (PS):

In this algorithm the job will execute according to their priority order. The job which has highest
priority will execute first and the job which has least priority will execute last at the CPU.
Priority scheduling can be preemptive or non-preemptive.

ALGORITHM:

Step 1: Start the process

Step 2: Accept the number of processes in the ready Queue

Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
Time
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
}
wtavg = wt[i]/n;
tatavg = tat[i]/n;
Step 4: Sort the ready queue according to the priority number.

Step 5: Set the waiting of the first process as  ̳0‘ and its burst time as its turnaround time

Step 6: Arrange the processes based on process priority

Step 7: For each process in the Ready Q

Step 8: for each process in the Ready Q calculate
a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)
b) Turnaround time (n)= waiting time(n)+Burst time(n)


Related Post :








Post a Comment

1 Comments

Thanks,To visit this blog.