0% found this document useful (0 votes)
9 views

Week 9 OS Task

The document discusses the First Come First Served (FCFS) CPU scheduling algorithm, which allocates the CPU to processes in the order they arrive without preemption. It includes two programming implementations, one using pointers and another using arrays, to demonstrate how to manage process scheduling and calculate average waiting and turnaround times. The output example shows the execution of three processes with their respective burst and arrival times, resulting in an average waiting time of 17 and a turnaround time of 27.

Uploaded by

rydhamgupta84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Week 9 OS Task

The document discusses the First Come First Served (FCFS) CPU scheduling algorithm, which allocates the CPU to processes in the order they arrive without preemption. It includes two programming implementations, one using pointers and another using arrays, to demonstrate how to manage process scheduling and calculate average waiting and turnaround times. The output example shows the execution of three processes with their respective burst and arrival times, resulting in an average waiting time of 17 and a turnaround time of 27.

Uploaded by

rydhamgupta84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

WEEK 09

SCHEDULING ALGORITHMS:

FIRST COME FIRST SERVED(FCFS)


By far the simplest, CPU-scheduling algorithm is the first come,first
served(FCFS) algorithm. With this scheme, the process that requests the CPU
first is allocated the CPU first. The implementation of the FCFS policy is easily
managed with a FIFO queue. When a process enters the ready queue, its PCB is
linked onto the tail of the queue. When the CPU is free, it is allocated to the
process at the head of the queue. The running process is then removed from the
queue.

The FCFS scheduling algorithm is non-preemptive. Once the CPU has been
allocated to a process, that process keeps the CPU until it releases the CPU,
either by terminating or by requesting i/o.

PROGRAM:

USING POINTERS:
#include<stdio.h> //HEADER FILES
#include<malloc.h>
#include<string.h>
typedefstruct node //STRUCTURE
{
char prss[3];
int burst;
int arrival;
struct node *next;
}node;
node *front=NULL; //GLOBAL VARIABLES
node *rear=NULL;
void insert(); //FUNCTION DECLARATION
void display(int);
void main() //MAIN FUNCTION
{
int i ,n;
printf("\nEnter number of processes : ");
scanf("%d",&n);
for(i=0;i<n;i++) //LOOP
insert(); //FUNCTION CALL
printf("\n\nExecuting processes : \n");
display(n); //FUNCTION CALL
printf("\n");
} //END OF MAIN

void insert() //FUNCTION DEFINITION


{
node *p; int
b ,a; char
str[3];
p=(node*)malloc(sizeof(node)); //DYNAMIC MEMORY
ALLOCATION
printf("\n\tEnter the process name : ");
scanf("%s",p->prss);
printf("\tEnter Burst time : ");
scanf("%d",&b); printf("\tEnter
arrival time : "); scanf("%d",&a);

p->burst=b; p-
>arrival=a; p-
>next=NULL;
if(front==NULL)
{
front=p;
rear=p;
}
else
{
rear->next=p;
rear=p;
}
}
void display(int n) //FUNCTION DEFINITION
{
node *temp=front;
intwttime=0,c=0;
float turn=0.0;
if(front!=NULL)
{

printf("\n---------------------------------------------------------------
\n\t");
while(temp!=NULL)
{
printf("|\t%s\t",temp->prss);
temp=temp->next;
}
printf("|\n-------------------------------------------------------------
--\n\t");
temp=front;
while(temp!=NULL)
{
printf(" \t%d\t ",temp->burst);
temp=temp->next;
}
printf("\n---------------------------------------------------------------
--\n\t");
temp=front;
printf("0\t");
while(temp!=NULL)
{
wttime+=c;
turn+=c+temp->burst;
c=c+temp->burst;
printf(" \t%d\t ",c);
temp=temp->next;
}
printf("\n---------------------------------------------------------------
--\n");
printf("\n\nAveragewt time = %d ",wttime/n);
printf("\nTurnaround time = %f\n",turn/n);
}
}

PROGRAM:

USINGARRAYS:
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
char p[10][5],temp[5]; int
c=0,pt[10],i,j,n,temp1; float
bst=0.0,turn=0.0; clrscr();

printf("enter no of processes:");
scanf("%d",&n); for(i=0;i<n;i++)

{
printf("enter process%d name:\n",i+1);
scanf("%s",&p[i]);
printf("enter process time");
scanf("%d",&pt[i]);
}
printf("\n.....................................................\n");
for(i=0;i<n;i++)
{
printf("|\t %s\t",p[i]);
}
printf("|\n.....................................................\n");
for(i=0;i<n;i++)
{
printf("\t\t%d",pt[i]);
}
printf("\n.....................................................\n");
printf("0");
for(i=0;i<n;i++)
{
bst+=c;
turn+=c+pt[i];
c=c+pt[i];
printf("\t\t%d",c);
}
printf("\nAverage time is %f: ",bst/n);
printf("\nTurn around time is %f", turn/n);
getch();
}
OUTPUT:

Enter number of processes : 3

Enter the process 1 name : P1


Enter Burst time : 24
Enter arrival time : 0

Enter the process 2 name : P2


Enter Burst time : 3
Enter arrival time : 0

Enter the process 3 name : P3


Enter Burst time : 3
Enter arrival time : 0
Executing processes :

------------------------------------------------------------------------------------------
| P1 | P2 | P3 |
------------------------------------------------------------------------------------------
24 3 3
------------------------------------------------------------------------------------------
0 24 27 30

Average wt time = 17
Turnaround time = 27.000000

You might also like