Lab Exercise
Lab Exercise
DEPARTMENT OF CSE
REG.NO:
________________
II.WORKING WITH FILE RELATED COMMANDS
Creating a file:
[cse414@cc2linux ~]$ cat > basic
multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.
every user can have a login name and a password .
operating system is used to have interface between system and user.
Display a file:
[cse414@cc2linux ~]$ cat commands
working with file related commands.
process and status information command
filter command
working with directories.
Append a file:
[cse414@cc2linux ~]$ cat >> pipes
filters are used to extract the lines, which contain a specific pattern to arrange
the contents.
[cse414@cc2linux ~]$ cat pipes
if we want to perform multiple tasks we can go pipes and filters. a pipe is a
mechanism which takes the output of a command
its input for the next command.
filters are used to extract the lines,which contain a specific pattern to arrange
the contents.
________________
Concatenation of files:
[cse414@cc2linux ~]$ cat basic commands > pipes
[cse414@cc2linux ~]$ cat pipes
multitasking is the capability of the operating system to peerform various tasks.
A single user can perform various tasks.
every user can have a login name and a password .
working with file related commands.
process and status information command
filter command
working with directories.
Copy a file:
[cse414@cc2linux ~]$ cp basic commands
[cse414@cc2linux ~]$ cat commands
multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.
every user can have a login name and a password .
Move a file:
[cse414@cc2linux ~]$ mv basic commands
[cse414@cc2linux ~]$ cat basic
cat: basic: No such file or directory
[cse414@cc2linux ~]$ cat commands
multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.
every user can have a login name and a password .
Comparing files:
[cse414@cc2linux ~]$ cmp commands pipes
cmp: EOF on commands
List :
[cse414@cc2linux ~]$ list ls
-bash: list: command not found
[cse414@cc2linux ~]$ ls
commands pipes
Remove:
[cse414@cc2linux ~]$ rm pipes
[cse414@cc2linux ~]$ ls
commands
More:
[cse414@cc2linux ~]$ more new old
::::::::::::::
new
::::::::::::::
multitasking is the capability of the operating system to peerform various tasks
.A single user can perform various tasks.
every user can have a login name and a password .
os is used to have interface between system and user.
old: No such file or directory
Calendar:
[cse414@cc2linux ~]$ cal
December 2000
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Binary calculator:
[cse414@cc2linux ~]$ bc
bc 1.06
5*4
20
Who :
[cse414@cc2linux ~]$ who
cse319 pts/1 Dec 31 23:04 (132.147.162.8)
cse318 pts/2 Dec 31 23:51 (132.147.162.7)
cse212 pts/3 Dec 31 23:04 (132.147.162.9)
cse316 pts/4 Dec 31 23:24 (132.147.162.5)
Finger command:
[cse414@cc2linux ~]$ finger
Login Name Tty Idle Login Time Office Office Phone
Cse303 cse303 pts/22 Dec 31 23:10 (132.147.162.11)
Cse322 cse322 pts/3 1 Dec 31 23:04 (132.147.162.9)
Cse324 cse324 pts/8 1 Dec 31 23:06 (132.147.162.10)
cse325 cse325 pts/13 Dec 31 23:19 (132.147.162.1)
cse326 cse326 pts/11 Dec 31 23:55 (132.147.162.2)
cse332 cse332 pts/9 Dec 31 23:06 (132.147.162.3)
cse335 cse335 pts/7 1 Dec 31 23:05 (132.147.162.4)
cse338 cse338 pts/4 Dec 31 23:24 (132.147.162.5)
cse340 cse340 pts/10 2 Dec 31 23:32 (132.147.162.27)
cse346 cse346 pts/2 Dec 31 23:51 (132.147.162.7)
Who am i:
[cse414@cc2linux ~]$ who am i
cse414 pts/11 Dec 31 23:55 (132.147.162.2)
Man:
[cse414@cc2linux ~]$ man date
DATE(1) User Commands DATE(1)
NAME
date - print or set the system date and time
SYNOPSIS
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
DESCRIPTION
Display the current time in the given FORMAT, or set the system
date.
-d, --date=STRING
display time described by STRING, not ânowâ
-f, --file=DATEFILE
like --date once for each line of DATEFILE
Word count:
[cse414@cc2linux ~]$ wc new
wc new
6 85 557 new
[cse414@cc2linux ~]$ wc -w new
85 new
[cse414@cc2linux ~]$ wc -c new
557 new
[cse414@cc2linux ~]$ wc -l new
6 new
Echo:
[cse414@cc2linux ~]$ echo
Read:
[cse414@cc2linux ~]$ read a
5
[cse414@cc2linux ~]$ echo $a
5
Terminal line:
[cse414@cc2linux ~]$ tty
/dev/pts/11
Which :
[cse414@cc2linux ~]$ which date
/bin/date
[cse414@cc2linux ~]$ which new
/usr/bin/which: no new in (/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr
/X11R6/bin:/home/cse414/bin)
IV.FILTER COMMAND
Sorting:
[cse414@cc2linux ~]$ cat new
multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.
every user can have a login name and a password .
[cse414@cc2linux ~]$ sort new
every user can have a login name and a password .
multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.
Head:
[cse414@cc2linux ~]$ head new
multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.
every user can have a login name and a password .
Tail:
[cse414@cc2linux ~]$ tail new
multitasking is the capability of the operating system to perform various tasks.
A single user can perform various tasks.
every user can have a login name and a password .
[cse414@cc2linux ~]$ tail -2 new
Necessary ingredients like conditional and control structures and variables.
communication is between different terminals.
Cut:
[cse414@cc2linux ~]$ cut -c1-3 new
mul
eve
UNI
os
UNI
com
[cse414@cc2linux ~]$ cut -r1-3 new
cut: invalid option -- r
Try `cut --help' for more information.
[cse414@cc2linux ~]$ ls
latest new
Paste:
[cse414@cc2linux ~]$ paste new latest
multitasking is the capability of the operating system to perform various tasks.A
single user can perform various tasks. multitasking is the capability of the
operating system to perform various tasks.A single user can perform various tasks.
Grep:
[cse414@cc2linux ~]$ grep "between" new
os is used to have interface between system and user.
communication is between different terminals.
Paste:
[cse414@cc2linux ~]$ paste -d "&" new latest
multitasking is the capability of the operating system to perform various tasks.A
single user can perform various tasks.&multitasking is the capability of the
operating system
.& os is used to have interface between system and user.&os is used to have
interface between system and user
Common:
[cse414@cc2linux ~]$ comm new latest
multitasking is the capability of the operating system to peerform
various tasks.A single user can perform various tasks.
every user can have a login name and a password .so, accessing
another users data is impossible without permission.
os is used to have interface between system and user.
communication is between different terminals.
Pipes:
[cse414@cc2linux ~]$ who | wc -l
22
________________
EX.NO: 2a IMPLEMENTING opendir, readdir, closedir, etc.. SYSTEM CALLS
PROGRAM:
#include<stdio.h>
#include<dirent.h>
struct dirent *dptr;
int main(int argc, char *argv[])
{
char buff[100];
DIR *dirp;
printf(“\n\n ENTER THE DIRECTORY NAME:”);
scanf(“%s”, buff);
if((dirp=opendir(buff))==NULL)
{
printf(“The given directory does not exist”);
exit(1);
}
while(dptr=readdir(dirp))
{
printf(“%s\n”,dptr->d_name);
}
closedir(dirp);}
OUTPUT:
[kalai@localhost ~]$ cc sys1.c
[kalai@localhost ~]$ ./a.out
________________
EX.NO: 2b IMPLEMENTING Fork, getpid, Exit, Etc…. SYSTEM CALLS
PROGRAM:
#include<stdio.h>
#include<unistd.h>
main()
{
int pid,pid1,pid2;
pid=fork();
if(pid==-1)
{
printf(“ERROR IN PROCESS CREATION \n”);
exit(1);
}
if(pid!=0)
{
pid1=getpid();
printf(“\n THE PARENT PROCESS ID IS: %d\n”, pid1);
}
else
{
pid2=getpid();
printf(“\n THE CHILD PROCESS ID IS: %d\n”, pid2);
}
}
OUTPUT:
[kalai@localhost ~]$ cc sys2.c
[kalai@localhost ~]$ ./a.out
________________
EX.NO: 2c IMPLEMENTING open,read SYSTEM CALLS
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int fd;
if((fd=open("prime.sh"))==-1)
{
perror("cannot open the file.dat");
exit(0);
}
else
printf("\n FILE OPENED SUCCESSSFULLY");
return 0;
}
OUTPUT:
[kalai@localhost ~]$ cc opdir.c
[kalai@localhost ~]$ ./a.out
FILE OPENED SUCCESSSFULLY
________________
2.READ SYSTEM CALL
#include<stdio.h>
#include<stdlib.h>
main()
{
char b[20];
int fd,xr;
if((fd=open("flowers.sh",0))==-1)
{
printf("cannot open file");
exit(1);
}
do
{
xr=read(fd,b,20);
b[xr]='\0';
printf("%s",b);
}
while(xr==20);
close(fd);}
OUTPUT:
[kalai@localhost ~]$ cc sysread1.c
[kalai@localhost ~]$ ./a.out
rose
jasmine
lotus
________________
EX.NO:3a SIMULATE grep COMMAND USING C
PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
char fn[10],pat[10],temp[200];
FILE *fp;
printf("Enter file name\n");
scanf("%s",fn);
printf("Enter pattern to be searched\n");
scanf("%s",pat);
fp=fopen(fn,"r");
while(!feof(fp))
{
fgets(temp,1000,fp);
if(strstr(temp,pat))
printf("%s",temp);
}
fclose(fp);
}
OUTPUT:
[kalai@localhost ~]$ cc grep1.c
[kalai@localhost ~]$ ./a.out
Enter file name
grep1.c
Enter pattern to be searched
printf
printf("Enter file name\n");
printf("Enter pattern to be searched\n");
printf("%s",temp);
________________
EX.NO:3b SIMULATE cp COMMAND USING C
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
/*Start data transfer from src file to dest file till it reaches EOF*/
while((nbread = read(srcFD,buff,BUFF_SIZE)) > 0)
{
if(write(destFD,buff,nbread) != nbread)
printf("\nError in writing data to %s\n",argv[2]);
}
if(nbread == -1)
printf("\nError in reading data from %s\n",argv[1]);
if(close(srcFD) == -1)
printf("\nError in closing file %s\n",argv[1]);
if(close(destFD) == -1)
printf("\nError in closing file %s\n",argv[2]);
exit(EXIT_SUCCESS);
}
OUTPUT:
[kalai@localhost ~]$ cc cp1.c
[kalai@localhost ~]$ cat flower
ROse
lotus
jasmine
[kalai@localhost ~]$ cat pookul
cat: pookul: No such file or directory
[kalai@localhost ~]$ ./a.out flower pookul
[kalai@localhost ~]$ cat pookul
ROse
lotus
jasmine
________________
EX.NO:3c SIMULATE cat COMMAND USING C
PROGRAM:
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
main( int argc,char *argv[3] )
{
int fd,i;
char buf[2];
fd=open(argv[1],O_RDONLY,0777);
if(fd==-argc)
{
printf("file open error");
}
else
{
while((i=read(fd,buf,1))>0)
{
printf("%c",buf[0]);
}
close(fd);
}
}
OUTPUT:
[kalai@localhost ~]$ cc cat1.c
[kalai@localhost ~]$ ./a.out area1.sh
echo "Enter the radius: "
read r
p=3.14
a=`expr $p\*$r\*$r|bc`
echo "Area is $a"
________________
EX.NO: 4a AREA OF A CIRCLE
PROGRAM:
echo "Enter the radius: "
read r
p=3.14
a=`expr $p\*$r\*$r|bc`
echo "Area is $a"
OUTPUT:
[kalai@localhost ~]$ sh area1.sh
Enter the radius:
4
Area is 50.24
________________
EX.NO: 4b GREATEST OF THREE NUMBERS
PROGRAM:
echo "Enter three numbers:"
read a
read b
read c
if test $a -gt $b -a $a -gt $c
then
echo "A is largest"
elif test $b -gt $c
then
echo "B is largest"
else
echo "C is largest"
fi
OUTPUT:
[kalai@localhost ~]$ sh greatest3.sh
Enter three numbers:
4
6
9
C is largest
________________
EX.NO: 4c ARITHMETIC OPERATION USING SWITCH STATEMENT
PROGRAM:
echo "Enter two no.s"
read a
read b
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
echo "5. Exit"
echo "Choose an option"
read c
case $c in
1)s=`expr $a+$b|bc`;;
2)s=`expr $a-$b|bc`;;
3)s=`expr $a\*$b|bc`;;
4)s=`expr $a\$b|bc`;;
5)exit;;
esac
echo "Result is $s"
OUTPUT:
[kalai@localhost ~]$ sh arithcase.sh
Enter two no.s
4
2
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Choose an option
1
Result is 6
________________
EX.NO: 4d SUM OF DIGITS OF GIVEN NUMBER
PROGRAM:
echo "Enter a number"
read n
s=0
z=0
t=10
while test $n -gt $z
do
r=`expr $n % $t|bc`
n=`expr $n / $t|bc`
s=`expr $s + $r|bc`
done
echo "Sum of digits is: $s"
OUTPUT:
[kalai@localhost ~]$ sh sumofdigits.sh
Enter a number
345
Sum of digits is: 12
________________
EX.NO: 4e COMMAND SUBSTITUTION
PROGRAM:
echo "1)Display list of files"
a=`ls`
echo $a
echo
echo
echo "2)Display calendar"
b=`cal`
echo $b
echo
echo
echo "3)Display the date"
c=`date`
echo $c
echo
echo
echo "4)Display the file content"
f=`cat area1.sh`
echo $f
echo
OUTPUT:
[kalai@localhost ~]$ sh comsub.sh
1)Display list of files
aaa area1.sh area.sh arithcase.sh bank.c bankpy.py cat1.c comsub.sh cp1.c ddetect.c
fact1.sh file1 file.dat files files3 files4
2)Display calendar
February 2019 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28
PROGRAM:
#include <stdio.h>
main()
{
int i,bt[10],p[10],cp[10],j,k,wt[10],tat[10];
int n,t;
float atat,awt;
printf("\n\t First In First Out");
printf("\n\t No. Of Process : ");
scanf(" %d",&n);
printf(" \n\t Process Burst Time\n");
for(i=1;i<=n;i++)
{
printf("\t P%d\t",i);
scanf(" %d",&bt[i]);
}
wt[1]=0;
tat[1]=bt[1];
atat=tat[1];
awt=0;
for(i=2;i<=n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
awt+=wt[i];
atat+=tat[i];
}
printf("\n\t Process Burst-Time Wait-Time Turnaround-Time\n");
for(i=1;i<=n;i++)
printf("\n\t P%d \t %d \t\t %d \t\t%d\n",i,bt[i],wt[i],tat[i]);
printf("\n\t Average Turn Around Time : %f \n",atat/n);
printf("\n\t Average Waiting Time : %f \n",awt/n);
}
________________
OUTPUT:
[pec292@localhost ~]$ cc firstcome.c
[pec292@localhost ~]$ ./a.out
P1 5 0 5
P2 3 5 8
P3 8 8 16
PROGRAM:
#include <stdio.h>
main()
{
int i,bt[10],p[10],cp[10],j,k,wt[10],tat[10];
int n,t;
float atat,awt;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(bt[i]<bt[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
t=p[i];
p[i]=p[j];
p[j]=t;
}
}
}
wt[1]=0;
tat[1]=bt[1];
atat=tat[1];
awt=0;
for(i=2;i<=n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
awt+=wt[i];
atat+=tat[i];
}
________________
OUTPUT:
[kalai@localhost ~]$ cc sjb.c
[kalai@localhost ~]$ ./a.out
No. Of Process : 3
P2 2
P1 5
P3 8
Process Burst-Time Wait-Time Turnaround-Time
P2 2 0 2
P1 5 2 7
P3 8 7 15
PROGRAM:
#include <stdio.h>
main()
{
int i,bt[10],p[10],pr[10],cp[10],j,k,wt[10],tat[10];
int n,t;
float atat,awt;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(pr[i]<pr[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
t=p[i];
p[i]=p[j];
p[j]=t;
t=pr[i];
pr[i]=pr[j];
pr[j]=t;
}
}
}
printf("\n\t Executed In Following Order\n");
wt[1]=0;
tat[1]=bt[1];
atat=tat[1];
awt=0;
for(i=2;i<=n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
awt+=wt[i];
atat+=tat[i];
}
________________
OUTPUT:
[pec292@localhost ~]$ cc priority.c
[pec292@localhost ~]$ ./a.out
Priority Scheduling
No. Of Process : 3
For Process P1 :
Burst Time : 2
Priority : 1
For Process P2 :
Burst Time : 4
Priority : 3
For Process P3 :
Burst Time : 6
Priority : 2
P1 2 1
P3 6 2
P2 4 3
Process Burst-Time Wait-Time Turnaround-Time
P1 2 0 2
P3 6 2 8
P2 4 8 12
PROGRAM:
#include <stdio.h>
main()
{
int i,f[10],bt[10],p[10],cbt[10],j,k,wt[10],tat[10];
int n,t,q,flag;
float atat,awt;
for(i=1;i<=n;i++)
{
printf("\n\t P%d \t ",i);
scanf(" %d",&bt[i]);
cbt[i]=bt[i];
p[i]=i;
}
for(i=1;i<=n;i++)
{
wt[i]=0;
tat[i]=0;
f[i]=0;
}
do
{
for(i=1;i<=n;i++)
{
if(f[i]==0)
{
if(bt[i]<=q)
{
f[i]=1;
t=bt[i];
}
else
t=q;
bt[i]=bt[i]-t;
for(j=1;j<=n;j++)
{
if((j==i)||(f[j]==1))
continue;
wt[j]=wt[j]+t;
}//for j
}//if finish
}//for i
flag=0;
for(i=1;i<=n;i++)
if(f[i]==0)
flag=1;
}while(flag==1);
atat=awt=0;
for(i=1;i<=n;i++)
{
tat[i]=wt[i]+cbt[i];
atat+=tat[i];
awt+=wt[i];
}
printf("\n\t Process Burst-Time Wait-Time Turnaround-Time\n");
for(i=1;i<=n;i++)
printf("\n\t P%d \t %d \t\t %d \t\t%d\n",p[i],cbt[i],wt[i],tat[i]);
return 0;
}
OUTPUT:
[pec292@localhost ~]$ cc robin.c
[pec292@localhost ~]$ ./a.out
No. Of Process : 3
P2 8
P3 2
Time Quantum : 3
P1 5 5 10
P2 8 7 15
P3 2 6 8
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
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;
}
}
return 0;
}
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("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
OUTPUT:
-bash-4.1$ cc sema1.c
-bash-4.1$ ./a.out
1.Producer
2.Consumer
3.Exit
Enter your choice:1
MSGSEND.C
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct mymsg {
long mtype;
char data[50];
}message;
main() {
int qid,len;
qid=msgget((key_t)107,IPC_CREAT|0666);
printf("input message? ");
scanf("%s",message.data);
message.mtype=1;
len=strlen(message.data);
if(msgsnd(qid,&message,len,0) == -1) {
printf("MESSAGE FAILED");
exit(1);
}
else
printf("MESSAGE SENT");
}
________________
MSGRECEIVE.C
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct mymsg {
long mtype;
char data[50];
}b;
int main()
{
int qid;
qid=msgget((key_t)107,0666);
if(qid==-1)
{
printf("MESSAGE FAILED");
exit(0);
}
if(msgrcv(qid,&b,13,1,IPC_NOWAIT) == -1)
printf("MESSAGE FAILED");
else
printf("MESSAGE RECIEVED %s \n",b.data);
}
OUTPUT:
[kalai@localhost ~]$ cc -o send msgsend.c
[kalai@localhost ~]$ ./send
input message? hi
MESSAGE SENT[kalai@localhost ~]$ cc -o rece msgreceive.c
[kalai@localhost ~]$ ./rece
MESSAGE RECIEVED welcome
________________
EX.NO:7b IMPLEMENTING SHARED MEMORY USING PIPES
#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>
#include<string.h>
#define MAXLINE 100
int main(void){
int n,i;
int fd[2];
pid_t pid;
char line[MAXLINE],msg[MAXLINE];
if (pipe(fd) < 0)
{
printf("pipe error");
return 0;
}
if ((pid = fork()) < 0)
{
printf("fork error");
return 0;
}
else if (pid > 0)
{ /* parent */
close(fd[0]);
for(i=0;i<5;i++)
{
printf("\ninput string? ");
scanf("%s",msg);
write(fd[1], msg , strlen(msg));
sleep(1);
}
}
else
{ /* child */
close(fd[1]);
for(i=0;i<5;i++)
{
n = read(fd[0], line, MAXLINE);
write(STDOUT_FILENO, line, n);
}
}
exit(0);
}
OUTPUT:
[kalai@localhost ~]$ cc share.c
[kalai@localhost ~]$ ./a.out
input string? hi
hi
________________
EX.NO: 8 IMPLEMENTING BANKERS ALGORITHM FOR DEADLOCK AVOIDANCE
PROGRAM:
#include<stdio.h>
void main()
{
int k=0,output[10],d=0,t=0,ins[5],i;
int avail[5],allocated[10][5],need[10][5],MAX[10][5],pno,P[10],j,rz, count=0;
clrscr();
printf("\n Enter the number of resources : ");
scanf("%d", &rz);
printf("\n enter the max instances of each resources\n");
for(i=0;i<rz;i++)
{
avail[i]=0;
printf("%c= ",(i+97));
scanf("%d",&ins[i]);
}
printf("\n Enter the number of processes : ");
scanf("%d", &pno);
printf("\n Enter the allocation matrix \n ");
for(i=0;i<rz;i++)
printf(" %c",(i+97));
printf("\n");
for(i=0;i <pno;i++)
{
P[i]=i;
printf("P[%d] ",P[i]);
for(j=0;j<rz;j++)
{
scanf("%d",&allocated[i][j]);
avail[j]+=allocated[i][j];
}
}
if(d!=-1)
{
pno=d+1;
goto A;
}
printf("\t <");
for(i=0;i<k;i++)
printf(" P[%d] ",output[i]);
printf(">");
}
OUTPUT:
[kalai@localhost ~]$ cc bank.c
[kalai@localhost ~]$ ./a.out
PROGRAM:
#include<stdio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Deadlock Detection Algo ************\n");
input();
show();
cal();
return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resource instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
} }
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
} }
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
} }}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
for(i=0;i<n;i++)
{ finish[i]=0;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
if(finish[i]==1)
i=n;
}
} } } } }
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
dead[j]=i;
j++;
flag=1;
}
}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock
process are\n");
for(i=0;i<n;i++)
{
printf("P%d\t",dead[i]);
}
}
else
{
printf("\nNo Deadlock Occur");
}}
________________
OUTPUT:
[kalai@localhost ~]$ cc deaddetect.c
[kalai@localhost ~]$ ./a.out
********** Deadlock Detection Algo ************
Enter the no of Processes 3
Enter the no of resource instances 3
Enter the Max Matrix
3 6 8
4 3 3
3 4 4
Enter the Allocation Matrix
3 3 3
2 0 3
1 2 4
Enter the available Resources
1 2 0
Process Allocation Max Available
P1 3 3 3 3 6 8 1 2 0
P2 2 0 3 4 3 3
P3 1 2 4 3 4 4