Os Lab Manual New - Merged
Os Lab Manual New - Merged
PRACTICAL RECORD
Name : ………………………………………………………..
Semester : …………………………………………………….…...
Course : ……………………………………….…………………
DHANALAKSHMI SRINIVASAN
UNIVERSITY
Name : ……………………………………………
Department : ……………………………………………
Semester : ……………………………………………
Certified that this is a Bonafide Record of the Practical work done for the
above course in Laboratory during the period …..………………………..
……………………………………………………………
AIM:
To study and execute Unix commands.
PROCEDURE:
Unix is security conscious, and can be used only by those persons who have an account.
Telnet (Telephone Network) is a Terminal emulator program for TCP/IP networks that enables
users to log on to remote servers.
To logon, type telnet server_ipaddress in run window.
User has to authenticate himself by providing username and password. Once verified, a greeting
and $ prompt appears. The shell is now ready to receive commands from the user. Options
suffixed with a hyphen (–) and arguments are separated by space.
GENERAL COMMANDS
Command Function
date Used to display the current system date and time.
date +%D Displays date only
date +%T Displays time only
date +% Y Displays the year part of date
date +% H Displays the hour part of time
cal Calendar of the current month
calyear Displays calendar for all months of the specified year
calmonth year Displays calendar for the specified month of the year
who Login details of all users such as their IP, Terminal No, User name,
Command Function
pwd Path of the present working directory
mkdirdir A directory is created in the given name under the current directory
FILE COMMANDS
Command Function
cat >filename To create a file with some contents. To end typing press
Ctrl+d.
The >symbol means redirecting output to a file. (<for input)
cat filename Displays the file contents.
cat >>filename Used to append contents to a file
cpsrc des Copy files to given location. If already exists, it will be overwritten
cp –i src des Warns the user prior to overwriting the destination file
cp –r src des Copies the entire directory, all its sub-directories and files.
mv old new To rename an existing file or directory. –i option can also be used
OUTPUT
GENERAL COMMANDS
[student@veccse ~]date
Sat May 16 06:10:34 UTC 2020
[student@veccse ~]date +%D
05/16/20
[student@veccse ~]date +%T
10:13:11
[student@veccse ~]date +%Y
2020
[student@veccse ~]date +%H
10
[student@veccse ~]cal
May 2020
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
[student@veccse ~]cal 2020
2020
January February March
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 6 7
5 6 7 8 9 10 11 2 3 4 5 6 7 8 8 9 10 11 12 13 14
12 13 14 15 16 17 18 9 10 11 12 13 14 15 15 16 17 18 19 20 21
19 20 21 22 23 24 25 16 17 18 19 20 21 22 22 23 24 25 26 27 28
26 27 28 29 30 31 23 24 25 26 27 28 29 29 30 31
April May June
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 2 1 2 3 4 5 6
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
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
[student@veccse ~]who
studentpts/1 May 16 10:05 (172.16.1.14)
[student@veccse ~]who am i
studentpts/1 May 16 10:05 (172.16.1.14)
[student@veccse ~]tty
/dev/pts/1 [student@veccse
~]uname Linux
[student@veccse ~]echo "hello"
hello
[student@veccse ~]echo $HOME
/home/student
[student@veccse ~]bc bc
1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with
ABSOLUTELY NO WARRANTY.
For details type `warranty'.
[student@veccse ~]man lp
lp(1) Easy Software Products lp(1) NAME
lp - print files cancel - cancel jobs SYNOPSIS
lp [ -E ] [ -c ] [ -d destination ] [ -h server ] [ -m ] [ -n num- copies [ -o option ] [ -q priority ] [ -s ]
[ -t title ] [ -H handling
] [ -P page-list ] [ file(s) ]
lp [ -E ] [ -c ] [ -h server ] [ -i job-id ] [ -n num-copies [ -o option ] [ -q priority ] [ -t title ] [ -H
handling ] [ -P page-list ] cancel [ -a ] [ -h server ] [ -u username ] [ id ] [ destination ] [
destination-id ]
DESCRIPTION
lpsubmits files for printing or alters a pending job. Use a filename of "-" to force printing from
the standard input.
cancelcancels existing print jobs. The -a option will remove all jobs from the specified
destination.
OPTIONS
The following options are recognized by lp:
[student@veccse ~]history
1 date
2 date +%D
3 date +%T
4 date +%Y
5 date +%H
6 cal
7 cal 2020
8 cal 7 2020
10 who
11 who am i
12 tty
13 uname
14 uname -r
15 uname -n
16 echo "helloi"
17 echo $HOME
18 bc
19 man lp
20 history
DIRECTORY COMMANDS
[student@veccse]$ pwd
/home/student
[student@veccse ~]mkdir san
[student@veccse ~]mkdir s1 s2
[student@veccse ~]ls
s1 s2 san [student@veccse
~]cd s1 [student@veccse s1]$
cd / [student@veccse /]$ cd . .
FILE COMMANDS
[student@vecit ~]$ cat>test
hi welcome operating systems lab
[student@vecit ~]$ cat test
hi welcome operating systems lab [student@vecit ~]$
cat>>test fourth semester [student@vecit ~]$ cat test
hi welcome operating systems lab fourth semester
[student@vecit ~]$ cat>test1
[student@vecit ~]$ cp test test1
[student@vecit ~]$ cat test1
hi welcome operating systems lab fourth semester [student@vecit ~]$ cp -
i test test1 cp: overwrite `test1'? y [student@vecit ~]$ cp -r test test1
[student@vecit ~]$ ls
s s2 san swap.sh temp.sh test TEST test1 [student@vecit
~]$ mv san san1 [student@vecit ~]$ ls
s s2 san1 swap.sh temp.sh test TEST test1
[student@vecit ~]$ mv test test1 san1 [student@vecit
~]$ mv -v san1 sannew
`san1' -> `sannew'
[student@vecit ~]$ ls
s s2 sannew swap.sh temp.sh TEST
[student@vecit ~]$ cmp test test1 cmp:
test: No such file or directory
.
EX:2A PROGRAM:
#include<stdio.h> main(int
arc,char*ar[])
{
int pid; char s[100]; pid=fork();
if(pid<0)
printf("error"); else
if(pid>0)
{
wait(NULL);
printf("\n Parent Process:\n"); printf("\n\tParent Process
id:%d\t\n",getpid()); execlp("cat","cat",ar[1],(char*)0);
error("can’t execute cat %s,",ar[1]);
}
else
{
}
}
printf("\nChild process:");
printf("\n\tChildprocess parent id:\t %d",getppid()); printf(s,"\n\tChild process id
:\t%d",getpid()); write(1,s,strlen(s));
printf(" ");
printf(" ");
printf(" "); execvp(ar[2],&ar[2]);
error("can’t execute %s",ar[2]);
OUTPUT:
[root@localhost ~]# ./a.out tst date Child process:
Child process id :
3137 Sat Apr 10 02:45:32 IST 2010
Parent Process:
Parent Process id:3136 sd
dsaASD[root@localhost ~]# cat tst sd dsaASD
EX.NO.2B)PROGRAM:
#include<sys/types.h>
#include<dirent.h>
#include<stdio.h>
main(int c, char* arg[])
{
DIR *d;
struct dirent *r; int i=0;
d=opendir(arg[1]);
printf("\n\t NAME OF ITEM \n");
while((r=readdir(d)) != NULL)
{
printf("\t %s \n",r->d_name); i=i+1;
}
printf("\n TOTAL NUMBER OF ITEM IN THAT DIRECTORY IS %d \n",i);
}
OUTPUT:
[root@localhost ~]# cc dr.c [root@localhost ~]#
./a.out lab_print
NAME OF ITEM pri_output.doc sjf_output.doc fcfs_output.doc rr_output.doc ipc_pipe_output.doc
pro_con_prob_output.doc
TOTAL NUMBER OF ITEM IN THAT DIRECTORY IS 8
EX.NO.3
temp[m]=’\0’;
for(m=0;m<strl
{
temp[m]=str[k+
m]
if(Strcmp(temp,argc[1])==0)
{
printf(“%s\n”,str); break;
}}
return(0); }
OUTPUT:-
[it3009@localhost ita]$ cat>3 the
lion
the king
the white
twitch the
water sun
apple
scanf("%s%d",p[i],&bt[i]);
printf("\nEnter the arrival-time:");
for(i=0;i<n;i++)
scanf("%d",&at[i]);
wt[0]=0;
for(i=1;i<=n;i++)
wt[i]=wt[i-1]+bt[i-1];
ft[0]=bt[0];
for(i=1;i<=n;i++)
ft[i]=ft[i-1]+bt[i];
printf("\n\n\t\t\tGANTT CHART\n");
printf("\n \n");
for(i=0;i<n;i++)
printf("|\t%s\t",p[i]);
printf("|\t\n");
printf("\n \n");
printf("\n");
for(i=0;i<n;i++)
printf("%d\t\t",wt[i]);
printf("%d",wt[n]+bt[n]);
printf("\n \n");
printf("\n");
for(i=0;i<n;i++)
wat[i]=wt[i]-at[i];
for(i=0;i<n;i++)
tat[i]=wat[i]-at[i];
printf("\n FIRST COME FIRST SERVE\n");
printf("\n Process Burst-time Arrival-time Waiting-time Finish-time Turnaround-
time\n");
for(i=0;i<n;i++)
printf("\n\n %d%s \t %d\t\t %d \t\t %d\t\t %d \t\t
%d",i+1,p[i],bt[i],at[i],wat[i],ft[i],tat[i]);
for(i=0;i<n;i++)
sum=sum+wat[i];
awt=sum/n;
for(i=0;i<n;i++)
sum1=sum1+bt[i]+wt[i];
att=sum1/n;
printf("\n\nAverage waiting time:%f",awt);
printf("\n\nAverage turnaround time:%f",att);
}
OUTPUT:
enter the number of process 3
Enter the process name and burst-time: p1 2
p2 3
p3 4
Enter the arrival-time:0 1 2
GANTT CHART
| p1 | p2 | p3 |
0 2 5 9
for(i=1;i<=n;i++)
{
scanf("%d\t %d\t %d",&p[i],&bt[i],&at[i]);
}
printf("\nPROCESS\tBURSTTIME\tARRIVALTIME");
for(i=1;i<=n;i++)
{
printf("\np%d\t%d\t\t%d",p[i],bt[i],at[i]);
}
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(bt[i]>bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp1=p[i];
p[i]=p[j];
p[j]=temp1;
}
}
if(i==1)
{
}
else
{
st[1]=0;
ft[1]=bt[1]; wt[1]=0;
st[i]=ft[i-1];
ft[i]=st[i]+bt[i];
wt[i]=st[i];
}
}
printf("\n\n\t\t\tGANTT CHART\n");
printf("\n \n");
for(i=1;i<=n;i++)
printf("|\tp%d\t",p[i]);
printf("|\t\n");
printf("\n \n");
printf("\n");
for(i=1;i<=n;i++)
printf("%d \t\t",wt[i]);
printf("%d",wt[n]+bt[n]);
printf("\n \n");
for(i=2;i<=n;i++)
wat[i]=wt[i]-at[i];
for(i=1;i<=n;i++)
tt[i]=wat[i]+bt[i]-at[i];
printf("\nPROCESS\tBURSTTIME\tARRIVALTIME\tWAITINGTIME\tT URNAROUNDTI
ME\n");
for(i=1;i<=n;i++)
{
printf("\np%d %5d %15d %15d %15d",p[i],bt[i],at[i],wat[i],tt[i]);
}
for(i=1,tot=0;i<=n;i++)
tot+=wt[i];
awt=(float)tot/n;
printf("\n\n\n AVERAGE WAITING TIME=%f",awt); for(i=1,tot=0;i<=n;i++)
tot+=tt[i];
att=(float)tot/n;
printf("\n\n AVERAGE TURNAROUND TIME=%f",att);
}
OUTPUT:
enter the no.of process3
enter the process number,burst time and arrival time
181
251
331
PROCESS BURSTTIME ARRIVALTIME WAITINGTIME TURNAROUNDTIME
p3 3 1 0 2
p2 5 1 2 6
p1 8 1 7 14
AVERAGE WAITING TIME=3.666667
AVERAGE TURNAROUND TIME=7.333333
EX.NO.5CPROGRAM:
#include<stdio.h> voidmain()
{
int ct=0,y[30],j=0,bt[10],cwt=0; int
tq,i,max=0,n,wt[10],t[10],at[10],tt[10],b[10]; float
a=0.0,s=0.0;
char p[10][10];
printf("\n enter the no of process:"); scanf("%d",&n);
printf("\nenter the time quantum");
scanf("%d",&tq);
printf("\nenter the process name,bursttime,arrival time");
for(i=0;i<n;i++)
{
scanf("%s",p[i]);
scanf("%d",&bt[i]); scanf("%d",&at[i]);
wt[i]=t[i]=0; b[i]=bt[i];
}
printf("\n\t\tGANTT CHART");
printf("\n \n"); for(i=0;i<n;i++)
{
if(max<bt[i])
max=bt[i];
}
while(max!=0)
{
for(i=0;i<n;i++)
{
if(bt[i]>0)
{
if(ct==0)
wt[i]=wt[i]+cwt;
else
wt[i]=wt[i]+(cwt-t[i]);
}
if(bt[i]==0) cwt=cwt+0;
else if(bt[i]==max)
{
if(bt[i]>tq)
{
cwt=cwt+tq;
}
printf("|\t%s",p[i]); bt[i]=0; y[j]=cwt;
j++;
}
t[i]=cwt;
}
ct=ct+1;
}
for(i=0;i<n;i++)
{
wt[i]=wt[i]-at[i];
a=a+wt[i];
tt[i]=wt[i]+b[i]-at[i];
s=s+tt[i];
}
a=a/n; s=s/n;
printf("\n ");
printf("\n0");
for(i=0;i<j;i++)
printf("\t%d",y[i]); printf("\n");
printf("\n "); printf("\n\t\t ROUND ROBIN\n");
printf("\n Process Burst-time Arrival-time Waiting-time Turnaround-
time\n");
for(i=0;i<n;i++)
printf("\n\n %d%s \t %d\t\t %d \t\t %d\t\t %d", i+1, p[i], b[i], at[i], wt[i], tt[i]);
printf("\n\nAvg waiting time=%f",a);
printf("\n\nAvgturn around time=%f",s);
}
OUTPUT:
enter the no of process:3 enter the
time quantum2
enter the process name, bursttime, arrival time
p1 2 0
p2 3 1
p3 4 2
GANTT CHART
0 2 4 6 7 9
ROUND ROBIN
for(i=0;i<np;i++)
scanf("%s%d%d",p[i],&bt[i],&pr[i]);
printf("\nEnter the arrival-time:"); for(i=0;i<np;i++)
scanf("%d",&at[i]);
for(i=0;i<np;i++)
for(j=i+1;j<np;j++)
{
if(pr[i]>pr[j])
{
x=pr[j];
pr[j]=pr[i];
pr[i]=x;
strcpy(y,p[j]);
strcpy(p[j],p[i]);
strcpy(p[i],y);
z=bt[j]; b
t[j]=bt[i];
bt[i]=z;
}
}
wt[0]=0;
for(i=1;i<=np;i++)
wt[i]=wt[i-1]+bt[i-1];
ft[0]=bt[0]; for(i=1;i<np;i++)
ft[i]=ft[i-1]+bt[i];
printf("\n\n\t\tGANTT CHART\n");
printf("\n \n");
for(i=0;i<np;i++)
printf("|\t%s\t",p[i]);
printf("|\t\n");
printf("\n \n");
printf("\n");
for(i=0;i<=np;i++)
printf("%d\t\t",wt[i]);
printf("\n \n");
printf("\n");
for(i=0;i<np;i++)
wat[i]=wt[i]-at[i];
for(i=0;i<np;i++)
tat[i]=wat[i]-at[i]; printf("\nPRIORITY
SCHEDULING:\n");
printf("\nProcess Priority Burst-time Arrival-time Waiting-time Turnaround- time");
for(i=0;i<np;i++)
printf("\n\n%d%s\t%d\t\t%d\t\t%d\t%d\t\t%d",i+1,p[i],pr[i],bt[i],a t[i],wt[i],tat[i]);
for(i=0;i<np;i++)
sum=sum+wat[i];
awt=sum/np; for(i=0;i<np;i++)
sum1=sum1+tat[i];
att=sum1/np;
printf("\n\nAverage waiting time:%f",awt); printf("\n\nAverageturn around time
is:%f",att);
}
OUTPUT:
Enter the number of process3
Enter the process, burst-time and priority: p1 3 3
p2 4 2
p3 5 1
| p3 | p2 | p1 |
0 5 9 12
PRIORITY SCHEDULING:
Process Priority Burst-time Arrival-time Waiting-time Turnaround-time
p3 1 5 0 0 0
p2 2 4 1 5 3
p1 3 3 2 9 5
Average waiting time: 3.666667 Average
turnaround time is: 2.666667
EX.NO:6PROGRAM :
#define BUFFERSIZE 10
int mutex,n,empty,full=0,item,item1; int
buffer[20];
int in=0,out=0,mutex=1; void
wait(int s)
{
while(s<0)
{
printf(“\nCannot add an item\n”);
exit(0);
}
s--;
}
void signal(int s)
{
s++;
}
void producer()
{
do
{
wait (empty);
wait(mutex);
printf(“\nEnter an item:”);
scanf(“%d”,&item);
buffer[in]=item;
in=in+1;
signal(mutex);
signal(full);
}
while(in<n);
}
void consumer()
{
do
{
wait(full); wait(mutex);
item1=buffer[out];
printf(“\nConsumed item =%d”,item1); out=out+1;
signal(mutex);
signal(empty);
}
while(out<n);
}
void main()
{
printf(“Enter the value of n:”);
scanf(“%d “,&n);
empty=n; while(in<n)
producer();
while(in!=out)
consumer();
}
OUTPUT:
$ cc prco.c
$ a.out
Enter the value of n :3 Enter
the item:2
Enter the item:5 Enter
the item:9 consumed
item=2 consumed
item=5 consumed
item=9
$
EX.NO:7SHARED MEMORY FOR WRITER PROCESS
#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h> using
namespace std; int main()
{
// ftok to generate unique key key_t key =
ftok("shmfile",65);
// shmget returns an identifier in shmid
int shmid = shmget(key,1024,0666|IPC_CREAT);
// shmat to attach to shared memory
char *str = (char*) shmat(shmid,(void*)0,0);
printf("Write Data : ");
gets(str);
printf("Data written in memory: %s\n",str);
//detach from shared memory shmdt(str);
return 0;
{
avail[j]=0;
total=0;
for(i=1;i<= pno;i++)
{
total+=allocated[i][j];
}
avail[j]=tres[j]-total;
work[j]=avail[j];
printf(" %d \t",work[j]);
}
do
{
for(i=1;i<= pno;i++)
{
for(j=1;j<= rno;j++)
{
need[i][j]=max[i][j]-allocated[i][j];
}
}
printf("\n Allocated matrix Max need");
for(i=1;i<= pno;i++)
{
printf("\n"); for(j=1;j<=
rno;j++)
{
printf("%4d",allocated[i][j]);
}
printf("|"); for(j=1;j<=
rno;j++)
{
printf("%4d",max[i][j]);
}
printf("|"); for(j=1;j<=
rno;j++)
{
printf("%4d",need[i][j]);
}
}
prc=0;
for(i=1;i<= pno;i++)
{
if(flag[i]==0)
{
prc=i;
for(j=1;j<= rno;j++)
{
if(work[j]< need[i][j])
{
prc=0;
break;
}
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\n Process %d completed",i);
count++;
printf("\n Available matrix:"); for(j=1;j<=
rno;j++)
{
work[j]+=allocated[prc][j];
allocated[prc][j]=0;
max[prc][j]=0; flag[prc]=1;
printf(" %d",work[j]);
}
}
}while(count!=pno&&prc!=0);
if(count==pno)
printf("\nThe system is in a safe state!!");
else
printf("\nThe system is in an unsafe state!!");
getch();
}
OUTPUT
Enter number of process:5 Enter
number of resources:3
Enter total numbers of each resources:10 5 7 Enter Max
resources for each process:
for process 1: 7 5 3
for process 2: 3 2 2
for process 3: 9 0 2
for process 4: 2 2 2
for process 5: 4 3 3
Enter allocated resources for each process: for
process 1: 0 1 0
for process 2: 3 0 2
for process 3: 3 0 2
for process 4: 2 1 1
for process 5: 0 0 2
available resources:
2 3 0
0 1 0| 7 5 3| 7 4 3
3 0 2| 3 2 2| 0 2 0
3 0 2| 9 0 2| 6 0 0
2 1 1| 2 2 2| 0 1 1
0 0 2| 4 3 3| 4 3 1
Process 2 completed
Available matrix: 5 3 2 Allocated
matrix Max need
0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
2 1 1| 2 2 2| 0 1 1
0 0 2| 4 3 3| 4 3 1
Process 4 completed
Available matrix: 7 4 3 Allocated
matrix Max need
0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 1 completed
Available matrix: 7 5 3 Allocated
matrix Max need
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 3 completed
Available matrix: 10 55
Allocated matrix Max need
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 5 completed
Available matrix: 10 57
The system is in a safe state!!
EX.NO:9PROGRAM
#include<stdio.h>
#include<conio.h> int
max[100][100]; i nt
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();
getch(); 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;
}
//find need matrix
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;
}
//printf("\nP%d",i);
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)
{
}
else
{
}
}
344
Enter the allocation matrix 3 3 3
203
124
Enter the available resources 1 2 0
Process allocation max available
P1 333 368 120
P2 203 433
P3 124 344
System is in deadlock and deadlock process are P1 P2
P3
EX.NO.10Program
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h> int
MAX = 10;
int count = 1;
pthread_mutex_t thr;
pthread_cond_t cond; void
*even(void *arg){
while(count < MAX) {
pthread_mutex_lock(&thr); while(count % 2
!= 0) {
pthread_cond_wait(&cond, &thr);
}
printf("%d ", count++);
pthread_mutex_unlock(&thr);
pthread_cond_signal(&cond);
}
pthread_exit(0);
}
void *odd(void *arg){
while(count < MAX) {
pthread_mutex_lock(&thr); while(count
% 2 != 1) {
pthread_cond_wait(&cond, &thr);
}
printf("%d ", count++);
pthread_mutex_unlock(&thr);
pthread_cond_signal(&cond);
}
pthread_exit(0);
}
int main(){ pthread_t
thread1; pthread_t
thread2;
pthread_mutex_init(&thr, 0);
pthread_cond_init(&cond, 0); pthread_create(&thread1, 0,
&even, NULL); pthread_create(&thread2, 0, &odd,
NULL); pthread_join(thread1, 0);
pthread_join(thread2, 0);
pthread_mutex_destroy(&thr);
pthread_cond_destroy(&cond); return 0;
}
Output
if we run above program then it will generate following output 1 2 3 4 5 6
7 8 9 10
.
Ex.NO: 11 PROGRAM:
#include<stdio.h> main()
{
int p[10],np,b[10],nb,ch,c[10],d[10],alloc[10],flag[10],i,j;
printf("\nEnter the no of process:");
scanf("%d",&np);
printf("\nEnter the no of blocks:"); scanf("%d",&nb);
printf("\nEnter the size of each process:");
for(i=0;i<np;i++)
{
printf("\nProcess %d:",i);
scanf("%d",&p[i]);
}
}
}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
printf("\n\nProcess %d of size %d is not allocated",i,p[i]);
}
break;
case 2: printf("\nBest Fit\n");
for(i=0;i<nb;i++)
{
for(j=i+1;j<nb;j++)
{
if(c[i]>c[j])
{
int temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
printf("\nAfter sorting block sizes:");
for(i=0;i<nb;i++)
printf("\nBlock %d:%d",i,c[i]);
for(i=0;i<np;i++)
{
for(j=0;j<nb;j++)
{
if(p[i]<=c[j])
{
alloc[j]=p[i];printf("\n\nAlloc[%d]",all oc[j]);
printf("\n\nProcess %d of size %d is allocated in
block %d of size
%d",i,p[i],j,c[j]); flag[i]=0,c[j]=0;break;
flag[i]=1;
}
else
}
}
for(i=0;i<np;i++)
{
if(flag[i]!=0)
printf("\n\nProcess %d of size %d is not
allocated",i,p[i]);
}
break;
OUTPUT
Enter the no of process:3 Enter the
no of blocks:3
Enter the size of each process:
Process 0:100
Process 1:150
Process 2:200
Enter the block sizes:
Block 0:300
Block 1:350
Block 2:200
1.First fit 2.Best fit 3.Worst fit
Enter your choice:1 Alloc[100]
Process 0 of size 100 is allocated in block 0 of size 300 Alloc[150]
Process 1 of size 150 is allocated in block 1 of size 350 Alloc[200]
Process 2 of size 200 is allocated in block 2 of size 200 Enter your
choice:2
Best Fit
After sorting block sizes are:
Block 0:200
Block 1:300
Block 2:350
Alloc[100]
Process 0 of size 100 is allocated in block:0 of size:200 Alloc[150]
Process 1 of size 150 is allocated in block:1 of size:300 Alloc[200]
Process 2 of size 200 is allocated in block:2 of size:350 enter your
choice:3
Worst Fit
After sorting block sizes are:
Block 0:350
Block 1:300
Block 2:200
Alloc[100]
Process 0 of size 100 is allocated in block 0 of size 350 Alloc[150]
Process 1 of size 150 is allocated in block 1 of size 300 Alloc[200]
Process 2 of size 200 is allocated in block 2 of size 200 Enter your
choice:6
Invalid Choice…!
EX.NO:12PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int ms, ps, nop, np, rempages, i, j, x, y, pa, offset; int
s[10], fno[10][20];
clrscr();
printf("\nEnter the memory size -- ");
scanf("%d",&ms);
printf("\nEnter the page size -- "); scanf("%d",&ps);
nop = ms/ps;
printf("\nThe no. of pages available in memory are -- %d ",nop); printf("\nEnter number of
processes -- ");
scanf("%d",&np);
rempages = nop;
for(i=1;i<=np;i++)
("\nThe Physical Address is -- %d",pa);
{
printf("\nEnter no. of pages required for p[%d]-- ",i);
scanf("%d",&s[i]);
if(s[i] >rempages)
{
printf("\nMemory is Full"); break;
}
rempages = rempages - s[i]; printf("\nEnter
pagetable for p[%d] --- ",i); for(j=0;j<s[i];j++)
scanf("%d",&fno[i][j]);
}
printf("\nEnter Logical Address to find Physical Address ");
printf("\nEnter process no. and pagenumber and offset -- "); scanf("%d
%d %d",&x,&y, &offset);
if(x>np || y>=s[i] || offset>=ps)
printf("\nInvalid Process or Page Number or offset");
else
{
pa=fno[x][y]*ps+offset;
} printf
getch();
}
OUTPUT
Enter the memory size – 1000
Enter the page size -- 100
The no. of pages available in memory are 10
Enter number of processes -- 3
Enter Logical Address to find Physical Address Enter process no. and pagenumber and offset
2
3
60
The Physical Address is --------- 760
EX.NO:13APROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int i, j, k, f, pf=0, count=0, rs[25], m[10], n;
clrscr();
printf("\n Enter the length of reference string -- ");
scanf("%d",&n);
printf("\n Enter the reference string -- ");
for(i=0;i<n;i++)
scanf("%d",&rs[i]); printf("\n
Enter no. of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
m[i]=-1;
printf("\n The Page Replacement Process is -- \n"); for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
if(m[k]==rs[i])
break;
}
if(k==f)
{
m[count++]=rs[i];
pf++;
}
for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\tPF No. %d",pf);
printf("\n");
if(count==f)
count=0;
}
printf("\n The number of Page Faults using FIFO are %d",pf); getch();
}
OUTPUT
Enter the length of reference string – 20
Enter the reference string -- 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 Enter no. of
frames -- 3
The Page Replacement Process is –
7 -1 -1 PF No. 1
7 0 -1 PF No. 2
7 0 1 PF No. 3
2 0 1 PF No. 4
2 0 1
2 3 1 PF No. 5
2 3 0 PF No. 6
4 3 0 PF No. 7
4 2 0 PF No. 8
4 2 3 PF No. 9
0 2 3 PF No. 10
0 2 3
0 2 3
0 1 3 PF No. 11
0 1 2 PF No. 12
0 1 2
0 1 2
7 1 2 PF No. 13
7 0 2 PF No. 14
7 0 1 PF No. 15
The number of Page Faults using FIFO are 15
EX.NO:13BPROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int i, j , k, min, rs[25], m[10], count[10], flag[25], n, f, pf=0, next=1; clrscr();
printf("Enter the length of reference string -- ");
scanf("%d",&n);
printf("Enter the reference string -- ");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]); flag[i]=0;
}
printf("Enter the number of frames -- ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0;
m[i]=-1;
}
printf("\nThe Page Replacement process is -- \n"); for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
flag[i]=1;
count[j]=next;
next++;
}
}
if(flag[i]==0)
{
for(j=0;j<f;j++)
printf("%d\t", m[j]); if(flag[i]==0)
printf("PF No. -- %d" , pf);
printf("\n");
}
printf("\nThe number of page faults using LRU are %d",pf); getch();
}
OUTPUT
Enter the length of reference string -- 20
Enter the reference string -- 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 Enter the
number of frames -- 3
The Page Replacement process is --
7 -1 -1 PF No. -- 1
7 0 -1 PF No. -- 2
7 0 1 PF No. -- 3
2 0 1 PF No. -- 4
EX.NO:13CPROGRAM
#include<stdio.h>
#include<conio.h>
main()
{
int rs[50], i, j, k, m, f, cntr[20], a[20], min, pf=0; clrscr();
printf("\nEnter number of page references -- "); scanf("%d",&m);
printf("\nEnter the reference string -- ");
for(i=0;i<m;i++)
scanf("%d",&rs[i]);
printf("\nEnter the available no. of frames -- "); scanf("%d",&f);
for(i=0;i<f;i++)
{
cntr[i]=0;
a[i]=-1;
}
Printf(“\nThe Page Replacement Process is – \n“); for(i=0;i<m;i++)
{
for(j=0;j<f;j++)
if(rs[i]==a[j])
{
cntr[j]++;
break;
}
if(j==f)
{
min = 0;
for(k=1;k<f;k++)
if(cntr[k]<cntr[min]) min=k;
a[min]=rs[i];
cntr[min]=1;
pf++;
}
printf("\n");
for(j=0;j<f;j++)
printf("\t%d",a[j]);
if(j==f)
printf(“\tPF No. %d”,pf);
}
printf("\n\n Total number of page faults -- %d",pf); getch();
}
OUTPUT
Enter number of page references -- 10
Enter the reference string -- 123452525143
Enter the available no. of frames 3
The Page Replacement Process is –
1 -1 -1 PF No. 1
1 2 -1 PF No. 2
1 2 3 PF No. 3
4 2 3 PF No. 4
5 2 3 PF No. 5
5 2 3
5 2 3
5 2 1 PF No. 6
5 2 4 PF No. 7
5 2 3 PF No. 8
Total number of page faults ------------- 8
EX.NO.14
#include<stdio.h> struct
{
char dname[10],fname[10][10]; int fcnt;
}dir;
void main()
{
int i,ch; char
f[30]; clrscr();
dir.fcnt = 0;
printf("\nEnter name of directory -- "); scanf("%s",
dir.dname);
while(1)
{
printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter your choice -- ");
scanf("%d",&ch); switch(ch)
{
case 1: printf("\n Enter the name of the file -- "); scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++; break;
case 2: printf("\n Enter the name of the file -- "); scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]); break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f); else
dir.fcnt--; break;
case 3: printf("\n Enter the name of the file -- "); scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f); break;
case 4: if(dir.fcnt==0) printf("\n
Directory Empty"); else
{
printf("\n The Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
getch();
}
OUTPUT:
Enter name of directory -- CSE
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 1 Enter the
name of the file -- A
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 1
Enter the name of the file -- B
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 1 Enter the
name of the file -- C
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 4 The Files are
-- A B C
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 3 Enter the
name of the file – ABC
File ABC not found
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 2
Enter the name of the file – B File B is
deleted
1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 5
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f); for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f); goto
jmp1;
}
}
printf("File %s not found",f); goto jmp1;
}
}
printf("Directory %s not found",d); jmp1:
break;
case 5: if(dcnt==0) printf("\nNo
Directory's "); else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break; default:exit(0);
}
}
getch();
}
OUTPUT:
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 1
Enter name of directory -- DIR1 Directory
created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 1 Enter name of
directory -- DIR2
Directory created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 2
Enter name of the directory – DIR1 Enter name
of the file -- A1
File created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 2
Enter name of the directory – DIR1 Enter name
of the file -- A2
File created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 2
Enter name of the directory – DIR2 Enter name
of the file -- B1
File created
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 5 Directory Files
DIR1 A1 A2 DIR2
B1
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 4
Enter name of the directory – DIR Directory not found
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 3 Enter name of the
directory – DIR1
Enter name of the file -- A2
File A2 is deleted
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice – 6
EX.NO:15A
#include<stdio.h> main()
{
int f[50],i,st,j,len,c,k;
clrscr(); for(i=0;i<50;i++)
f[i]=0;
X:
printf("\n Enter the starting block & length of file"); scanf("%d%d",&st,&len);
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("Block already allocated"); break;
}
if(j==(st+len))
printf("\n the file is allocated to disk"); printf("\n if u
want to enter more files?(y-1/n-0)"); scanf("%d",&c);
if(c==1)
goto X;
else
getch();
}
OUTPUT
Output: Enter the starting block & length of file 4 10 4 ->1
5->1
6->1
7->1
8->1
9->1
10->1
11->1
12->1
13->1
The file is allocated to disk
If you want to enter more files? (Y-1/N-0)
EX.NO:15B
#include<stdio.h> main()
{
int f[50],p,i,j,k,a,st,len,n,c; clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks that are already allocated");
scanf("%d",&p);
printf("\nEnter the blocks no.s that are already allocated"); for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X: printf("Enter the starting index block & length"); scanf("%d%d",&st,&len);
k=len;
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
getch( );
}
OUTPUT: