Professional Documents
Culture Documents
Table of contents
PRIORITY SCHEDULING…………………………………………….…..19
EXPERIMENT NO: 1
ALGORITHM:
found to be less than or equal to zero print the process attributes and
delete the node from the linked lists.
9. Repeat the step 8 until the linked list is empty.
10. The formula for waiting time initial starts time-arrival time of the
process.
11. Formula for TAT is :( Final Finish Time-Initial Staring Time) +waiting
time.
12. Formula for NTAT is (TAT)/(service time).
13. The finish time of the process is nothing but the sum of the time slice
and the finish time of previous process. Also the finish time of the
previous process is its time slice or its service time.
14. Intermediate starting time of the second process would be intermediate
finish time of the first process or arrival time of the second process,
whichever is bigger.
15. Calculate the other process attributes which are mentioned above.
16. Repeat these steps for each and every process.
SAMPLE RUN:
Required input: process name, Arrival time, Service time, Time slice
Expected output: process name, Arrival time, Service time, turn
around time, waiting time.
EXAMPLE OUTPUT:
Process name service time TAT waiting time arrival time
A 3 5 4 0
B 4 5 3 0
C 5 6 3 0
#include<stdio.h>
#include<conio.h>
struct process
{
char na[20];
int at,bt,ft,tat,rem;
float ntat;
}Q[5],temp;
int rr[20],q,x,k;
Void main()
{
int f,r,n,i,j,tt=0,qt,t,flag,wt=0;
float awt=0,antat=0,atat=0;
clrscr();
printf("Enter the no. of jobs:");
scanf("%d",&n);
for(r=0;r<n;r++)
{
printf("Enter process name,arrival time and burst time:");
scanf("%s%d%d",Q[r].na,&Q[r].at,&Q[r].bt);
}
printf("Enter quantum:");
scanf("%d",&qt);
for(i=0;i<n;i++)
HT NO: 08E21A0594 Page 4
B.TECH 3-2 SEM (2010-2011)
{
for(j=i+1;j<n;j++)
{
if(Q[i].at>Q[j].at)
{
temp=Q[i];
Q[i]=Q[j];
Q[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
Q[i].rem=Q[i].bt;
Q[i].ft=0;
}
tt=0;
q=0;
rr[q]=0;
do
{
for(j=0;j<n;j++)
if(tt>=Q[j].at)
{
x=0;
for(k=0;k<=q;k++)
if(rr[k]==j)
HT NO: 08E21A0594 Page 5
B.TECH 3-2 SEM (2010-2011)
x++;
if(x==0)
{
q++;
rr[q]=j;
}
}
if(q==0)
i=0;
if(Q[i].rem==0)
i++;
if(i>q)
i=(i-1)%q;
if(i<=q)
{
if(Q[i].rem>0)
{
if(Q[i].rem<qt)
{
tt+=Q[i].rem;
Q[i].rem=0;
}
else
{
tt+=qt;
Q[i].rem-=qt;
HT NO: 08E21A0594 Page 6
B.TECH 3-2 SEM (2010-2011)
}
Q[i].ft=tt;
}
i++;
}
flag=0;
for(j=0;j<n;j++)
if(Q[j].rem>0)
flag++;
}while(flag!=0);
clrscr();
printf("\n\n\t\tROUND ROBIN ALGORITHM");
printf("\n***************************");
printf("\nprocesses Arrival time burst time finish time tat wt ntat");
for(f=0;f<n;f++)
{
wt=Q[f].ft-Q[f].bt-Q[f].at;
Q[f].tat=Q[f].ft-Q[f].at;
Q[f].ntat=(float)Q[f].tat/Q[f].bt;
antat+=Q[f].ntat;
atat+=Q[f].tat;
awt+=wt;
printf("\n\t%s\t%d\t\t%d\t%d\t%d\t%d %f",
Q[f].na,Q[f].at,Q[f].bt,Q[f].ft,Q[f].tat,wt,Q[f].ntat);
}
antat/=n;
atat/=n;
HT NO: 08E21A0594 Page 7
B.TECH 3-2 SEM (2010-2011)
awt/=n;
printf("\nAverage tat is %f",atat);
printf("\nAverage normalised tat is %f",antat);
printf("\n average waiting time is %f",awt);
getch();
}
Enter number of processes: 5
OUTPUT:
Process name service time TAT waiting time arrival time
A 3 5 4 0
B 4 5 3 0
C 5 6 3 0
Experiment number: 2
ALGORITHM:
SAMPLE RUN:
Example output:
Turn Normalized
Process Arrival Service Start Finish
around turn around
name time time time time
time time
C 2 1 2 3 1 1.00
A 0 1 3 4 4 4.00
B 1 1004 4 104 103 1.03
D 3 100 104 204 201 2.01
void main()
{
int f,r,i,j,n,k,l;
float awt=0;
clrscr();
printf("enter the num of process :");
scanf("%d",&n);
for(r=0;r<n;r++)
{
printf("\n enter the process name, arrival time and burst time=");
scanf("%s%d%d", &q[r].name, &q[r].at,&q[r].bt);
q[r].wt=0;
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(q[i].at>q[j].at)
{
temp=q[i];
q[i]=q[j];
q[j]=temp;
}
printf("\n\n\t\t\t\t\t\t ** SHORTEST JOB FIRST **");
printf("\n ------------------------------------------------");
printf("---------------------------------------------------");
printf("\n process name Arrival time burst time finish time waiting time
turn around time");
printf("\n-------------------------------------------------");
for(f=0;f<n;f++)
{
if(f==0)
q[f].ft=q[f].bt+q[f].at;
else
q[f].ft=q[f-1].ft+q[f].bt;
for(j=f+1;j<n;j++)
if(q[f].ft>=q[j].at)
{
i=j;
for(k=j+1;k<n;k++)
if(q[f].ft>=q[k].at&&q[i].bt>q[k].bt)
i=k;
temp=q[i];
q[i]=q[j];
q[j]=temp;
}
q[f].tat=q[f].ft-q[f].at;
if(f==0)
q[f].wt=0;
else
q[f].wt=q[f-1].ft-q[f].at;
if(q[f].wt<0)
q[f].wt=0;
awt=awt+q[f].wt;
printf("\n\t%-12s %-11d",q[f].name,q[f]
.at);
printf("%-11d %-11d %-14d",q[f].bt, q[f].ft, q[f].wt,q[f].tat);
}
printf("\n------------------------------------------------------------------");
awt=awt/n;
printf("\n\n\t\taverage waiting time=%5.4f",awt);
getch();
}
Output:
Enter the num of processes: 5
Waiting Turn
Name Arrival time Burst time Finish time around
time
A 0 3 3 0 3
B 2 6 9 1 7
E 8 2 11 1 3
C 4 4 15 7 11
D 6 5 20 9 14
Experiment number: 3
With this scheme, the process that requests the CPU first is
allocated the CPU first. This FCFS policy is easily managed with a
FCFS 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.
Algorithm:
1. Create a structure to represent process details.
2. Get the number of process as input from the user.
3. Get the input for the process attributes, which are process name,
arrival time, service time.
4. Calculate the process attributes such TAT, avg TAT, waiting time,
avg waiting time, normalized TAT.
5. Formula for waiting time is
Starting time – arrival time of that process.
6. The formula for TAT is
(Finish time – starting time) + waiting time.
7. The formula for calculating normalized TAT is
(TAT) / (service time).
8. The finish time of the process is nothing but the sum of the service
time of this process and the finish time of the previous process.
Also, the finish time of this process + service time of the process.
9. Starting time of the second process would be finish time of the first
process or arrival time of the second process, whichever is
bigger.
10. Calculate the other process attributes which are mentioned
above.
SAMPLE RUN:
Example output:
Turn
Process Arrival Service Start Finish Normalized
around
name time time time time TAT
time
A 0 1 0 1 1 1
B 1 100 1 101 100 1
C 2 1 101 102 100 100
D 3 100 102 202 199 1.99
}q[20],temp;
void main()
{
int f,r,i,j,n,ft,wt,tat;
float awt=0;
clrscr();
printf("enter the number of processes:");
scanf("%d",&n);
for(r=0;r<n;r++)
{
printf("\nenter process name,arrival time and burst time :");
scanf("%s%d%d",&q[r].name,&q[r].at,&q[r].bt);
}
for(i=0;i<n;i++)
for(j=j+1;j<n;j++)
if(q[i].at>q[j].at)
{
temp=q[i];
q[i]=q[j];
q[j]=temp;
}
printf("\n\n\t\t\t **FCFS**");
printf("\n--------------------------------");
printf("----------------------------------");
printf("\n process name Arrival time Burst time");
printf("Finish time Turn around time Waiting time");
printf("\n---------------------------------------------");
printf("_______________________________________________");
wt=0;
ft=q[0].at;
tat=0;
for(f=0;f<n;f++)
{
wt=ft-q[f].at;
if(wt<0) wt=0;
ft=ft+q[f].bt;
tat=ft-q[f].at;
awt=awt+wt;
printf("\n%-14s,%-11d",q[f].name,q[f].at);
printf("%-11d %-15d %-8d",q[f].bt,ft,tat,wt);
}
printf("\n-----------------------------------");
printf("-------------------------------------");
awt=awt/n;
printf("\n\n\t\t average waiting time=%5.4f", awt);
getch();
}
Output:
** FCFS **
Process Arrival time Burst time Finish time Turn Norm. TAT
name around time
A 0 3 3 3 0
B 2 6 9 7 1
C 4 4 13 9 5
D 6 5 18 12 7
E 8 2 20 12 10
Experiment number: 4
PRIORITY SCHEDULING
ALGORITHM:
1. Create a structure to represent process details.
2. Get the number of process as input from the user.
3. Get the input for the process attributes, which are process name, arrival
time, service time and priority.
4. Sort the list nodes based on its priority if the process are same then sort
it based on its arrival time.
5. Calculate the other process attributes such as TAT, average TAT,
waiting time, average waiting time, normalized TAT.
6. Formula for waiting time is, starting time – arrival time.
7. Formula for calculating TAT,
(Finish time – starting time) + waiting time
8. Formula for normalized TAT= (TAT) / (service time).
9. The finish time of a process is nothing but the sum of service time of
first process and finish time of the previous process. Also, the finish
time of the first process is its service time or finish time of the process is
starting time of this process + service time of the process.
10. Starting time of second process would be finish time of the first
process or arrival time of second process, whichever is bigger.
11. Calculate other process attributes which are mentioned above.
12. Repeat these steps for each and every process.
13. Print the results.
SAMPLE RUN:
Required input: process name, arrival time, service time, time slice
Expected output: process name, arrival time, service time, turn around
time, waiting time.
Example output:
A 3 5 4 2 0
B 4 5 3 3 0
C 5 6 3 5 0
#include<stdio.h>
#include<conio.h>
struct process
char name[20];
int at,bt,wt,tat,ft,pt;
}Q[20],temp;
void main()
int f,r,i,j,k,l,n;
float awt=0;
clrscr();
scanf("%d",&n);
for(r=0;r<n;r++)
scanf("%s%d%d%d",&Q[r].name,&Q[r].pt,&Q[r].at,&Q[r].bt);
Q[r].wt=0;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(Q[i].at>Q[j].at)
temp=Q[i];
Q[i]=Q[j];
Q[j]=temp;
for(f=0;f<n;f++)
if(f==0)
Q[f].ft=Q[f].bt+Q[f].at;
else
Q[f].ft=Q[f-1].ft+Q[f].bt;
for(j=f+1;j<n;j++)
if(Q[f].ft>=Q[j].at)
l=j;
for(k=j+1;k<n;k++)
l=k;
temp=Q[l];
Q[l]=Q[j];
Q[j]=temp;
Q[f].tat=Q[f].ft-Q[f].at;
if(f==0)
Q[f].wt=0;
else
Q[f].wt=Q[f-1].ft-Q[f].at;
if(Q[f].wt<0)
Q[f].wt=0;
awt=awt+Q[f].wt;
printf("\n%-14s%-11d",Q[f].name,Q[f].at);
printf("%-11d%-15d%-8d%d",Q[f].at,Q[f].bt,Q[f].wt,Q[f].tat);
awt=awt/n;
getch();
Output:
Enter number of processes: 5
Waiting
Name Priority Arrival time Burst time TAT
time
A 1 0 4 0 4
B 4 2 6 0 5
C 3 4 4 5 9
D 3 5 6 8 14
E 2 4 6 17 23
Experiment number: 5
Algorithm:
13. Once user enters terminating page number it returns to main program.
14. Now call another function whose function is to print the nodes with
head as argument.
15. In this function print the page detail that is stored in the linked list and
return to the main function.
SAMPLE RUN:
Example output:
N=5
Pages in memory:
5
7
6
printf("\n");
getch();
}
Output:
2 2
3 2 3
2 2 3
1 2 3 1
5 5 3 1
2 5 2 1
4 5 2 4
5 5 2 4
3 3 2 4
2 3 2 4
5 3 5 4
2 3 5 2
Experiment number: 6
ALGORITHM:
1. Create a structure to represent page details.
2. Every node should contain page number, link and also a counter in it
to represent how recently it has used.
3. Declare another counter globally to indicate the maximum number of
the pages. Declare 2 pointer objects.
4. The head pointer is used to mark starting node and its list pointer
object is used for adding nodes.
5. Create a node using a malloc function. As each node is added its
counter value is incremented by 1.
6. Then pass this node to a function whose purpose is to create
nodded.
7. Within this function, increment the global counter and then get the
page number and if the page number already exists then increment
the counter for that node and call the create function with the current
node as arguments.
8. Have a terminating condition on page number.
9. If user enters page number apart from terminating number, then user
wishes to add another page. So in this case compare the global
counter with a value. This value is the max. Number of pages that
can be stored.
10. If the global counter value is greater than the max. Number of
pages then the node which has been not used for longest time
should be removed.
11. To find the node with minimum counter value call a function to find
the node which has minimum counter value. This node is deleted
from the list which means the page which is least recently used has
been taken out first.
12. Then create another node and call the same program with this
newly created node as an argument.
13. Here nodes are added in the tail of linked list and nodes are
removed based on their counter value.
14. Once user enters -999 it returns to main program.
15. Now call another function whose function is to print the nodes with
head as argument.
16. In this function print the page details that are stored in the linked list
and return to the main function.
SAMPLE RUN:
Required input: max. Number of pages, pages numbers, terminating
page number.
Expected output: page numbers.
Example output:
N=5
p[j]=re[i];
j++;
}
else
{
for(k=1;k<=no;k++)
{
for(ch=i-1;ch>=1;ch--)
{
a=0;
if(p[k]==re[ch])
{
a++;
break;
}
}
if(a!=0)
{
if(opr.st>ch)
{
opr.st=ch;
opr.l=re[ch];
opr.ps=k;
}
}
else if(a==0)
{
opr.ps=k;
break;
}
}
p[(opr.ps)]=re[i];
}
}
display(no,p,i);
}
printf("\n");
getch();
}
Output:
2 0 2
3 0 2 3
2 0 2 3
1 0 2 3 1
5 0 2 5 1
2 0 2 5 1
4 0 2 5 4
5 0 2 5 4
3 0 3 5 4
2 0 3 5 2
5 0 3 5 2
2 0 3 5 2
Experiment number: 7
LEAST FREQUENTLY USED
ALGORITHM:
1. Create a structure to represent the page details.
2. Every node should contain page number, link and also a counter in it
to represent how frequently it has been used.
3. Declare another counter globally to indicate the maximum number of
pages. Declare two pointer objects.
4. The head pointer object is used to mark starting node and the least
pointer object is used for adding nodes.
5. Create a node using malloc function. As each node page is added its
counter value is incremented by 1.
6. Then pass this node to function whose purpose is to create nodes.
7. Within this function increment the global counter and then get the
page number and if the page number already exists then increment
the counter for that node and call the create function with the current
node as arguments.
A page can be found existing or not existing by calling
another function with arguments as a head and page number. If that
the main program if the flag set then call the crate function with the
current node itself.
9. If the user enters page number apart from the terminating number,
then the user wishes to add another page. So in this case compare
the global counter with a value. This value is maximum number of
pages that can be stored.
10. If the global counter value is greater than the maximum number of
pages then the node which is least frequently used should be
removed i.e. the node with a minimum counter value should be
removed.
11. To find the node with a minimum counter value calls a function to
find the node which has the minimum counter value? This node is
deleted from the list which means the page which is least frequently
used is has been taken out first.
12. Then create another node and call the same program with this
newly created node as an argument.
13. Here nodes are added in the tail of linked list and nodes are
removed based on their counter value( i.e. page which is least
frequently used).
15. Now call another function whose function is to print the nodes with
head as argument.
16. In this function print the page details and return to main function.
SAMPLE RUN:
Example output:
N=3
Enter page number: 0
Enter page number: 1
Enter page number: 2
Enter page number: 3
Enter page number: 0
Enter page number: 1
Enter page number: 2
Enter page number: 3
Enter page number: 0
Enter page number: 1
Enter page number: 2
Enter page number: 3
Enter page number: 4
Enter page number: 5
Enter page number: 6
Enter page number: 7
Enter page number: -999
Pages in memory:
3
1
7
Number of page faults after frame allocation has filled = 9
Frame allocation size = 3
Total page faults = 12
#include<stdio.h>
#include<conio.h>
void main()
{
int n,fz,i,j,k,fl,fll,c,m;
int p[50];
int f[10];
int pr[10]={0};
clrscr();
printf("enter how many no of processes :");
scanf("%d",&n);
printf("enter %d process memory locations :\n",n);
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("enter frame size:");
scanf("%d",&fz);
/*placing into the frame*/
for(i=0;i<fz;i++)
f[i]=-1;
for(i=0;i<n;i++)
{
for(k=0;k<fz;k++)
pr[k]=0;
fl=0;fll=0;
for(j=0;j<fz;j++)
if(f[j]==p[i])
{
fl=1;
break;
}
if(fl==0)
{
for(j=0;j<fz;j++)
{
for(c=0,k=-1;k>=0;k--)
if(f[j]==p[k])
break;
else
c++;
pr[j]=c;
}
m=pr[0];
j=0;
for(k=0;k<fz;k++)
if(m<pr[k])
{
m=pr[k];
j=k;
}
for(k=0;k<fz;k++)
if(f[k]<0)
{
j=k;
break;
}
f[j]=p[i];
fll=1;
}
printf("\n");
if(fll==1)
{
for(k=0;k<fz;k++)
if(f[k]>=0)
printf("%6d",f[k]);
}
else
printf("\n\nno page fault");
}
getch();
}
Output:
2 0 2
3 0 2 3
2 0 2 3
1 0 2 3 1
5 0 2 5 1
2 0 2 5 1
4 0 2 5 4
5 0 2 5 4
3 0 2 5 3
2 0 2 5 3
5 0 2 5 3
2 0 2 5 3
Experiment number: 8
Algorithm:
1. Define one dimensional arrays to represent resources on the system
and available resources.
2. Define two dimensional arrays to represent allocation of resources
and process claim for each class of resources i.e. a[i,j] represents
allocated jth class resource for ith process. Similarly, c[i,j] represents
ith process claim for jth class resource.
3. Prompt for the number of processes.
4. Prompt the different class of resources.
5. Read the allocation matrix and decrease the available resources
accordingly.
6. Read the claim matrix that represents type of resources and number
of each type of resources required for that process to complete its
execution.
7. Print the allocation matrix, claim matrix, available array and
resources array.
d) Call the subroutine SAFE, if it returns the safe mode carry out
the allocation. Else restore the original state, suspend the
process.
FUNCTION SAFE:
1. Current availability = Availability
2. Process rest = { number of processes }
3. Rest = { all processes }
4. Possible = true
5. While(possible)
{
Find the process in rest such that the process (claim –
allocation) is less than or equal to currently available. If such
process is found
i. Assign the resources to that process so that, that
process can complete its execution.
ii. Once the execution is over, relieve all the resources from
that process and add to the current availability.
}
Otherwise, possible is false.
6. Return NULL.
printf("%d",alloc[i][j]);
}
printf("\n");
}
prc=0;
for(i=1;i<=p;i++)
{
if(comp[i]==0)//if not completed
{
prc=i;
for(j=1;j<=r;j++)
{
if(avail[j])
{
prc=0;
break;
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\nProcess ",prc,"runs to completion!");
count++;
for(j=1;j<=r;j++)
{
avail[j]+=alloc[prc][j];
alloc[prc][j]=0;
clm[prc][j]=0;
comp[prc]=1;
}
}
}
}while(count!=p&&prc!=0);
if(count==p)
printf("\nThe system is in a safe state!!");
else
printf("\nThe system is in an unsafe state!!");
getch();
}
OUT PUT:
For process I : 2 4 5
Available resource is :