Академический Документы
Профессиональный Документы
Культура Документы
DATE
COMMANDS:
1. TASK
COMMAND
SYNTAX
EXPLANATION: This command displays the current system date and time on the
screen.
OUTPUT
2. TASK
: Tue Jun 19
COMMAND
: date.
SYNTAX
: date +%m.
3. TASK
COMMAND
SYNTAX
: 06.
EXPLANATION: This command displays the name of the current month on the
screen.
OUTPUT
4. TASK
COMMAND
SYNTAX
: Jun.
EXPLANATION: This command displays the current system date on the screen.
OUTPUT
5. TASK
COMMAND
SYNTAX
: 19.
6. TASK
COMMAND
SYNTAX
: 07.
EXPLANATION: This command displays the current system time (in hours) on the
screen.
OUTPUT
: 11.
2
Department of Information Technology, RGCE, Sriperumbudur
7. TASK
COMMAND
SYNTAX
: date.
: date +%M.
EXPLANATION: This command displays the current system time (in minutes) on
the
screen.
OUTPUT
: 43.
8. TASK
COMMAND
SYNTAX
: date.
: date +%S.
EXPLANATION: This command displays the current system time (in seconds) on
the
screen.
OUTPUT
: 15.
9. TASK
COMMAND
SYNTAX
: calendar.
: cal.
EXPLANATION: This command displays the calendar of the current month on the
screen.
OUTPUT
:
S
Jun 07
T
S
3
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
10. TASK
COMMAND
SYNTAX
EXPLANATION: This command displays on the screen the argument of the echo
command.
OUTPUT
: echo OS.
OS
11. TASK
COMMAND
SYNTAX
EXPLANATION : This command lists the information about all the users who
have
logged on to that system.
OUTPUT
12. TASK
COMMAND
4
Department of Information Technology, RGCE, Sriperumbudur
SYNTAX
: whoami.
13. TASK
COMMAND
SYNTAX
: root.
: To create a directory.
: make directory.
: mkdir.
14. TASK
COMMAND
SYNTAX
: mkdir student.
: The directory student is created.
: To change directory.
: change directory.
: cd directory name.
15. TASK
COMMAND
: cd staff.
: The directory staf is switched onto.
: To delete a directory.
: remove directory.
5
SYNTAX
: rmdir student.
: The student directory is deleted.
16. TASK
COMMAND
SYNTAX
17. TASK
COMMAND
SYNTAX
EXPLANATION : This command displays all the files and directories of the
system.
OUTPUT
18. TASK
COMMAND
SYNTAX
: To create a file.
: cat.
: cat> file name.
EXPLANATION : This command leads to the creation of a new file with the
specified
file name and contents.
EXAMPLE
: cat> wind.
6
Department of Information Technology, RGCE, Sriperumbudur
OUTPUT
19. TASK
COMMAND
SYNTAX
: To view a file.
: cat.
: cat file name.
: cat wind.
OUTPUT
screen.
20. TASK
: To copy a file.
COMMAND
SYNTAX
: copy.
: cp sourcefile destinationfile.
EXPLANATION : This command produces a copy of the source file and is stored
in
the specified destination file by overwriting its previous
contents.
EXAMPLE
OUTPUT
21. TASK
COMMAND
SYNTAX
: cp sun moon.
: The contents of sun file will be copied to the moon file.
: To move a file.
: move.
: mv sourcefile destinationfile.
EXPLANATION : After moving the contents of the source file into destination
file,
the source file is deleted.
EXAMPLE
OUTPUT
: mv sun moon.
: After copying contents from the sun file to moon file,
the
sun file is deleted.
22. TASK
COMMAND
SYNTAX
: cut c3 moon.
: Those characters occurring in the 3rd column of the file called
moon are displayed.
23. TASK
COMMAND
: To delete a file.
: remove.
8
Department of Information Technology, RGCE, Sriperumbudur
SYNTAX
: rm file name.
EXPLANATION : This command deletes the specified file from the directory.
EXAMPLE
OUTPUT
24. TASK
COMMAND
SYNTAX
: rm sun.
: The file called sun will be deleted.
EXPLANATION : This command displays the specified no. of rows form the top
of the specified file.
EXAMPLE
OUTPUT
25. TASK
COMMAND
SYNTAX
EXPLANATION
bottom
: head -1 sun.
: The first row of the file called sun is displayed.
: To retrieve a file.
: tail.
: tail -no. of rows file name.
: This command displays the specified no. of rows form the
of the specified file.
EXAMPLE
OUTPUT
26. TASK
: tail -1 moon.
: The last row of the file called moon is displayed.
COMMAND
SYNTAX
: sort.
: sort
file name.
: sort win.
: The contents of the file win are displayed on the screen in
a
sorted order.
27. TASK
COMMAND
SYNTAX
file name.
EXPLANATION : This command displays on the screen the no. of rows, words,
and
the sum of no. of characters and words.
EXAMPLE
OUTPUT
: wc ball.
: The no. of rows, words, and no. of characters present in the
file
ball are displayed.
28. TASK
COMMAND
SYNTAX
: cal 2007.
10
Department of Information Technology, RGCE, Sriperumbudur
OUTPUT
EX. NO. :2
DATE
SHELL PROGRAMMING
11
Step1:
In the dollar prompt type
$ vi < file name>
Where vi is the editor ,it will open a new window in which you can type the program
you want
Step2:
After typing the program press ESC and : together then at the bottom of the vi
screen you can see i.e. prompt .In that type as wq which means write and quit i.e.
the content what is typed will be written and saved into that file that has been
created
Step3:
Once wq is typed at the : prompt ,the prompt would change to $ symbol in which
you have to do the following
12
Department of Information Technology, RGCE, Sriperumbudur
Sh
<file name> - is the name of the file for which the output is to be got
Basically to print a text in the your shell programs echo command is used
13
IF THEN-ELSE CONSTRUCT
if [ condition]
then
<action>
else
statements
fi (end of if)
WHILE
while <condition>
do
<statements>
Done
CASE
Case $<option> in
1) <statements>;;
2) <statements>;;
3) ..
14
Department of Information Technology, RGCE, Sriperumbudur
4)
.
.
.
*) <error statement>;;
esac
For loop
For(( intitialization;condition;incremetation/decrementation))
15
PROGRAM:
echo Enter the number:
read number
i= 1
fact= 1
while [$i le $number]
do
fact= `expr $fact \* $i `
i= `expr $i +1`
done
echo The factorial of $number is $fact.
SAMPLE OUTPUT:
Enter the number:
3
The factorial of 3 is 6.
16
Department of Information Technology, RGCE, Sriperumbudur
17
PROGRAM:
SAMPLE OUTPUT:
18
Department of Information Technology, RGCE, Sriperumbudur
PROGRAM:
echo Enter the number:
read n
x= $n
sum= 0
while [ $n gt 0]
do
y= `expr $n % 10`
z= `expr $y \* $y \* $y`
sum= `expr $sum + $z`
n= `expr $n / 10`
done
if [ $x eq $sum]
then
echo $x is an Armstrong number.
else
echo $x is not an Armstrong number.
fi
SAMPLE OUTPUT 1:
Enter a number:
153
153 is an Armstrong number.
SAMPLE OUTPUT 2:
19
Enter a number:
33
33 is not an Armstrong number.
20
Department of Information Technology, RGCE, Sriperumbudur
PROGRAM:
echo Enter the first number:
read a
echo Enter the second number:
read b
echo Enter the third number:
read c
if [$a gt $b a $a gt $c]
then
echo $a is greater.
elif [$b gt $c]
then
echo $b is greater.
else
echo $c is greater.
fi
SAMPLE OUTPUT:
22
Department of Information Technology, RGCE, Sriperumbudur
PROGRAM:
SAMPLE OUTPUT:
23
PROGRAM:
a= -1
b= 1
c= 0
echo Enter the limit:
read n
echo FIBONACCI SERIES
for (i =1; i <=n; i++)
do
c= `expr $a + $b`
echo $c
a=$b
b=$c
done
SAMPLE OUTPUT:
Enter the limit:
5
FIBONACCI SERIES
0
1
1
2
24
Department of Information Technology, RGCE, Sriperumbudur
25
PROGRAM:
echo enter the day of the week (1-7)
read n
case $n in
1)echo 1 is Sunday;;
2)echo 2 is Monday;;
3)echo 3.is Tuesday;;
4)echo 2 is Wednesday;;
5)echo 3.is thursday;;
6)echo 2 is Friday;;
7)echo 3.is Saturday;;
*)echo Invalid option;;
SAMPLE OUTPUT 1:
Enter the day of the week(1-7)
1
Sunday
SAMPLE OUTPUT 2:
26
Department of Information Technology, RGCE, Sriperumbudur
27
PROGRAM:
echo enter string 1
read s1
echo enter string2
read s2
if [ $s1 == $s2]
then
echo the given string are equal
else
echo The given strings are not equal
fi
SAMPLE OUTPUT 1:
enter string 1
ram
enter string 2
sam
The given strings are not equal
28
Department of Information Technology, RGCE, Sriperumbudur
SAMPLE OUTPUT 2
enter string 1
sam
enter string 2
sam
The given strings are qual
29
Aim
Algorithm
5. Start with the first process from its initial position let other process to be in
queue
Result
31
Program
#include<stdio.h>
main()
{
int n,a[10],b[10],t[10],w[10],g[10],i,m;
float att=0,awt=0;
for(i=0;i<10;i++)
{
a[i]=0; b[i]=0; w[i]=0; g[i]=0;
}
printf("enter the number of process");
scanf("%d",&n);
printf("enter the burst times");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
printf("\nenter the arrival times");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
g[0]=0;
for(i=0;i<10;i++)
32
Department of Information Technology, RGCE, Sriperumbudur
g[i+1]=g[i]+b[i];
for(i=0;i<n;i++)
{
w[i]=g[i]-a[i];
t[i]=g[i+1]-a[i];
awt=awt+w[i];
att=att+t[i];
}
awt =awt/n;
att=att/n;
printf("\n\tprocess\twaiting time\tturn arround time\n");
for(i=0;i<n;i++)
{
printf("\tp%d\t\t%d\t\t%d\n",i,w[i],t[i]);
}
printf("the average waiting time is %f\n",awt);
printf("the average turn around time is %f\n",att);
}
33
34
Department of Information Technology, RGCE, Sriperumbudur
Output
waiting time
p0
p1
11
p2
17
p3
18
21
35
Aim
Algorithm
8. Make the CPU scheduler goes around the ready queue allocating CPU to each
process
36
Department of Information Technology, RGCE, Sriperumbudur
9. Make the CPU scheduler picks the first process and sets time to interrupt after
quantum
expired dispatches the process
9. If the process have burst less than the time quantum than the process release
the CPU
10. If the process have bust greater then time quantum then time will go off and
cause
interrupt to OS and the process put into the tail of ready queue and the
schedule select
next process
Result
The program for round robin scheduling was implemented and hence verified
37
Program
#include<stdio.h>
int ttime,i,j,temp;
main()
{
int pname[10],btime[10],pname2[10],btime2[10];
int n,x,z;
printf("Enter the no. of process:");
scanf("%d",&n);
printf("Enter the process name and burst time for the process\n");
for(i=0;i<n;i++)
{
printf("Enter the process name:");
scanf("%d",&pname2[i]);
printf("Enter burst time for the process %d:",pname2[i]);
scanf("%d",&btime2[i]);
}
printf("PROCESS NAME \t\t BURST TIME\n");
for(i=0;i<n;i++)
printf("%d\t\t\t %d\n",pname2[i],btime2[i]);
z=1;
38
Department of Information Technology, RGCE, Sriperumbudur
while(z==1)
{
ttime=0;
for(i=0;i<n;i++)
{
pname[i]=pname2[i];
btime[i]=btime2[i];
}
printf("Invalid option");
break;
}
printf("\n\n If you want to continue press 1:");
scanf("%d",&z);
}
}
while(j<n)
{
for(i=0;i<n;i++)
40
Department of Information Technology, RGCE, Sriperumbudur
{
if(btime[i]>0)
{
if(btime[i]>=tslice)
{
ttime+=tslice;
btime[i]=btime[i]-tslice;
printf("\n%d\t\t %d \t\t %d",pname[i],btime[i],ttime);
if(btime[i]==0)
j++;
}
else
{
ttime+=btime[i];
btime[i]=0;
printf("\n%d\t\t %d \t\t %d",pname[i],btime[i],ttime);
}
}
}
}
}
41
Output
PROCESS NAME
BURST TIME
7
43
10
12
14
16
44
Department of Information Technology, RGCE, Sriperumbudur
Aim
Algorithm
4. Select the process which have shortest burst will execute first
5. If two process have same burst length then FCFS scheduling algorithm used
7. Start with the first process from its selection as above and let other process to
be in
queue
Result
46
Department of Information Technology, RGCE, Sriperumbudur
Program
#include<stdio.h>
int main()
{
int n,j,temp,temp1,temp2,pr[10],b[10],t[10],w[10],p[10],i;
float att=0,awt=0;
for(i=0;i<10;i++)
{
b[i]=0;w[i]=0;
}
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
p[i]=i;
}
47
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(b[i]>b[j])
{
temp=b[i];
temp1=p[i];
b[i]=b[j];
p[i]=p[j];
b[j]=temp;
p[j]=temp1;
}
}
}
w[0]=0;
for(i=0;i<n;i++)
w[i+1]=w[i]+b[i];
for(i=0;i<n;i++)
{
t[i]=w[i]+b[i];
48
Department of Information Technology, RGCE, Sriperumbudur
awt=awt+w[i];
att=att+t[i];
}
awt=awt/n;
att=att/n;
printf("\n\t process \t waiting time \t turn around time \n");
for(i=0;i<n;i++)
printf("\t p[%d] \t %d \t\t %d \n",p[i],w[i],t[i]);
printf("the average waitingtimeis %f\n",awt);
printf("the average turn around time is %f\n",att);
return 1;
}
49
Output
process
waiting time
p[0]
p[1]
p[2]
11
p[3]
11
17
p[4]
17
25
50
Department of Information Technology, RGCE, Sriperumbudur
Aim
Algorithm
51
6. If not start at the hole that is sharing the pervious best fit search end
Result
The program for best fit was implemented and hence verified
52
Department of Information Technology, RGCE, Sriperumbudur
Program
#include<stdio.h>
#include<process.h>
void main()
{
int a[20],p[20],i,j,n,m,temp,b[20],temp1,temp2,c[20];
printf("Enter no of Blocks.\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %dst Block size:",i);
scanf("%d",&a[i]);
b[i]=i;
}
printf("Enter no of Process.\n");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the size of %dst Process:",i);
scanf("%d",&p[i]);
c[i]=i;
}
for(i=0;i<n;i++)
{
53
for(j=0;j<m;j++)
{
if(a[i]<a[j])
{
temp=a[i];
temp1=b[i];
a[i]=a[j];
b[i]=b[j];
a[j]=temp;
b[j]=temp1;
}
if(p[i]<p[j])
{
temp=p[i];
temp2=c[i];
p[i]=p[j];
c[i]=c[j];
p[j]=temp;
c[j]=temp2;
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
54
Department of Information Technology, RGCE, Sriperumbudur
{
if(p[j]<=a[i])
{
printf("The Process %d allocated to Block %d\n",c[j],b[i]);
p[j]=10000;
break;
}
}
}
for(j=0;j<m;j++)
{
if(p[j]!=10000)
{
printf("The Process %d is not allocated\n",j);
}
}
}
55
\Output
Enter no of Blocks.
3
Enter the 0st Block size:300
Enter the 1st Block size:200
Enter the 2st Block size:100
Enter no of Process.
3
56
Department of Information Technology, RGCE, Sriperumbudur
57
Aim
Algorithm
58
Department of Information Technology, RGCE, Sriperumbudur
6. If not start at the hole that is sharing the pervious first fit search end
Result
The program for first fit was implemented and hence verified
Program
59
#include<stdio.h>
#include<process.h>
void main()
{
int a[20],p[20],i,j,n,m;
printf("Enter no of Blocks.\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %dst Block size:",i);
scanf("%d",&a[i]);
}
printf("Enter no of Process.\n");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("Enter the size of %dst Process:",i);
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(p[j]<=a[i])
{
60
Department of Information Technology, RGCE, Sriperumbudur
Output
Enter no of Blocks.
5
Enter the 0st Block size:500
Enter the 1st Block size:400
61
62
Department of Information Technology, RGCE, Sriperumbudur
Aim
Algorithm
4. Check the need of replacement from old page to new page in memory
Result
The program for FIFO page replacement was implemented and hence
verified.
64
Department of Information Technology, RGCE, Sriperumbudur
Program
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
65
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
Output
70120304230321201701
page frames
-1
-1
-1
3
2
0
1
Page Fault Is 15
67
68
Department of Information Technology, RGCE, Sriperumbudur
Aim
Algorithm
69
Result
The program for LRU page replacement was implanted and hence verified
70
Department of Information Technology, RGCE, Sriperumbudur
Program
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
71
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
72
Department of Information Technology, RGCE, Sriperumbudur
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}
73
Output
Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
7
74
Department of Information Technology, RGCE, Sriperumbudur
75
Aim
Algorithm
8. Write a separate routine for creating memory delete memory in separate region
76
Department of Information Technology, RGCE, Sriperumbudur
Result
The program for creation of shared memory was implemented and hence
verified.
77
Program
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
main()
{
int shmid;
key_t key=0x10;
shmid=shmget(key,100,IPC_CREAT|0666);
if( shmid < 0 )
printf("\nFirst SHMID failed\n");
else
printf("\nFirst SHMID Succeded id=%d \n",shmid);
shmid=shmget(key,101,IPC_CREAT|0666);
if(shmid<0)
printf("\nSecond SHMID failed\n");
else
printf("\nSecond SHMID Succeded id=%d \n",shmid);
shmid=shmget(key,90,IPC_CREAT|0666);
if(shmid<0)
printf("\nThird SHMID failed\n");
78
Department of Information Technology, RGCE, Sriperumbudur
else
printf("\n Third SHMID Succeded id=%d \n",shmid);
}
Output
79
80
Department of Information Technology, RGCE, Sriperumbudur
Aim
Algorithm
1. Start
5. Display the changes and verify it by opeing the file and modify the contents
81
Result
The program for the file locking was implemented and hence verified
82
Department of Information Technology, RGCE, Sriperumbudur
Program
ls -l
echo "enter the file name to be locked";
read f;
echo $f;
ls -l $f
echo "1.read only \n";
echo "2.read write \n";
read c;
if [ $c == 1 ]
then
chmod 444 $f
ls -l $f
elif [ $c == 2 ]
then
chmod 666 $f
ls -l $f
else
echo "wrong choice";
fi
83
Output
$ sh lock.sh
-rw-rw-rw-
-rw-rw-r--
-rw-rw-r--
rrb.c
-rw-rw-r--
1.read only \n
2.read write \n
1
-r--r--r--
85
Aim
Algorithm
11. If need[i,j]=k then process Pi may need k more instances of resource type
Rj to complete its task
a) Finish[i] = false
iii)
finish[i] = true
iv)
i)
available = available-request
88
Department of Information Technology, RGCE, Sriperumbudur
need = need-request
Result
Program
/* Bankers Algorithm */
#include<iostream.h>
#include<conio.h>
89
class bank
{
int **p,**max,**need,*avail,prno,rno;
int check(int);
public:
bank();
void input();
void process();
~bank();
};
bank::bank()
{
clrscr();
cout<<"\nEnter the number of Process:\t";
cin>>prno;
cout<<"\n Enter the number of Resources:\t";
cin>>rno;
avail=new int[rno];
p=new int*[prno];
max=new int*[prno];
need=new int*[prno];
for(int i=0;i<prno;i++)
{
p[i]=new int[rno];
max[i]=new int[rno];
90
Department of Information Technology, RGCE, Sriperumbudur
need[i]=new int[rno];
}
}
void bank::input()
{
int a,b;
cout<<"\n Enter maximum Need:\n";
for(a=0;a<prno;a++)
for(b=0;b<rno;b++)
cin>>max[a][b];
cout<<"\nEnter the Current state:\n";
for(a=0;a<prno;a++)
for(b=0;b<rno;b++)
{
cin>>p[a][b];
need[a][b]=max[a][b]-p[a][b];
}
cout<<"\n Enter Available Resources:\n";
for(b=0;b<rno;b++)
cin>>avail[b];
}
int bank::check(int c)
{
int d;
for(d=0;(avail[d]>=need[c][d])&&(d<rno)&&(need[c][d]!=-1);d++);
return d;
91
}
void bank::process()
{
int e=0,f=0;
do
{
int g=check(f);
if(g==rno)
{
for(int h=0;h<rno;h++)
{
avail[h]+=p[f][h];
need[f][h]=-1;
}
e++;
cout<<"\n Process["<<f+1<<"] is completed \n";
f=0;
}
else
{
f++;
if(f==prno)
{
cout<<"\n Deadlock\n";
break;
92
Department of Information Technology, RGCE, Sriperumbudur
}
}
}
while(e<prno);
}
bank::~bank()
{
delete *p,*max,*need;
delete p,max,need,avail;
}
void main()
{
clrscr();
bank msat;
char op;
do
{
msat.input();
msat.process();
cout<<"\n Do you want to continue(y/n):\t";
cin>>op;
getch();
}
while(op=='y');
cout<<"\n Press Any Key To Exit";
getch();
93
94
Department of Information Technology, RGCE, Sriperumbudur
#include<stdio.h>
int mutex=1,full=0,empty=3,x=0;
main()
{
int n;
void producer();
void consumer();
int wait(int);
95
int signal(int);
printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");
while(1)
{
printf("\nENTER YOUR CHOICE\n");
scanf("%d",&n);
switch(n)
{
case 1:
if((mutex==1)&&(empty!=0))
producer();
else
printf("BUFFER IS FULL");
break;
case 2:
if((mutex==1)&&(full!=0))
consumer();
else
printf("BUFFER IS EMPTY");
break;
case 3:
exit(0);
break;
}
}
96
Department of Information Technology, RGCE, Sriperumbudur
}
int wait(int s)
{
return(--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nproducer produces the item%d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\n consumer consumes item%d",x);
x--;
mutex=signal(mutex);
97
98
Department of Information Technology, RGCE, Sriperumbudur
OUTPUT:1.PRODUCER
2.CONSUMER
3.EXIT
#include<stdio.h>
struct ptable
{
int pno, staddr;
}p[50];
struct pcount
{
int pnum;
char data[50];
}pg[50][50];
main()
{
int n,psize, i,j,k,st,pagenum,disp,phyaddr,laddr,found=0;
printf("\n MEMORYIFORMATION\n");
101
printf("***************\n");
printf("\n Enter the number of pages:\n");
scanf("%d",&n);
printf("Enter the page size:\n");
scanf("%d", &psize);
for(i=1;i<=n;i++)
{
printf("\n Enter the starting addresss for page[%d]:\n",i);
scanf("%d",&st);
for(j=1;j<=psize;j++)
{
printf("page[%d]\tData:",st);
scanf("%s", pg[i][j].data);
pg[i][j].pnum=st;
st=st+1;
}
}
printf("\n MEMORY \n");
printf("**********\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=psize;j++)
{
printf("\n page[%d]\t Data:%s\n", pg[i][j].pnum, pg[i][j].data);
}
102
Department of Information Technology, RGCE, Sriperumbudur
}
printf("\n page table details\n");
printf("***********************");
printf("\n Enter the starting address for the pages:",i);
for(k=1;k<=n;k++)
{
printf("page [%d] \t starting address:\n",k);
p[k].pno=k;
scanf("%d", &p[k].staddr);
}
printf("\n pageno\t starting address\n");
for(k=1;k<=n;k++)
{
printf("\n %d\t\t%d\n", p[k].pno, p[k].staddr);
}
printf("\nEnter the logical addressL:\n");
scanf("%d", &laddr);
pagenum=laddr/100;
disp=laddr%100;
printf("\n pageno%d\tdisp%d\n", pagenum, disp);
for(k=1;k<=n;k++)
if(p[k].pno==pagenum)
{
phyaddr=p[k].staddr+disp;
found=1;
break;
103
}
if(found==1)
{
printf("\n physical address%d\n", phyaddr);
for(j=1;j<=psize;j++)
{
if(pg[pagenum][j].pnum==phyaddr)
printf("\nData%s\n",pg[pagenum][j].data);
}
}
else
printf("\n not found\n");
}
104
Department of Information Technology, RGCE, Sriperumbudur
OUTPUT:-
MEMORYIFNORMATION
***************
Data:blue
page[101]
Data:green
Data:red
page[201]
Data:yellow
105
MEMORY
**********
page[100]
Data:blue
page[101]
Data:green
page[200]
Data:red
page[201]
Data:yellow
starting
100
page [2]
starting address:
200
106
Department of Information Technology, RGCE, Sriperumbudur
100
200
pageno1
disp1
physical address101
Datagreen
Ex:no:2
Date:
107
108
Department of Information Technology, RGCE, Sriperumbudur
DEKKERS ALGORITHM
AIM:
To solve mutual exclusion problem by using DEKKERS algorithm.
Algorithm:
Step 3: Every process checks the other flag after setting its own.If both are set, the
turn
variable is used to allow only one process to proceed.
Step 4: The trun variable is only considered when both processes are using,or
trying to
use, the resource.
Step 5: The process with the turn will(eventually)discover the other flag free and
will
proceed.
109
PROGRAM 2:
# include<iostream.h>
class dekker
{
public:
int c1,c2;
dekker()
{
c1=c2=0;
}
void dek()
{
do
{
cout<<\n The both process doesnt have the same value;
c1 = 1;
if(c1==1 && c2==0)
{
cout<<Setting value for process C2;
c1 = c2 =2;
}
if(c1 ==2 && c2 == 2)
{
cout<<\n the process C2 is entered;
110
Department of Information Technology, RGCE, Sriperumbudur
break;
}
}while(c1!=c2);
}
};
int main()
{
dekker d;
d.dek();
return 0;
}
111
OUTPUT:
112
Department of Information Technology, RGCE, Sriperumbudur
BANKERS ALGORITHM
AIM:
Program to implement Safe state of a system and to avoid deadlock situation
Using Bankers Algorithm in C language.
Algorithm:
113
Step 2: Get the number of process, resource, allocation matrix, Maximum matrix
and
Available matix.
Step 3: Calcualte need matrix using Maximum matrix and allocation matrix i.e.,
NEED[i,j]=MAX[i,j]-ALLOC[i,j]. Also initialize work matrix i.e.,
WORK[j]=AVAIL[j].
Step 4:Now find safety sequence of the system using work matrix,need matrix.
Step 6: If condition doesnt satisfies, move to next process and repeat Step 5,
until completion of all processes and print the safety sequence.
Step 7: If any process request for resource, get the process id and request matrix
from
The user.
Step 8: Check the following conditions for the respective process using Request
matrix,Need matrix and Available matrix i.e., REQ[j]<=NEED[k,j] and
REQ[j]<=AVAIL[j].
114
Department of Information Technology, RGCE, Sriperumbudur
Step 9: If the above condition satisfies, request can be granted and proceed next
step
otherwise display error as Request cannot be granted.
Step 11: To find safety sequence of the system, repeat Step 5 and Step 6 for all
processes.
115
PROGRAM 10:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
int alloc[20][20],max[20][20],avail[20],need[20][20],work[20]={0};
int newavail[20],req[20]={0},check=0,check2=0,cond,p;
int i=0,j=0,m=0,n=0,t=0,x=0,c[20]={0},k=0,count,count2,a[20],b;
int x2=0,c2[20];
printf("Enter the no. of resources\n");
scanf("%d",&m);
printf("Enter the no. of process");
scanf("%d",&n);
printf("\n Enter the resources for available\n");
for(j=1;j<=m;j++)
{
printf("Enter the %d resources of avail",j);
scanf("%d", &avail[j]);
work[j]=avail[j];
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
116
Department of Information Technology, RGCE, Sriperumbudur
max
need\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
printf("%2d",alloc[i][j]);
printf("\t");
for(j=1;j<=m;j++)
printf("%2d",max[i][j]);
printf("\t");
for(j=1;j<=m;j++)
printf("%2d",need[i][j]);
printf("\n");
}
printf("\n Process executes in this order\n");
do{
for(i=1;i<=n;i++)
117
{
count=0;
if(c[i]!=i+1)
{
for(j=1;j<=m;j++)
{
if(need[i][j]<=work[j])
count = count+1;
}
if(count == m)
{
printf("p%d\t",i);
c[i]=i+1;
x = x+1;
for(j=1;j<=n;j++)
work[j]=work[j]+alloc[i][j];
}
}
}
check = check +1;
}
while(x<n && check <=n);
if(x==n)
printf("\n system is in saftey\n");
else
118
Department of Information Technology, RGCE, Sriperumbudur
exit(0);
}
printf("\n Execution of process after request");
do
{
for(i=1;i<=n;i++)
{
count2 = 0;
if(c2[i]!=i+1)
{
for(j=1;j<=m;j++)
{
if(need[i][j]<=avail[j])
count2 = count2+1;
}
if(count2 == m)
{
printf("p%d\t",i);
c2[i]=i+1;
x2=x2+1;
for(j=1;j<=n;j++)
{
avail[j]=avail[j]+alloc[i][j];
}
}
120
Department of Information Technology, RGCE, Sriperumbudur
}
}
check2 = check2 +1;
}while((x2<n)&&(check2<=n));
if(x2==n)
printf("\n System is in safe state we can grant the request");
else
printf("\n System is in unsafe state we cannot grant the request");
OUTPUT 10:
Enter the no. of resources
3
Enter the no. of process3
122
Department of Information Technology, RGCE, Sriperumbudur
max
need
3 2 1 1 2 3 -2 0 2
222 333 111
2 2 2 1 2 3 -1 0 1
Process executes in this order
p1
p2
p3
system is in saftey
Checking the bankers algorithm after the request
Enter the request process number2
Enter the values1
1
1
req is not satisfied
123