Lab Exercise
Lab Exercise
DEPARTMENT OF CSE
REG.NO:
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.
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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
5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13
12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20
19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27
26 27 28 29 30 24 25 26 27 28 29 30 28 29 30
31
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1 1 2 3 4 5
5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12
12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19
19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26
26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30
30 31
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5
4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12
11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19
18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26
25 26 27 28 29 30 31 29 30 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)
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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 .
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
Cut:
[cse414@cc2linux ~]$ cut -c1-3 new
mul
eve
UNI
os
UNI
com
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
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
Pipes:
[cse414@cc2linux ~]$ who | wc -l
22
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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
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
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
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
OUTPUT:
[kalai@localhost ~]$ cc sysread1.c
[kalai@localhost ~]$ ./a.out
rose
jasmine
lotus
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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);
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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*/
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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"
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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);
}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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;
}
}
}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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];
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
pr[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:
[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++)
{
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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];
}
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;
}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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");
}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
#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);
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
OUTPUT:
[kalai@localhost ~]$ cc share.c
[kalai@localhost ~]$ ./a.out
input string? hi
hi
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];
}
}
avail[i]=ins[i]-avail[i];
}
printf("\n");
for(i=0;i <pno;i++)
{
printf("P[%d] ",i);
for(j=0;j<rz;j++)
scanf("%d", &MAX[i][j]);
}
printf("\n");
A: d=-1;
for(i=0;i <pno;i++)
{
count=0; t=P[i];
for(j=0;j<rz;j++)
{
need[t][j] = MAX[t][j]-allocated[t][j];
if(need[t][j]<=avail[j])
count++;
}
if(count==rz)
{
output[k++]=P[i];
for(j=0;j<rz;j++)
avail[j]+=allocated[t][j];
}
else
P[++d]=P[i];
}
if(d!=-1)
{
pno=d+1;
goto A;
}
printf("\t <");
for(i=0;i<k;i++)
printf(" P[%d] ",output[i]);
printf(">");
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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++)
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
{
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++)
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
{
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;
} } } } } }
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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");
}}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF CSE
REG.NO:
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
368
433
344
Enter the Allocation Matrix
333
203
124
Enter the available Resources
120
Process Allocation Max Available
P1 333 368 120
P2 203 433
P3 124 344