Dr. M.G.R.
EDUCATIONAL AND
RESEARCH INSTITUTE
(Dr. M.G.R. UNIVERSITY)
RECORD NOTE BOOK
NAME :
REG.NO. :
BRANCH :
SUBJECT:
Year :
Dr. M.G.R.
EDUCATIONAL AND RESEARCH INSTITUTE
UNIVERSITY
Maduravoyal ,Chennai -600 095.
Register No.
RECORD NOTE
BOOK
2009-2010
Name of Lab :
Department: :
Certified that this is a bonafide record of work done by ................................................................
Of ...............................................class in the ...................................... ..........laboratory during the
Year 2009.
Signature of lab in charge Signature – Head of Dept.
Submitted for the practical examination held on ..................................................
Internal Examiner External Examiner
INDEX
Exp Date Name of Experiment Page no. Marks Staff
no. sign.
UNIX
COMMANDS
SHELL
PROGRAMMING
CALCULATION OF DA,HRA OF EMPLOYEE
echo enter basic pay
read bp
if test $bp –lt 5000
then
DA=`expr $bp \* 10 / 100`
HRA=`expr $bp \* 20 / 100`
echo DA=$DA
echo HRA=$HRA
elif test $bp -lt 10000
then
DA=`expr $bp \* 20 / 100`
HRA=`expr $bp \* 30 / 100`
echo DA=$DA
echo HRA=$HRA
elif test $bp -gt 10000
then
DA=`expr $bp \* 40 / 100`
HRA=`expr $bp \* 40 / 100`
echo DA=$DA
echo HRA=$HRA
fi
OUTPUT:
enter basic pay
4000
DA=400
HRA=800
enter basic pay
7000
DA=1400
HRA=2100
enter basic pay
15000
DA=6000
HRA=6000
FIND WHETHER NUMBER IS EVEN OR ODD
echo enter the number
read n
r=`expr $n % 2`
if test $r -eq 0
then
echo the number $n is even
else
echo the number $n is odd
fi
OUTPUT:
enter the number
56
the number 56 is even
enter the number
67
the number 67 is odd
PERFORM VARIOUS ARITHMETIC OPERATIONS USING SWITCH CASE
echo menu
echo 1.Addition
echo 2.Subtraction
echo 3.Multiplication
echo 4.Division
echo 5.Exit
ch=y
while ch=y
do
echo enter the choice
read choice
echo enter the number
read a b
case $choice in
1)echo Adding $a and $b gives
res=`expr $a + $b`
echo $res;;
2)echo Subtracting $a and $b gives
res=`expr $a - $b`
echo $res;;
3)echo Multiplication of $a and $b gives
res=`expr $a \* $b`
echo $res;;
4)echo division of $a and $b gives
res=`expr $a / $b`
echo $res;;
5)
exit;;
esac
echo do you want to continue
read ch
done
OUTPUT:
menu
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
enter the choice
enter 2 number
2 5
Adding 2 and 5 gives
do you want to continue
enter the choice
2
enter 2 number
7 3
Subtracting 7 and 3 gives
do you want to continue
enter the choice
enter 2 number
5 3
Multiplying 5 and 3 gives
15
do you want to continue
enter the choice
enter 2 number
10 2
Dividing 10 and 2 gives
do you want to continue
y
enter the choice
DISPLAY WHETHER NUMBER IS POSITIVE,NEGATIVE OR ZERO
echo enter the number
read n
if test $n -gt 0
then
echo number is positive
elif test $n -lt 0
then
echo number is negative
else
echo number is zero
fi
OUTPUT:
enter the number
56
number is positive
enter the number
-3
number is negative
enter the number
00
number is zero
SWAP TWO NUMBERS WITHOUT USING ADDITIONAL VARIABLE
echo enter the two number
read a b
echo before swapping
echo value of a=$a
echo value of b=$b
a=`expr $a + $b`
b=`expr $a - $b`
a=`expr $a - $b`
echo after swapping
echo value of a=$a
echo value of b=$b
OUTPUT:
enter the two number
47 98
before swapping
value of a=47
value of b=98
after swapping
value of a=98
value of b=47
C
PROGRAMS
PROCESS CREATION
#include<sys/types.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main(){
int pid;
pid=fork();
if(pid<0)
printf(“fork failed”);
exit(-1);
if(pid==0)
printf(“child:my process id is %d and my parent id is %d “,getpid(),getppid());
execlp(“bin/ls”,”ls”,NULL);
}
wait(NULL);
printf(“\n child complete”);
exit(0);
OUTPUT:
child:my process id is 4018 and my parent process is 4017
child complete
FIRST COME FIRST SERVED CPU SCHEDULING ALGORITHM
#include<stdio.h>
main()
{
int i,n,w[10],e[10],b[10];
float wa=0,ea=0;
printf("\nEnter the no of jobs: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the burst time of job %d :",i+1);
scanf("%d",&b[i]);
if(i==0)
{
w[0]=0;
e[0]=b[0];
}
else
{
e[i]=e[i-1]+b[i];
w[i]=e[i-1];
}
}
printf("\n\n\tJobs\tWaiting time \tBursttime\tExecution time\n");
printf("\t-------------------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("\t%d\t\t%d\t\t%d\t\t%d\n",i+1,w[i],b[i],e[i]);
wa+=w[i];
ea+=e[i];
}
wa=wa/n;
ea=ea/n;
printf("\n\nAverage waiting time is :%2.2f ms\n",wa);
printf("\nAverage execution time is:%2.2f ms\n\n",ea);
}
OUTPUT:
Enter the no of jobs: 4
Enter the burst time of job 1 : 5
Enter the burst time of job 2 : 4
Enter the burst time of job 3 : 3
Enter the burst time of job 4 : 6
Jobs Waiting time Burst time Execution time
----------------------------------------------------------------
1 0 5 5
2 5 4 9
3 9 3 12
4 12 6 18
Average waiting time is : 6.50 ms
Average execution time is: 11.00 ms
SHORTEST JOB FIRST CPU SCHEDULING ALGORITHM
#include<stdio.h>
struct sjfs
{
char pname[10];
int btime;
}proc[10],a;
int main( )
{
struct sjfs proc[10];
int n,i,j;
int temp=0,temp1=0,temp2;
char name[20];
float tt,awt;
printf("Enter the number of processes:\n");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("Enter the process name: \n");
scanf("%s",&proc[i].pname);
printf("Enter the Burst time:\n");
scanf("%d",&proc[i].btime);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(proc[i].btime<proc[j].btime)
{
a=proc[i];
proc[i]=proc[j];
proc[j]=a;
}
}
}
printf("-------------------- CPU SCHEDULING ALGORITHM - SJFS -------------------------- ");
printf("\n\tprocess name \tBurst time \twaiting time \tturnaround time\n");
temp=0;
for(i=0;i<n;i++)
{
temp=temp1+temp+proc[i].btime;
temp1=temp1+proc[i].btime;
temp2=temp1-proc[i].btime;
printf("\n\t %s \t %d ms \t %d ms \t %d
ms\n",proc[i].pname,proc[i].btime,temp2,temp1);
}
printf("-------------------------------------------------------------------------------");
awt=(temp-temp1)/n;
tt=temp/n;
printf("\nThe Average Waiting time is %4.2f milliseconds\n",awt);
printf("\nThe Average Turnaround time is %4.2f",tt);
}
OUTPUT:
Enter the number of processes:
4
Enter the process name:
p1
Enter the Burst time:
5
Enter the process name:
p2
Enter the Burst time:
5
Enter the process name:
p3
Enter the Burst time:
6
Enter the process name:
p4
Enter the Burst time:
2
-------------------- CPU SCHEDULING ALGORITHM - SJFS --------------------------
Process name Burst time waiting time turnaround time
p4 2 ms 0ms 2 ms
p1 5 ms 2 ms 7 ms
p2 5 ms 7 ms 12 ms
p3 6 ms 12 ms 18 ms
-----------------------------------------------------------------------------------------------------
The Average Waiting time is 5.00 milliseconds
The Average Turnaround time is 9.00 milliseconds
ROUND ROBIN CPU SCHEDULING ALGORITHM
#include <stdio.h>
struct roundrobin
{
int b_time;
int run_time;
int w_time;
int t_time;
}rr[10];
int main()
{
int i,p_no=0,time=0;
int et=0;
int tot_pro,tot_time=0;
int tq,flag=0;
float awt;
int wt=0;
printf("\t\tProgram for Round Robin Scheduling\n\n");
printf("Enter Timequantom of a system=");
scanf("%d",&tq);
printf("\n\nEnter the number of Process=");
scanf("%d",&tot_pro);
for(i=1;i<=tot_pro;i++)
{
printf("\n\tEnter the Burst time of Process%d=",i);
scanf("%d",&rr[i].b_time);
tot_time=tot_time+rr[i].b_time;
}
printf("\n\t\tRESULT\n");
printf("\n\n\t--------------------------\n");
printf("\t| Start | Process | End | \n");
printf("\t| Time | No | Time | \n");
printf("\t--------------------------\n");
while(time<tot_time)
{
p_no=p_no+1;
if (p_no>tot_pro)
p_no=1;
if (rr[p_no].b_time!=0)
{
if (rr[p_no].b_time>tq)
{
if (flag!=p_no)
rr[p_no].w_time=et-rr[p_no].run_time;
rr[p_no].run_time=rr[p_no].run_time+tq;
et=et+tq;
printf("\t| %d",time);
rr[p_no].b_time=rr[p_no].b_time-tq;
time=time+tq;
flag=p_no;
}
else
{
if (flag!=p_no)
rr[p_no].w_time=et-rr[p_no].run_time;
rr[p_no].run_time=rr[p_no].run_time+rr[p_no].b_time;
et=et+rr[p_no].b_time;
printf("\t| %d",time);
time=time+rr[p_no].b_time;
rr[p_no].b_time=0;
flag=p_no;
}
printf("\t| P%d | %d\t |\n",p_no,time);
}
}
printf("\t--------------------------\n");
for(i=1;i<=tot_pro;i++)
{
printf("Run time of P%d is %d\n",i,rr[i].run_time);
printf("Wt P%d is %d\n",i,rr[i].w_time);
wt=wt+rr[i].w_time;
}
printf("Waiting time of the process is=%d\n",wt);
awt=wt/tot_pro;
printf("Average Waiting time is=%5.3f",awt);
}
OUTPUT:
Enter Time quantum of the system = 1
Enter the number of Process = 4
Enter the Burst time of Process 1= 4
Enter the Burst time of Process 2= 3
Enter the Burst time of Process 3= 5
Enter the Burst time of Process 4= 6
|
Start time Process No. End time
0 P1 1
1 P2 2
2 P3 3
3 P4 4
4 P1 5
5 P2 6
6 P3 7
7 P4 8
8 P1 9
9 P2 10
10 P3 11
11 P4 12
12 P1 13
13 P3 14
14 P4 15
15 P3 16
16 P4 17
17 P4 18
Run time of P1 is 4 ms
Wt P1 is 9 ms
Run time of P2 is 3
Wt P2 is 7 ms
Run time of P3 is 5
Wt P3 is 11 ms
Run time of P4 is 6
Wt P4 is 12 ms
Average Waiting time is=9.000 ms
PRIORITY CPU SCHEDULING ALGORITHM
#include<stdio.h>
#include<malloc.h>
void line(int i)
int j;
for(j=1;j<=i;j++)
printf("__");
printf("\n");
struct process
int p_id,priority;
int etime,wtime,tatime;
};
struct process *p, temp;
int i,j,k,l,n;
float awtime=0,atatime=0;
int main()
printf("Priority Scheduling \n");
line(29);
printf("enter the number of process:");
scanf("%d",&n);
p=(struct process *) calloc(n+1,sizeof(struct process));
p[0].wtime=0;
p[0].tatime=0;
for(i=1;i<=n;i++)
printf("enter the execution time of process %d:",i-1);
scanf("%d",&p[i].etime);
printf("enter the priority of process %d:",i-1);
scanf("%d",&p[i].priority);
p[i].p_id=i-1;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(p[j].priority>p[j+1].priority)
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
for(i=1;i<=n;i++)
p[i].wtime=p[i-1].tatime;
p[i].tatime=p[i-1].tatime + p[i].etime;
awtime+=p[i].wtime;
atatime +=p[i].tatime;
awtime=awtime/n;
atatime=atatime/n;
printf("Scheduling \n");
line(60);
printf("process \t\t execution \t wait \t\t turn around \n");
printf("id_no \t\t time \t\t time \t\t time \n");
line(60);
for(i=1;i<=n;i++)
printf("%7d \t %14d \t %8d \t %14d \n",p[i].p_id,p[i].wtime,p[i].etime,p[i].tatime);
line(60);
printf("avg waiting time:\t %2f \n",awtime);
printf("avg trunaround time:\t %2f \n",atatime);
line(60);
return(0);
}
OUTPUT:
enter the number of process:3
enter the execution time of process 0:2
enter the priority of process 0:2
enter the execution time of process 1:1
enter the priority of process 1:1
enter the execution time of process 2:4
enter the priority time of process 2:3
Scheduling____________________________________________
______________________________________________________
process execution wait turnaround
Id no time time time
_____________________________________________________
1 1 0 1
0 2 1 3
2 4 3 7
_____________________________________________________
avg waiting time: 1.333333
avg turnaround time: 3.6666667
IMPLEMENTATION OF BANKER’S ALGORITHM
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,z;
int res[10];
int resource,process,boolean=0;
int allocation[10][10],sel[10],max[10][10],need[10][10],work[10],work1[10];
int check=0;
int adpro;
int ar[10];
printf("Welcome to Bankers Algorithms");
printf("\n Enter the no .of processes:");
scanf("%d",&process);
printf("\n Enter the number of Resources");
scanf("%d",&resource);
for(i=0;i<resource;i++)
{
printf("Enter the instances of Resource %d:",i+1);
scanf("%d",&res[i]);
}
for(i=0;i<process;i++)
{
printf("\n Enter allocated resources for process %d:",i+1);
for(j=0;j<resource;j++)
{
printf("\n Resource %d:",j+1);
scanf("%d",&allocation[i][j]);
}
}
for(i=0;i<10;i++)
sel[i]=-1;
for(i=0;i<process;i++)
{
printf("Enter maximum need of process: %d",i+1);
for(j=0;j<resource;j++)
{
printf("\n Resource %d:",j+1);
scanf("%d",&max[i][j]);
}
}
for(i=0;i<process;i++)
for(j=0;j<resource;j++)
need[i][j]=max[i][j]-allocation[i][j];
printf("\n The Need is \n");
for(i=0;i<process;i++)
{
for(j=0;j<resource;j++)
printf("\t%d",need[i][j]);
printf("\n");
}
printf("\n The Avalilable is ");
for(i=0;i<resource;i++)
{
work[i]=0;
for(j=0;j<process;j++)
work[i]=work[i]+allocation[j][i];
work[i]=res[i]-work[i];
}
for(z=0;z<process;z++)
{
for(i=0;i<process;i++)
{
for(j=0;j<resource;j++)
{
if(work[j]>=need[i][j]&&sel[i]==-1)
{
boolean=1;
}
else
{
boolean=0;
break;
}
}
if(boolean==1)
{
sel[i]=1;
for(j=0;j<resource;j++)
{
work[j]=work[j]+allocation[i][j];
}
}
}
}
if(check==1)
printf("\n System is in Unsafe mode");
else
printf("\n System is in safe mode");
}
OUTPUT:
Enter the no .of processes:5
Enter the number of Resources3
Enter the instances of Resource 1:10
Enter the instances of Resource 2:5
Enter the instances of Resource 3:7
Enter allocated resources for process 1:
Resource 1:0
Resource 2:1
Resource 3:0
Enter allocated resources for process 2:
Resource 1:2
Resource 2:0
Resource 3:0
Enter allocated resources for process 3:
Resource 1:3
Resource 2:0
Resource 3:2
Enter allocated resources for process 4:
Resource 1:2
Resource 2:1
Resource 3:1
Enter allocated resources for process 5:
Resource 1:0
Resource 2:0
Resource 3:2
Enter maximum need of process:1
Resource 1:7
Resource 2:5
Resource 3:3
Enter maximum need of process: 2
Resource 1:3
Resource 2:2
Resource 3:2
Enter maximum need of process: 3
Resource 1:9
Resource 2:0
Resource 3:2
Enter maximum need of process: 4
Resource 1:2
Resource 2:2
Resource 3:2
Enter maximum need of process: 5
Resource 1:4
Resource 2:3
Resource 3:3
The Need is
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1
The Available is 3 3 2
System is in safe mode
IMPLEMENTATION OF FIRST FIT ALGORITHM
#include<stdio.h>
#include<string.h>
main()
{
int n,j,i,size[10],sub[10],f[10],m,x,ch,t;
int cho;
printf("\t\t MEMORY MANAGEMENT \n");
printf("\t\t =================\n");
printf("\tEnter the total no of blocks: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter the size of blocks: ");
scanf("%d",&size[i]);
}
cho=0;
while(cho==0)
{
printf("\n Enter the size of the file: ");
scanf("%d",&m);
x=0;
for(i=1;i<=n;i++)
{
if(size[i]>=m)
{
printf("\n size can occupy %d",size[i]);
size[i]-=m;
x=i;
break;
}
}
if(x==0)
{
printf("\n\nBlock can't occupy\n\n");
}
printf("\n\nSNO\t\tAvailable block list\n") ;
for(i=1;i<=n;i++)
printf("\n\n%d\t\t\t%d",i,size[i]);
printf("\n\n Do u want to continue.....(0-->yes/1-->no): ");
scanf("%d",&cho);
}
}
OUTPUT:
Enter the total no of blocks: 4
Enter the size of blocks: 50
Enter the size of blocks: 20
Enter the size of blocks: 30
Enter the size of blocks: 40
Enter the size of the file: 25
size can occupy 50
SNO Available block list
1 25
2 20
3 30
4 40
Do u want to continue.....(0-->yes/1-->no): 0
Enter the size of the file: 28
size can occupy 30
SNO Available block list
1 25
2 20
3 2
4 40
Do u want to continue.....(0-->yes/1-->no):0
Enter the size of the file: 32
size can occupy 40
SNO Available block list
1 25
2 20
3 2
4 8
Do u want to continue.....(0-->yes/1-->no): 0
IMPLEMENTATION OF BEST FIT ALGORITHM
#include<stdio.h>
main()
{
int n,j,i,size[10],sub[10],f[10],m,x,ch,t;
int cho;
printf("\t\t MEMORY MANAGEMENT \n");
printf("\t\t =================\n");
printf("\tENTER THE TOTAL NO OF blocks : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter the size of blocks : ");
scanf("%d",&size[i]);
}
cho=0;
while(cho==0)
{
printf("\n Enter the size of the file : ");
scanf("%d",&m);
for(i=1;i<=n;i++)
{
sub[i]=size[i];
f[i]=i;
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
if(sub[i]>sub[j])
{
t=sub[i];
sub[i]=sub[j];
sub[j]=t;
t=f[i];f[i]=f[j];
f[j]=t;
}
}
for(i=1;i<=n;i++)
{
if(size[f[i]]>=m)
{
printf("size can occupy %d : ",size[f[i]]);
size[f[i]]-=m;
x=i;
break;
}
}
if(x==0)
{
printf("block can't occupy");
}
printf("\n\nSNO\t\t Available Block size\n") ;
for(i=1;i<=n;i++)
printf("\n%d\t\t%d",i,size[i]);
printf("\n\n Do u want to continue.....(0-->yes\t/1-->no)");
scanf("%d",&cho);
}
}
OUTPUT:
ENTER THE TOTAL NO OF blocks : 4
Enter the size of blocks : 50
Enter the size of blocks : 100
Enter the size of blocks : 200
Enter the size of blocks : 150
Enter the size of the file : 95
size can occupy 100 :
SNO SIZE
1 50
2 5
3 200
4 150
Do u want to continue.....(0-->yes /1-->no)0
Enter the size of the file : 48
size can occupy 50 :
SNO Available Block list
1 2
2 5
3 200
4 150
Do u want to continue.....(0-->yes /1-->no)1
IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHM
IN FIFO PATTERN
#include<stdio.h>
main()
{
int main_mem,cur=0,i=0,j,fault=0;
static int page[100],page_mem[100],flag,num;
for(i=0;i<100;i++)
page_mem[i]=-2;
printf("\n\t\t\t\t paging->fifo\n");
printf("\n\n Enter the number of pages in main memory ");
scanf("%d",&main_mem);
printf("\n enter no of page references");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\n Enter page reference:");
scanf("%d",&page[i]);
}
printf("\n\t\t\t\t Fifo-> paging \n\n\n");
for(i=0;i<main_mem;i++)
printf("\t page %d",i+1);
for(i=0;i<num;i++)
{
for(j=0;j<main_mem;j++)
if(page[i]==page_mem[j])
{
flag=1;
break;
}
if(!flag)
{
page_mem[cur]=page[i];
fault++;
}
printf("\n\n");
for(j=0;j<main_mem;j++)
printf("\t%d",page_mem[j]);
if(!flag&&cur<main_mem-1)
cur++;
else if(!flag)
cur=0;
flag=0;
}
printf("\n\n-2 refers to empty blocks\n\n")
printf("\n\n No of page faults:%d\n",fault);
}
OUTPUT:
PAGING -> FIFO
Enter the number of pages in main memory: 3
enter no of page references: 8
Enter page reference: 2
Enter page reference: 0
Enter page reference: 3
Enter page reference: 0
Enter page reference: 2
Enter page reference: 3
Enter page reference: 5
Enter page reference: 9
FIFO - PAGING
page 1 page 2 page 3
2 -2 -2
2 0 -2
2 0 3
2 0 3
2 0 3
2 0 3
5 0 3
5 9 3
-2 refers to empty blocks
No of page faults: 5
IMPLEMENTATION OF LRU-PAGE REPLACEMENT ALGORITHM
#include<stdio.h>
#include<stdlib.h>
#define max 100
int frame[10],count[10],cstr[max],tot,nof,fault;
main()
{
getdata();
push();
}
getdata()
{
int pno,i=0;
printf("\n\t\tL R U - Page Replacement Algorithm\n");
printf("\nEnter No. of Pages in main memory:");
scanf("%d",&nof);
printf("\nEnter the no of page references:\n");
scanf("%d",&pno);
for(i=0;i<pno;i++)
{
printf("Enter page reference%d:",i);
scanf("%d",&cstr[i]);
}
tot=i;
for(i=0;i<nof;i++)
printf("\tpage%d\t",i);
}
push()
{
int x,i,j,k,flag=0,fault=0,nc=0,mark=0,maximum,maxpos=-1;
for(i=0;i<nof;i++)
{
frame[i]=-1;
count[i]=mark--;
}
for(i=0;i<tot;i++)
{
flag=0;
x=cstr[i];
nc++;
for(j=0; j<nof; j++)
{
for(k=0; k<nof;k++)
count[k]++;
if(frame[j]==x)
{
flag=1;
count[j]=1;
break;
}
}
if(flag==0)
{
maximum = 0;
for(k=0;k<nof;k++)
{
if(count[k]>maximum && nc>nof)
{
maximum=count[k];
maxpos = k;
}
}
if(nc>nof)
{
frame[maxpos]=x;
count[maxpos]=1;
}
else
frame[nc-1]=x;
fault++;
dis();
}
}
printf("\nTotal Page Faults :%d",fault);
}
dis()
{
int i=0;
printf("\n\n");
while(i<nof)
{
printf("\t%d\t",frame[i]);
i++;
}
}
OUTPUT:
paging->LRU
Enter the number of pages in main memory: 3
Enter the no of page references: 8
Enter page reference0: 0
Enter page reference1: 1
Enter page reference2: 2
Enter page reference3: 1
Enter page reference4: 2
Enter page reference5: 5
Enter page reference6: 0
Enter page reference7: 1
Page 0 page 1 page 2
0 -1 -1
0 1 -1
0 1 2
5 1 2
5 0 2
5 0 1
Total Page Faults : 6
IMPLEMENTAION OF DINING PHILOSOPHER’S PROBLEM
#include<stdio.h>
char state[10],self[10],spoon[10];
void test(int k)
{
if((state[(k+4)%5]!='e')&&(state[k]=='h')&&(state[(k+1)%5]!='e'))
{
state[k]='e';
self[k]='s';
spoon[k]='n';
spoon[(k+4)%5]='n';
}
}
void pickup(int i)
{
state[i]='h';
test(i);
if(state[i]=='h')
{
self[i]='w';
}
}
void putdown(int i)
{
state[i]='t';
spoon[i]='s';
spoon[i-1]='s';
test((i+4)%5);
test((i+1)%5);
}
int main()
{
int ch,a,n,i;
printf("\t\t Dining Philosopher Problem\n");
for(i=0;i<5;i++)
{
state[i]='t';
self[i]='s';
spoon[i]='s';
}
printf("\t\t Initial State of Each Philosopher\n");
printf("\n\t Philosopher No.\t Think/Eat \tStatus \tspoon");
for(i=0;i<5;i++)
{
printf("\n\t\t %d\t\t%c\t\t%c\t%c\n",i+1,state[i],self[i],spoon[i]);
}
printf("\n 1.Exit \n 2.Hungry\n3.Thinking\n");
printf("\n Enter your choice\n");
printf("\t\t");
scanf("%d",&ch);
while(ch!=1)
{
switch(ch)
{
case 2:
{
printf("\n\t Enter which philosopher is hungry\n");
printf("\t\t");
scanf("%d",&n);
n=n-1;
pickup(n);
break;
}
case 3:
{
printf("\n\t Enter which philosopher is thinking\n");
printf("\t\t");
scanf("%d",&n);
n=n-1;
putdown(n);
break;
}
}
printf("\n\t State of Each philosopher\n\n");
printf("\n\t Philosoper No.\t Thinking\t Hungry");
for(i=0;i<5;i++)
{
printf("\n\t\t %d\t\t%c\t\t%c\t%c\n",i+1,state[i],self[i],spoon[i]);
}
printf("\n 1.Exit\n 2.Hungry\n 3.Thinking\n");
printf("\n Enter your choice\n");
printf("\t\t");
scanf("%d",&ch);
}
}
OUTPUT:
Initial State of Each Philosopher
Philosopher No. Think/Eat Status spoon
1 t s s
2 t s s
3 t s s
4 t s s
5 t s s
1.Exit
2.Hungry
3.Thinking
Enter your choice: 2
Enter which philosopher is hungry : 4
State of Each philosopher
Philosopher No. Think/Eat Status spoon
1 t s s
2 t s s
3 t s n
4 e s n
5 t s s
1.Exit
2.Hungry
3.Thinking
Enter your choice 2
Enter which philosopher is hungry : 3
State of Each philosopher
Philosopher No. Think/Eat Status spoon
1 t s s
2 t s s
3 h w n
4 e s n
5 t s s
1.Exit
2.Hungry
3.Thinking
Enter your choice 3
Enter which philosopher is thinking : 4
State of Each philosopher
Philosopher No. Think/Eat Status spoon
1 t s s
2 t s n
3 e s n
4 t s s
5 t s s