036 - Os - Lab
036 - Os - Lab
036 - Os - Lab
: 1 UNIX COMMANDS
DATE :
AIM
To execute different Unix commands.
COMMANDS
1) Command to print the login name of the user
[cs15090@CSDB312 ~]$ logname
cs15090
9) Create a file
[cs15090@CSDB312 ~]$ cat>f1
this is file1
[cs15090@CSDB312 ~]$ cat f1
this is file1
12) List all the files and directories in the current directory
[cs15090@CSDB312 ~]$ ls
bst.cpp elen.c factorial.cpp filename.c fribin.cpp Music
Pictures six.c test2.cpp time.cpp two.c
a.out Desktop eleven.c fifteen.c five.c friunary.cpp nine.c
Public sixteen.c test.cpp timefinal.cpp upper.cpp
armstrong.cpp Documents f1 fifty.c forty.c matrixadd.cpp
nineteen.c sam streverse.cpp thirteen.c twelve.cVideos
avg.cpp Downloads f2 fiftyone.c fortyone.c maximum.cpp one.c
seven.c student.cpp thirty twenty virtual.cpp
binary.cpp eight.c f3 file1name.c four.c multilevel.cpp
opover.cpp seven.cvi Templates thirty.c twenty.c
book.cpp eighteen.c f4 filename2.c fourteen.c multiple.cpp
palindrome.cpp seventeen.c ten.c three.c twentyfive.c
32) To view all the files starting with the given letter
[cs15090@CSDB312 ~]$ cat>f3
this is file f3
[cs15090@CSDB312 ~]$ cat>f33
this is file f33
[cs15090@CSDB312 ~]$ cat f3
this is file f3
[cs15090@CSDB312 ~]$ cat f33
this is file f33
[cs15090@CSDB312 ~]$ ls f3*
f3 f33
33) To view all the files starting with either of the given letters (aeiou)
[cs15090@CSDB312 ~]$ ls [aeiou]*
a a1 a.out armstrong.cpp avg.cpp eight.c eighteen.c elen.c eleven.c one.c
opover.cpp os1.txt os2.txt upper.cpp
34) To view all the files starting with the letters other than (aeiou)
[cs15090@CSDB312 ~]$ls –L[a,e,i,o,u]*
bst.cpp factorial.cpp filename.c fribin.cpp Music Pictures
six.c test2.cpp time.cpp two.c Desktop fifteen.c five.c
friunary.cpp nine.c Public sixteen.c test.cpp timefinal.cpp
Documents f1 fifty.c forty.c matrixadd.cpp nineteen.c sam
streverse.cpp thirteen.c twelve.c Videos
Downloads f2 fiftyone.c fortyone.c maximum.cpp seven.c
student.cpp thirty twenty virtual.cpp binary.cpp f3
file1name.c four.c multilevel.cpp seven.cvi Templates thirty.c
twenty.c book.cpp f4 filename2.c fourteen.c multiple.cpp
palindrome.cpp seventeen.c ten.c three.c twentyfive.c
38) Give all rights for the user,for group give only write and execute permission
[cs15090@cs4210 ~]$ chmod 730 f11
[cs15090@cs4210 ~]$ls –l f11
-rwx-wx---l cs15090 cs14 feb 10 10:30 f11
41) Calculator
[cs15090@cs4210 ~]$ bc
bc 1.06.95
Reg. No. : 190501036 Page No. :
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2+3
5
quit
52) Sort the contents of two files and store it in another file
[cs15090@cs4210 ~]$ cat>l1
Ghj
Uio
[cs15090@cs4210 ~]$ cat>l2
How r u
[cs15090@cs4210 ~]$ sort l1l2>>l3
[cs15090@cs4210 ~]$ cat l3
Ghj
How r u
Uio
1 a 100
2 b 101
3 c 102
4 d 103
[cs15090@cs4210 ~]$cut –c 1 s1
1
2
3
4
60) Search for the pattern ignoring case and starting with either of [a-f] in the file and print it
[cs15090@cs4210 ~]$grep [a-f]?h –i –n f6:
f6:hai friends.1
f6:hai.how r u?2
f6:fine 3
61) Search for the pattern ending with either of [s-z] in the file and print it along with the line numbers
[cs15090@cs4210 ~]$grep –n [s-z]$f6
f6:hai friends.1
f6:hai.how r u?2
64) Count the number of times the particular data is present in the file
[cs15090@cs4210 ~]$cat f7
hai
hai
Reg. No. : 190501036 Page No. :
fine
[cs15090@cs4210 ~]$uniq –d f7
hai 2
fine 1
RESULT
Thus the Unix commands has been executed successfully
AIM
To find sum of two numbers using Shell programming.
ALGORITHM
i) START
ii) Read two values
iii) c is equals to a + b
iv) then print c
v) STOP
PROGRAM
#!/bin/sh
echo Enter two numbers :
read a b
c=`expr $a + $b`
echo Sum of $a and $b is $c
RESULT
Hence, the program to find sum of two numbers using Shell programming has been executed successfully.
AIM
To swap two numbers using Shell Programming.
ALGORITHM
i) START
ii) Read two numbers
iii) temp is equal to a
iv) a is equal to b
v) b is equal to temp
vi) Then print both values
vii) STOP
PROGRAM
#!/bin/sh
echo Enter two numbers :
read a b
Reg. No. : 190501036 Page No. :
temp=$a
a=$b
b=$temp
echo Numbers after swapping : $a $b
RESULT
Hence, the program to swap two numbers using Shell Programming has been executed successfully.
AIM
To find the largest of three numbers using Shell Programming.
ALGORITHM
i) START
ii) Read three values
iii) Check if, a is greater than b and a is greater than c
iv) If yes, print a is greatest
v) Else check if, b is greater than c
vi) If yes, print b is greatest
vii) Else, print c is greatest
viii) STOP
PROGRAM
#!/bin/sh
echo Enter 3 numbers :
read a b c
if(( ($a > $b) && ($a > $c) ))
then
echo $a is greatest
elif(($b > $c))
then
echo $b is greatest
else
echo $c is greatest
fi
RESULT
Hence, the program to find the largest of three numbers using Shell Programming has been executed
successfully.
D) FACTORIAL OF A NUMBER
AIM
To find the factorial of a number using Shell Programming.
Reg. No. : 190501036 Page No. :
ALGORITHM
i) START
ii) Read a value
iii) Initialize f is equal to 1
iv) Open loop for condition i is less than equal to n
v) When i is equal to 0 and increment i
vi) f is equal to f * i
vii) then print f
viii) STOP
PROGRAM
#!/bin/sh
echo Enter a number :
read n
f=1
for((i=1; i<=n; i++))
do
f=`expr $f \* $i`
done
echo "The factorial of $n is $f"
RESULT
Hence, the program to find the factorial of a number using Shell Programming has been executed successfully.
E) DAYS IN A WEEK
AIM
To find days in a week using Shell Programming.
ALGORITHM
i) START
ii) Read a value
iii) Check if, n is equal to 1
iv) Then print Sunday
v) Check if, n is equal to 2
vi) Then print Monday
vii) Check if, n is equal to 3
viii) Then print Tuesday
ix) Check if, n is equal to 4
x) Then print Wednesday
xi) Check if, n is equal to 5
xii) Then print Thursday
xiii) Check if, n is equal to 6
xiv) Then print Friday
xv) Check if, n is equal to 7
xvi) Then print Saturday
xvii) else, print invalid
xviii) STOP
PROGRAM
Reg. No. : 190501036 Page No. :
#!/bin/sh
echo Enter a number :
read n
if (( $n == 1))
then
echo $n=sunday
elif (( $n == 2 ))
then
echo $n=monday
elif (( $n == 3 ))
then
echo $n=tuesday
elif (( $n == 4 ))
then
echo $n=wednesday
elif (( $n == 5 ))
then
echo $n=Thursday
elif (( $n == 6 ))
then
echo $n=friday
elif (( $n == 7 ))
then
echo $n=saturday
else
echo $n=invalid
fi
RESULT
Hence, the program to find days in a week using Shell Programming has been executed successfully.
F) FIBONACCI SERIES
AIM
To find Fibonacci series using Shell Programming.
ALGORITHM
i) START
ii) Read a value
iii) Initialize a is equal 0 and b is equal to 1
iv) Print a and b
v) Enter loop, i is less than or equal to n
vi) When i = 3 and i++
vii) print c
viii) c is equal to a + b
ix) a is equal to b and b is equal to c
x) STOP
PROGRAM
#!/bin/sh
echo Enter the limit
read n
a=0
Reg. No. : 190501036 Page No. :
b=1
echo The fibonacci series
echo $a
echo $b
for((i=3; i<=n; i++))
do
c=`expr $a + $b`
echo $c
a=$b
b=$c
done
RESULT
Hence, the program to find Fibonacci series using Shell Programming has been executed successfully.
G) AMSTRONG NUMBER
AIM
To find whether the number is Armstrong or not using Shell Programming.
ALGORITHM
i) START
ii) Read a value
iii) m is equal to n and s is equal to 0
iv) enter loop for condition n is less than 0
v) if yes, p is equal to n % 10
vi) c is equal to p*p*p
vii) s is equal to s + c
viii) n is equal to n / 10
ix) check if, s is equal to m
x) if yes, print Armstrong
xi) else, print Not Armstrong
xii) STOP
PROGRAM
#!/bin/sh
echo Enter the number
read n
m=$n
s=0
while(($n > 0))
do
p=`expr $n % 10`
c=`expr $p \* $p \* $p`
s=`expr $s + $c`
n=`expr $n / 10`
done
if(($s == $m))
then
echo Armstrong
else
echo Not Armstrong
fi
RESULT
Hence, the program to find whether the number is Armstrong or Not using Shell Programming has been
executed successfully.
DATE :
AIM
To implement system calls of UNIX operating system fork, exec, getpid, exit, wait, close, stat,
opendir, readdir.
ALGORITHM
1) Start
2) In main initialise all the vlues and call the sem_init function twice
with paramet wrt,0,1 and mutex,0,1.
3) Then call pthread_create function four times with parameters.
4)Call the pthread_join function four times with parameter.
5) In w function call sem_wait function with wrt as parameter.
6) Then get the value and print the shared value and call sem_post with
parameter of wrt.
7) In r function initialise rr as int of a then call sem_wait of mutex.
8)Increment readcount and check if it is equal to 1 then sem_wait of wrt
as a parameter.
9)Then print all the values and again call sem_wait of mutex.
10)Decrement readcount value and if it is equal to 0 then sem_post of wrt
as a paramter.
11)Call sem_post with mutex as a parameter.
12)Stop.
PROGRAM
Stat()
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
int main(void)
{
char *path,path1[10];
struct stat *nfile;
nfile=(struct stat *) malloc (sizeof(struct stat));
printf("enter name of file whose stsistics has to");
scanf("%s",path1);
stat(path1,nfile);
printf("user id %d\n",nfile->st_uid);
printf("block size :%d\n",nfile->st_blksize);
printf("last access time %d\n",nfile->st_atime);
printf("time of last modification %d\n",nfile->st_atime);
printf("porduction mode %d \n",nfile->st_mode);
printf("size of file %d\n",nfile->st_size);
printf("nu,mber of links:%d\n",nfile->st_nlink);
}
Getpid()
#include<stdio.h>
int main()
{
int pid;
pid=getpid();
printf("process ID is %d\n",pid);
pid=getppid();
printf("parent process ID id %d\n",pid);
}
Fork()
#include<stdio.h>
int main(void)
{
int fork(void),value;
value=fork();
printf("main:value =%d\n",value);
return 0;
}
Opendir,readdir
#include<stdio.h>
#include<dirent.h>
struct dirent *dptr;
int main(int argc,char *argv[])
{
char buff[256];
DIR *dirp;
printf("\n\nEnter directory name");
scanf("%s",buff);
if((dirp=opendir(buff))==NULL)
{
printf("Error");
exit(1);
}
while(dptr=readdir(dirp))
{
printf("%s\n",dptr->d_name);
}
closedir(dirp);
}
STAT::
OUTPUT:::
enter name of file whose stsistics has toALGO
user id 0
block size :0
last access time 0
Reg. No. : 190501036 Page No. :
time of last modification 0
porduction mode 0
size of file 0
number of links:0
WAIT:
enter name of file whose stsistics has toSE ALL DIAGRAMS
user id 0
block size :0
last access time 0
GETPID:::
process ID is 461
parent process ID id 460
FORK::::
process ID is 461
parent process ID id 460
Exec()::::
total 16
-rwxr-xr-x 1 runner2 runner2 8769 Sep 16 04:23 a.out
-rwxrwxrwx 1 root root 249 Sep 16 04:23 main.c
Parent Process
Parent process
Opendir,readdir:::
Enter directory nameCoding
Error
RESULT
Hence, the programs to implement system calls of UNIX operating system has been executes
successfully.
DATE :
AIM
To simulate UNIX commands such as cp, mv, ls, grep.
A) cp
ALGORITHM
i) START
ii) Create file pointers fp1,fp2
iii) Open the first file in read mode to read the data from file.
iv) Open the second file in write mode to write the date to the file.
v) Check end of file of f1 and write the data into the second file.
vi) Close both the files.
vii) STOP
PROGRAM
#include<stdio.h>
void main()
{
char temp[200];
FILE *fp1,*fp2;
fp1=fopen("file1.txt","r");
fp2=fopen("file2.txt","w");
fgets(temp,200,fp1);
while(!feof(fp1))
{
fputs(temp,fp2);
fgets(temp,200,fp1);
}
fclose(fp1);
fclose(fp2);
}
B) MOVE (mv)
ALGORITHM
i) START
ii) Create file pointers fp1,fp2
iii) Open the first file in read mode to read the data from file.
iv) Open the second file in write mode to write the date to the file.
v) Check end of file of f1 and write the data into the second file.
vi) Close both the files
vii) Remove the first file.
viii) STOP
In file2
Xxx
Ttt
Yyyy
//file1 gets deleted
C) GREP
ALGORITHM
i) START
ii) Create a file pointer f1
iii) Declare i=0,temp[100],b[30]
iv) Ask the user to enter a pattern
v) Open the file in read mode.
vi) If the pattern matches in the file then print the line number and the string.
vii) Close the file
viii) STOP
PROGRAM
#include<stdio.h>
#include<string.h>
void main()
{
FILE *f1;
int i=0;
char temp[100],b[30];
printf("Enter pattern\n");
scanf("%s",b);
f1=fopen("file1.txt","r");
fgets(temp,100,f1);
while(!feof(f1))
{
i++;
if(strstr(temp,b))
{
printf("sub string is %s \n line number is %d ",temp,i);
}
fgets(temp,100,f1);
}
Reg. No. : 190501036 Page No. :
fclose(f1);
}
D) LIST
ALGORITHM
i) START
ii) Create a DIR pointer p and a struct dirent pointer x
iii) Open the required directory.
iv) Do x=readdirectory(p).
v) In a loop while(x!=NULL)
vi) Print the name of directory
vii) STOP
PROGRAM
#include<stdio.h>
#include<dirent.h>
void main()
{
DIR *p;
struct dirent *x;
p=opendir("oslab");
x=readdir(p);
while(x!=NULL)
{
printf("%s",x->d_name);
x=readdir(p);
}
}
RESULT
Thus the basic UNIX commands were successfully simulated.
DATE :
AIM
To implement CPU Scheduling Algorithm.
ALGORITHM
1. Start
2. Define a structure to initialize the variables pro,b,f,w,t
main()
3. Input the number of processes, process name, burst time
4. s[0].w=0, s[0].t=s[0].b, s[0].f=s[0].b
5. wavg=s[0].w, tavg=s[0].t
6. Repeat steps 6 to 8 till i<n, s[i].f=s[i].b+s[i-1].f
7. s[i].w=s[i].f-s[i].a-s[i].b, s[i].t=s[i].f-s[i].a
8. wavg+=s[i].w, tavg+=s[i].t
9. Find the wavg and tavg and print
10.Stop
PROGRAM
#include<stdio.h>
struct fcfs
{
char pro[10];
int b,f,w,t;
}s[10];
main()
{
int n,i;
float wavg,tavg;
printf("Enter the number of process:");
scanf(" %d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the process name:");
scanf("%s",&s[i].pro);
printf("Enter the burst time:");
scanf("%d",&s[i].b);
}
s[0].w=0;
s[0].t=s[0].b;
s[0].f=s[0].b;
wavg=s[0].w;
tavg=s[0].t;
for(i=1;i<n;i++)
{
s[i].f=s[i].b+s[i-1].f;
s[i].w=s[i].f-s[i].a-s[i].b;
s[i].t=s[i].f-s[i].a;
wavg+=s[i].w;
tavg+=s[i].t;
printf("\nPN\tBT\tFT\tWT\tTAT");
for(i=0;i<n;i++)
{
printf("\n%s\t%d\t%d\t%d\t%d",s[i].pro,s[i].b,s[i].f,s[i].w,s[i].t);
}
PN BT FT WT TAT
P1 24 24 0 24
P2 3 27 24 27
P3 3 30 27 30
ALGORITHM
1. Start
2. Define a structure fcfs to initialize the variables pro,a,b,f,w,t
main()
3. Input the number of processes, process name, arrival time, and burst time
4. If min.a>s[j].a, swap s[i] and s[j]
5. s[0].w=0, s[0].t=s[0].b
6. s[0].f=s[0].b+s[0].a
7. wavg=s[0].w, tavg=s[0].t;
8. s[i].w=s[i].f-s[i].a-s[i].b;
9. s[i].t=s[i].f-s[i].a;
10. wavg+=s[i].w, tavg+=s[i].t;
11. Find the wavg and tavg and print
12. Stop
PROGRAM
#include<stdio.h>
main()
{
int n,i,j;
float wavg,tavg;
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the process name:");
scanf("%s",&s[i].pro);
printf(" Enter the arrival time:");
scanf("%d",&s[i].a);
printf("Enter the burst time:");
scanf("%d",&s[i].b);
}
for(i=0;i<=n;i++)
{
min=s[i];
for(j=i+1;j<=n-1;j++)
{
if(min.a>s[j].a)
{
min=s[j];
s[j]=s[i];
s[i]=min;
}
}
}
s[0].w=0;
s[0].t=s[0].b;
s[0].f=s[0].b+s[0].a;
wavg=s[0].w;
tavg=s[0].t;
for(i=1;i<n;i++)
{
if(s[i-1].f>=s[i].a)
{
s[i].f=s[i].b+s[i-1].f;
}
else
{
s[i].f=s[i].a+s[i].b;
}
s[i].w=s[i].f-s[i].a-s[i].b;
s[i].t=s[i].f-s[i].a;
wavg+=s[i].w;
tavg+=s[i].t;
}
wavg=wavg/n;
tavg=tavg/n;
printf("\nPN\tAT\tBT\tFT\tWT\tTAT");
Reg. No. : 190501036 Page No. :
for(i=0;i<n;i++)
{
printf("\n%s\t%d\t%d\t%d\t%d\t%d",s[i].pro,s[i].a,s[i].b,s[i].f,s[i].w,s
[i].t);
}
PN AT BT FT WT TAT
p1 0 10 10 0 10
p5 1 5 15 9 14
p3 2 3 18 13 16
p4 3 4 22 15 19
p2 5 1 23 17 18
C) PRIORITY SCHEDULING
ALGORITHM
1) Start
2) In main initialise all the vlues and call the sem_init function twice
with paramet wrt,0,1 and mutex,0,1.
3) Then call pthread_create function four times with parameters.
4)Call the pthread_join function four times with parameter.
5) In w function call sem_wait function with wrt as parameter.
6) Then get the value and print the shared value and call sem_post with
parameter of wrt.
7) In r function initialise rr as int of a then call sem_wait of mutex.
8)Increment readcount and check if it is equal to 1 then sem_wait of wrt
as a parameter.
9)Then print all the values and again call sem_wait of mutex.
10)Decrement readcount value and if it is equal to 0 then sem_post of wrt
as a paramter.
11)Call sem_post with mutex as a parameter.
12)Stop.
PROGRAM
#include<stdio.h>
main()
{
int i,j,k,n,t,pv,priority, a[10],b[10],p[10],w[10],f[10],ta[10],flag[10];
printf("Enter the number of jobs : ");
scanf("%d",&n);
printf("\nEnter the arrival & burst times of the jobs along with the priority of
execution\n\n");
for(i=0;i<n;i++)
{
printf("Job %d : ",i+1);
flag[i]=0;
scanf("%d", &a[i]);
scanf("%d", &b[i]);
scanf("%d", &p[i]);
}
for(i=0;i<n;i++) /* SORTING */
{
for(j=i+1;j<n;j++)
{
if(a[i] > a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
t=b[i];
b[i]=b[j];
b[j]=t;
}
}
}
Reg. No. : 190501036 Page No. :
i=0;
do
{
if(i == 0)
{
f[i] = b[i];
flag[i] = 1;
w[i] = f[i] - (a[i] + b[i]);
ta[i] = f[i] - a[i];
pv = i++;
continue;
}
j = 0;
k = 0;
priority = n+1;
Enter the arrival & burst times of the jobs along with the priority of execution
Job 1 : 0 11 2
AT BT PRIORITY FT WT
0 11 2 11 0 11
2 10 0 21 9 19
5 28 3 65 32 60
9 16 1 37 12 28
12 2 4 67 53 55
D) ROUND ROBIN
ALGORITHM
1) Start
2) In main initialise all the vlues and call the sem_init function twice
with paramet wrt,0,1 and mutex,0,1.
3) Then call pthread_create function four times with parameters.
4)Call the pthread_join function four times with parameter.
5) In w function call sem_wait function with wrt as parameter.
6) Then get the value and print the shared value and call sem_post with
parameter of wrt.
7) In r function initialise rr as int of a then call sem_wait of mutex.
8)Increment readcount and check if it is equal to 1 then sem_wait of wrt
as a parameter.
9)Then print all the values and again call sem_wait of mutex.
10)Decrement readcount value and if it is equal to 0 then sem_post of wrt
as a paramter.
11)Call sem_post with mutex as a parameter.
12)Stop.
PROGRAM
#include<stdio.h>
main()
{
int n, i, k = 0, count = 0, bt[10], rt[10], ft[10], flag[10], TS;
char pn[10][10];
P1 8
P2 2
P3 7
P4 3
P5 5
E) SJF
ALGORITHM
1) Start
2) In main initialise all the vlues and call the sem_init function twice
with paramet wrt,0,1 and mutex,0,1.
3) Then call pthread_create function four times with parameters.
4)Call the pthread_join function four times with parameter.
5) In w function call sem_wait function with wrt as parameter.
6) Then get the value and print the shared value and call sem_post with
parameter of wrt.
7) In r function initialise rr as int of a then call sem_wait of mutex.
8)Increment readcount and check if it is equal to 1 then sem_wait of wrt
as a parameter.
9)Then print all the values and again call sem_wait of mutex.
10)Decrement readcount value and if it is equal to 0 then sem_post of wrt
as a paramter.
PROGRAM
#define maxno 1000
#include<stdio.h>
#include<string.h>
main()
{
int i,j,k,n,t,pv,flg,smallbt,a[10],b[10],w[10],f[10],ta[10],flag[10];
char p[10][4],temp[10];
printf("Enter the number of jobs : ");
scanf("%d",&n);
printf("\nEnter the process name, arrival & burst times of the jobs\n\n");
for(i=0;i<n;i++)
{
printf("Job %d : ",i+1);
flag[i]=0;
scanf("%s", p[i]);
scanf("%d", &a[i]);
scanf("%d", &b[i]);
}
for(i=0;i<n;i++) /* SORTING */
{
for(j=i+1;j<n;j++)
{
if(a[i] > a[j])
{
t=a[i]; a[i]=a[j]; a[j]=t;
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
i=0,f[0]=0,pv=0;
do
{
flg = 0;
j = 0;
k = 0;
smallbt = maxno;
while((f[pv] >= a[j]) && (j < n))
{
if((smallbt > b[j]) && (flag[j] == 0))
{
flg = 1;
smallbt = b[j];
k = j;
}
j++;
}
if(flg == 0)
{
f[i] = a[i] + b[i]; /* IDLE TIME */
flag[i] = 1;
pv = i;
}
else
Reg. No. : 190501036 Page No. :
{
f[k] = f[pv] + b[k];
flag[k] = 1;
pv = k;
}
w[pv] = f[pv] - (a[pv] + b[pv]);
ta[pv] = f[pv] - a[pv];
i++;
}while(i<n);
printf("\n\n\t\t\t\t OUTPUT \n");
printf("\nPN\tAT\tBT\tFT\tWT\tTT\t\n\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t%d\t%d\t%d\n",p[i],a[i],b[i],f[i],w[i],ta[i]);
}
Enter the process name, arrival & burst times of the jobs
Job 1 : P1 4 4
Job 2 : P2 0 3
Job 3 : P3 2 7
Job 4 : P4 5 6
OUTPUT
PN AT BT FT WT TT
P2 0 3 3 0 3
P3 2 7 10 1 8
P1 4 4 14 6 10
P4 5 6 20 9 15
RESULT
Hence, the programs to implement CPU Scheduling Algorithm has been executed successfully.
DATE :
AIM
To implement file allocation strategies.
A) SEQUENTIAL
ALGORITHM
1) Start.
2) Get the value of number of pages and the value of frame numbers.
3)Run the for loop from 0 to nop and get the value of page table.
4) Get the page size and logical address.
5) Pageno. is equal to logical addres by page si6)ze.
6)Offset is equal to logical addres module of page size.
7)physical addres is equal to page table of pageno. multiplied by page size and add it with ofset.
8)Then print physical address.
9)Stop.
PROGRAM
#include<stdio.h>
#include<stdlib.h>
struct all
{
int pgno, status, linkno;
}a[10];
print(int n)
{
int i;
for(i = 1; i <= n; i++)
{
printf("%d :: %d :: %d\n",i, a[i].pgno, a[i].linkno);
}
}
main()
{
int i, n, pg = 1, fz, bz, nf, ch, cnt, occ=0, vac, k;
printf("Enter the number of frames :: ");
scanf("%d", &n);
printf("Enter the frame size :: ");
scanf("%d", &fz);
do
{
printf("Enter the size of the block to be allocated :: ");
scanf("%d",&bz);
nf = bz/fz;
if(bz%fz != 0)
nf++;
vac = n-occ;
if(occ == n)
{
printf("Frames unavailable\n");
exit(0);
}
else if(vac < nf)
{
printf("Less Vacant frames than required!!! Can't allocate this
block!!!\n");
}
else
{
for(i = 1, cnt = 1; i <= n; i++)
{
if((nf != 0) && (a[i].status == 0))
{
nf--;
occ++;
a[i].status = 1;
a[i].pgno = pg * 10 + cnt;
if((cnt == 1) && ( nf == 0))
a[i].linkno = -1;
else if(cnt == 1)
a[i].linkno = 0;
else if(nf == 0)
{
a[i].linkno = -1;
a[k].linkno = i;
break;
}
else
a[k].linkno = i;
cnt++;
k = i;
printf("\tk=%d\n",k);
}
pg++;
}
print(n);
printf("Do you want to continue (0/1) :: ");
scanf("%d",&ch);
Reg. No. : 190501036 Page No. :
}while(ch == 1);
}
1 :: 2
2 :: 3
3 :: 4
4 :: 1
5 :: 2
B) INDEXED
ALGORITHM
1) Start.
2) Get the value of number of pages and the value of frame numbers.
3)Run the for loop from 0 to nop and get the value of page table.
4) Get the page size and logical address.
5) Pageno. is equal to logical addres by page si6)ze.
6)Offset is equal to logical addres module of page size.
7)physical addres is equal to page table of pageno. multiplied by page size and add it with ofset.
8)Then print physical address.
9)Stop.
PROGRAM
#include<stdio.h>
#include<stdlib.h>
struct all
{
int pgno, status;
}a[10];
print(int n)
{
int i;
for(i = 1; i <= n; i++)
{
printf("%d :: %d\n",i, a[i].pgno);
}
}
main()
Reg. No. : 190501036 Page No. :
{
int i, n, pg = 1, fz, bz, nf, ch, cnt, occ=0, vac;
printf("Enter the number of frames :: ");
scanf("%d", &n);
printf("Enter the frame size :: ");
scanf("%d", &fz);
do
{
printf("Enter the size of the block to be allocated :: ");
scanf("%d",&bz);
nf = bz/fz;
if(bz%fz != 0)
nf++;
vac = n-occ;
if(occ == n)
{
printf("Frames unavailable\n");
exit(0);
}
else if(vac < nf)
{
printf("Less Vacant frames than required!!! Can't allocate this
block!!!\n");
}
else
{
for(i = 1, cnt = 1; i <= n; i++)
{
if((nf != 0) && (a[i].status == 0))
{
a[i].pgno = pg * 10 + cnt;
occ++;
cnt++;
nf--;
a[i].status=1;
}
}
pg++;
}
print(n);
printf("Do you want to continue (0/1) :: ");
scanf("%d",&ch);
}while(ch == 1);
}
1 :: 2
2 :: 1
3 :: 3
Frames unavailable
C) LINKED
ALGORITHM
1) Start.
2) Get the value of number of pages and the value of frame numbers.
3)Run the for loop from 0 to nop and get the value of page table.
4) Get the page size and logical address.
5) Pageno. is equal to logical addres by page si6)ze.
6)Offset is equal to logical addres module of page size.
7)physical addres is equal to page table of pageno. multiplied by page size and add it with ofset.
8)Then print physical address.
9)Stop.
PROGRAM
#include<stdio.h>
#include<stdlib.h>
struct all
{
int pgno, status;
}a[10];
print(int n)
{
int i;
for(i = 1; i <= n; i++)
{
printf("%d :: %d\n",i, a[i].pgno);
}
}
main()
{
int i, n, pg = 1, fz, bz, nf, ch, cnt, occ=0, vac;
printf("Enter the number of frames :: ");
scanf("%d", &n);
Reg. No. : 190501036 Page No. :
printf("Enter the frame size :: ");
scanf("%d", &fz);
do
{
printf("Enter the size of the block to be allocated :: ");
scanf("%d",&bz);
nf = bz/fz;
if(bz%fz != 0)
nf++;
vac = n-occ;
if(occ == n)
{
printf("Frames unavailable\n");
exit(0);
}
else if(vac < nf)
{
printf("Less Vacant frames than required!!! Can't allocate this
block!!!\n");
}
else
{
for(i = 1, cnt = 1; i <= n; i++)
{
if((nf != 0) && (a[i].status == 0))
{
a[i].pgno = pg * 10 + cnt;
occ++;
cnt++;
nf--;
a[i].status=1;
}
}
pg++;
}
print(n);
printf("Do you want to continue (0/1) :: ");
scanf("%d",&ch);
Reg. No. : 190501036 Page No. :
}while(ch == 1);
}
1 :: 1
2 :: 1
3 :: 2
Frames unavailable
RESULT
Hence, the programs for file allocation strategies has been executed successfully.
AIM
To implement Producer Consumer Problem using Semaphores
ALGORITHM
i) START
ii) Initialise the semaphone and all the values needs to be used.
iii) From main call the producer function.
iv) In producer function we need to run an infinite loop.
v) Initialise i has 0 and call wait function with empty and mutex as a parameter.
vi) Increment i and assign it to put.
vii) Then assign p to who and print put value.
viii) Call the signal function with mutex and full as parameter.
ix) Call wait function with full and mutex as a parameter.
x) Assign put to get and c to who then print get.
xi) Call the signal function with mutex and empty as parameter.
xii) In wait function check whether s is less than or equal to 0 and who is equal to p then call customer orelse call
producer.
xiii) Orelse s is subtracted by 1 itself.
xiv) In signal function we increment s by 1.
xv) STOP
PROGRAM
#include<stdio.h>
typedef int semaphore;
semaphore mutex=1,full=0,empty=1;
char who;
int put,get;
producer()
{
do
{
static int i=0;
wait(&empty);
wait(&mutex);
i++;
put=i;
who='p';
printf("PUT: %d/n",put);
signal(&mutex);
signal(&full);
}
while(1);
}
consumer()
{
do
{
wait(&full);
wait(&mutex);
get=put;
who='c';
printf("GET: %d/n",get);
Reg. No. : 190501036 Page No. :
signal(&mutex);
signal(&empty);
}
while(1);
}
wait(semaphore *s)
{
if(*s<=0)
{
if(who=='p')
consumer();
else
producer();
}
else
*s=*s-1;
}
signal(semaphore *s)
{
*s=*s+1;
}
main()
{
printf("\t\tproducer consumer problem using semaphore\n");
printf("\n\npress ctrl+z to break execution\n\n");
producer();
}
PUT: 1/nGET: 1/nPUT: 2/nGET: 2/nPUT: 3/nGET: 3/nPUT: 4/nGET: 4/nPUT: 5/nGET: 5/nPUT: 6/nGET:
6/nPUT: 7/nGET: 7/nPUT: 8/nGET: 8/nPUT: 9/nGET: 9/nPUT: 10/nGET: 10/nPUT: 11/nGET: 11/nPUT:
12/nGET: 12/nPUT: 13/nGET: 13/nPUT: 14/nGET: 14/nPUT: 15/nGET: 15/nPUT: 16/nGET: 16/nPUT:
17/nGET: 17/nPUT: 18/nGET: 18/nPUT: 19/nGET: 19/nPUT: 20/nGET: 20/nPUT: 21/nGET: 21/nPUT:
22/nGET: 22/nPUT: 23/nGET: 23/nPUT: 24/nGET: 24/nPUT: 25/nGET: 25/nPUT: 26/nGET: 26/nPUT:
27/nGET: 27/nPUT: 28/nGET: 28/nPUT: 29/nGET: 29/nPUT: 30/nGET: 30/nPUT: 31/nGET: 31/nPUT:
32/nGET: 32/nPUT: 33/nGET: 33/nPUT: 34/nGET: 34/nPUT: 35/nGET: 35/nPUT: 36/nGET: 36/nPUT:
37/nGET: 37/nPUT: 38/nGET: 38/nPUT: 39/nGET: 39/nPUT: 40/nGET: 40/nPUT: 41/nGET: 41/nPUT:
42/nGET: 42/nPUT: 43/nGET: 43/nPUT: 44/nGET: 44/nPUT: 45/nGET: 45/nPUT: 46/nGET: 46/nPUT:
47/nGET: 47/nPUT: 48/nGET: 48/nPUT: 49/nGET: 49/nPUT: 50/nGET: 50/nPUT: 51/nGET: 51/nPUT:
52/nGET: 52/nPUT: 53/nGET: 53/nPUT: 54/nGET: 54/nPUT: 55/nGET: 55/nPUT: 56/nGET: 56/nPUT:
57/nGET: 57/nPUT: 58/nGET: 58/nPUT: 59/nGET: 59/nPUT: 60/nGET: 60/nPUT: 61/nGET: 61/nPUT:
62/nGET: 62/nPUT: 63/nGET: 63/nPUT: 64/nGET: 64/nPUT: 65/nGET: 65/nPUT: 66/nGET: 66/nPUT:
67/nGET: 67/nPUT: 68/nGET: 68/nPUT: 69/nGET: 69/nPUT: 70/nGET: 70/nPUT: 71/nGET: 71/nPUT:
72/nGET: 72/nPUT: 73/nGET: 73/nPUT: 74/nGET: 74/nPUT: 75/nGET: 75/nPUT: 76/nGET: 76/nPUT:
77/nGET: 77/nPUT: 78/nGET: 78/nPUT: 79/nGET: 79/nPUT: 80/nGET: 80/nPUT: 81/nGET: 81/nPUT:
82/nGET: 82/nPUT: 83/nGET: 83/nPUT: 84/nGET: 84/nPUT: 85/nGET: 85/nPUT: 86/nGET: 86/nPUT:
87/nGET: 87/nPUT: 88/nGET: 88/nPUT: 89/nGET: 89/nPUT: 90/nGET: 90/nPUT: 91/nGET: 91/nPUT:
92/nGET: 92/nPUT: 93/nGET: 93/nPUT: 94/nGET: 94/nPUT: 95/nGET: 95/nPUT: 96/nGET: 96/nPUT:
97/nGET: 97/nPUT: 98/nGET: 98/nPUT: 99/nGET: 99/nPUT: 100/nGET: 100/nPUT: 101/nGET: 101/nPUT::
2289/
RESULT
Hence, the program to implement Producer Consumer Problem using Semaphores has been executed successfully.
AIM
To implement File Organization Techniques.
A) SINGLE LEVEL
ALGORITHM
1) Start
2) Get the size of directory.
3) Get the names of each directory.
4)Get the files to store in directory.
5)Store the files in directory.
PROGRAM
#include<stdio.h>
void main()
{
int i, j, k, n, x;
char d[10][10];
char fn[10][10][10];
int s[10];
printf("\n\nDIRECTORY\tSIZE\tFILENAMES\n");
for(i = 0; i < n; i++)
{
printf("\n%s\t%d\t",d[i],s[i]);
No of Directories 1
size of Directory is 1
Name of Directory is Dir
Enter the file names F1
B) TWO LEVEL
ALGORITHM
1) Start
2) Get the size of directory.
3) Get the names of each directory.
4)Get the size of Sub Directory.
5)Get the Names of each sub Directory.
6)Get the file names.
7)Store the files in Sudirectory.
PROGRAM
#include<stdio.h>
void main()
{
int i, j, k, n, x;
char d[10][10];
char sd[10][10][10];
char fn[10][10][10][10];
int s[10], ss[10][10];
printf("\n\nDIRECTORY\tSIZE\tSUBDIRECTORY\tSIZE\tFILENAMES\n");
for(i = 0; i < n; i++)
{
printf("\n%s\t%d\t",d[i],s[i]);
for(j = 0; j < s[i]; j++)
{
printf("\t%s\t%d",sd[i][j], ss[i][j]);
for(k = 0; k < ss[i][j]; k++)
{
printf("\t%s\t", fn[i][j][k]);
printf("\t\t\t\t");
}
printf("\n\t\t");
}
}
No of Directories 1
size of Directory is 1
Name of Directory is Dir
Enter the size of sub Directory 1
Enter the name of Sub Directory Sub1
Enter the file names F1
In Directory Dir
In Sub Directory sub1
F1
C) HIERARCHICAL
ALGORITHM
1) Start
2) Get the size of directory.
3) Get the names of each directory.
4)Get the size of Sub Directory.
5) Get the Names of each sub Directory.
6) Get the sub directory size and name of sub Directory
7)Get the file names.
8)Store the files.
PROGRAM
#include<stdio.h>
#include<conio.h>
clrscr();
printf("Enter the number of directories :: ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("Enter the name of the directories :: ");
scanf("%s",d[i]);
}
printf("\n\nDIRECTORY\tSIZE\tSUBDIRECTORY\tSIZE\tSUBDIRECTORY\tSIZE\tFILENAMES\n")
;
for(i = 0; i < n; i++)
{
printf("\n%s\t%d\t",d[i],s[i]);
for(j = 0; j < s[i]; j++)
{
printf("\t%s\t%d",sd[i][j], ss[i][j]);
for(k = 0; k < ss[i][j]; k++)
{
printf("\t%s\t%d\t", sdd[i][j][k], sss[i][j][k]);
for(l = 0; l < sss[i][j][k]; l++)
{
Reg. No. : 190501036 Page No. :
printf("%s\n",fn[i][j][k][l]);
printf("\t\t\t\t\t\t\t");
}
printf("\t\t\t\t\t\t");
}
printf("\n\t\t");
}
printf("\n\t");
}
getch();
}
In Directory Dir
In Sub Directory sub1
In Sub Directory SSub1
F1
D) DAG
ALGORITHM
1) Start
2) Get the size of directory.
3) Get the names of each directory.
4)Get the size of Sub Directory.
5)Get the Names of each sub Directory.
6)Get the file names.
7) Ask Whether the files connected to anyother sub directory.
8) If yes means connect files to that sub directory,No means store NULL.
9)Store the files.
PROGRAM
#include<stdio.h>
void main()
{
int i, j, k, n, x;
char d[10][10];
char sd[10][10][10];
char fn[10][10][10][10], lk[10][10][10][10];
int s[10], ss[10][10];
printf("\n\nDIRECTORY\tSIZE\tSUBDIRECTORY\tSIZE\tFILENAMES\tLINK\n");
for(i = 0; i < n; i++)
{
printf("\n%s\t%d\t",d[i],s[i]);
for(j = 0; j < s[i]; j++)
{
printf("\t%s\t%d",sd[i][j], ss[i][j]);
for(k = 0; k < ss[i][j]; k++)
{
printf("\t%s\t", fn[i][j][k]);
printf("%s\n",lk[i][j][k]);
printf("\t\t\t\t");
}
printf("\n\t\t");
}
}
In Directory Dir
In Sub directory sub1
Files are F1->NULL
In Sub directory sub2
Files are F2->sub1
RESULT
Hence, the programs to implement File Organization Techniques has been executed successfully.
AIM
To implement Bankers algorithm for Deadlock Avoidance.
ALGORITHM
1) Start
2) Initialise all the values required for the algorithm.
3) In input function collect all the number process and its resources.
4)Then get the allocation matrix,maximum matrix and availabe matrix
from the user
5)In show function display all the values we got from the input function
6)In safety function run the for loop from 0 to n and assign finish of
i as 0.
7)Run the for loop from 0 to r and initialise work of i as avail of i.
8)Run a while loop with statement i is less than n and initialise flag
as 0 and again a loop from 0 to r.
9) Check if finish of i is equal to 0 and need is less than avialable then
add available with the allocated values.
10) Then break the loop if the flag is equal to 0.
11) Check the count of values which is equal to finish of i and if it is
equal to the value of n then the system is safe or else its not.
12) Check the process number and get the value of request from the user.
13)Check whether the request is greater than need.
14) If so request cant be formed or else the request is formed.
15) Run the form loop from 0 to r and calculate availabem allocation and
need of the page and check whether it is safe or not.
16) Stop.
PROGRAM
#include<stdio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int work[100];
int finish[100];
int request[100];
int n,r;
void input()
{
int i,j;
printf("Enter the number of processes :: ");
scanf("%d",&n);
printf("Enter the number of resources :: ");
scanf("%d",&r);
printf("Enter the allocation matrix :: ");
for(i=0;i<n;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("Enter the max matrix :: ");
for(i=0;i<n;i++)
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
for(i=0;i<n;i++)
for(j=0;j<r;j++)
need[i][j] = max[i][j] - alloc[i][j];
}
void show()
{
int i,j;
printf("Allocation Matrix\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<r;j++)
{
printf("\t");
printf("%d",alloc[i][j]);
}
}
printf("\nMax Matrix\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<r;j++)
{
printf("\t");
printf("%d",max[i][j]);
}
}
printf("\nAvailable Matrix\n");
for(i=0;i<r;i++)
printf("%d\t",avail[i]);
printf("\nNeed Matrix\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<r;j++)
{
printf("\t");
printf("%d",need[i][j]);
}
}
}
int safety()
{
int i,j, k, c1 = 0, flag, count = 0;
for(i=0;i<n;i++)
{
finish[i] = 0;
}
for(i=0;i<r;i++)
{
work[i] = avail[i];
}
printf("\n");
while(count < n)
{
Reg. No. : 190501036 Page No. :
flag = 0;
for(i = 0; i < n; i++)
{
int c=0;
for(j = 0; j < r; j++)
{
if((finish[i] == 0) && ( need[i][j] <= work[j]))
{
c++;
if(c==r)
{
for(k = 0; k < r; k++)
{
work[k] +=
alloc[i][k];
}
finish[i] = 1;
count++;
printf("P%d -> ",i);
flag = 1;
}
}
}
}
if(flag == 0)
break;
}
printf("%d", c1);
if(c1 == n)
{
printf("\n The system is in safe state!!!\n\n");
return 1;
}
else
{
printf("\n The system is in unsafe state since there is a
deadlock!!!\n\n");
return 0;
}
}
void resourcerequest()
{
int p, i;
printf("Enter the process number requesting for resources :: ");
scanf("%d", &p);
show();
int x = safety();
if(x)
printf("The new system is safe!!! Request can be granted!!!\n");
else
printf("The new system is unsafe!!! Request can't be granted!!!\n");
}
main()
{
int x;
input();
show();
x = safety();
if(x) resourcerequest();
else
printf("RESOURCE REQUEST CANT BE PERFORMED \n");
}
4 0 1
0 2 0
1 1 2
Enter the max matrix :: 4 3 3
3 2 2
9 0 2
7 5 3
2 1 2
4 0 1
0 2 0
1 1 2
Max Matrix
4 3 3
3 2 2
9 0 2
7 5 3
1 1 2
Available Matrix
2 1 0
Need Matrix
3 2 1
1 1 0
5 0 1
7 3 3
0 0 0
RESULT
Hence the program to implement Bankers algorithm for Deadlock Avoidance has been executes successfully.
AIM
To implement an algorithm for Deadlock Detection
ALGORITHM
1) Start
2) Initialise all the values required for the algorithm.
3) In input function collect all the number process and its resources.
4)Then get the allocation matrix,maximum matrix and availabe matrix
from the user
5)In show function display all the values we got from the input function
6)In safety function run the for loop from 0 to n and assign finish of
i as 0.
7)Run the for loop from 0 to r and initialise work of i as avail of i.
8)Run a while loop with statement i is less than n and initialise flag
as 0 and again a loop from 0 to r.
9) Check if finish of i is equal to 0 and need is less than avialable then
add available with the allocated values.
10) Then break the loop if the flag is equal to 0.
11) Check the count of values which is equal to finish of i and if it is
equal to the value of n then the system is safe or else its not.
12) Stop.
PROGRAM
#include<stdio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int work[100];
int finish[100];
int n,r;
void input()
{
int i,j;
printf("Enter the number of processes :: ");
scanf("%d",&n);
printf("Enter the number of resources :: ");
scanf("%d",&r);
printf("Enter the allocation matrix :: ");
for(i=0;i<n;i++)
for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
printf("Enter the max matrix :: ");
for(i=0;i<n;i++)
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
printf("Enter the available matrix :: ");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
for(i=0;i<n;i++)
for(j=0;j<r;j++)
need[i][j] = max[i][j] - alloc[i][j];
Reg. No. : 190501036 Page No. :
}
void show()
{
int i,j;
printf("Allocation Matrix\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<r;j++)
{
printf("\t");
printf("%d",alloc[i][j]);
}
}
printf("\nMax Matrix\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<r;j++)
{
printf("\t");
printf("%d",max[i][j]);
}
}
printf("\nAvailable Matrix\n");
for(i=0;i<r;i++)
printf("%d\t",avail[i]);
printf("\nNeed Matrix\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<r;j++)
{
printf("\t");
printf("%d",need[i][j]);
}
}
}
void safety()
{
int i,j, k, c1 = 0, flag, count = 0;
for(i=0;i<n;i++)
{
finish[i] = 0;
}
for(i=0;i<r;i++)
{
work[i] = avail[i];
}
printf("\n");
while(count < n)
{
flag = 0;
for(i = 0; i < n; i++)
{
int c=0;
for(j = 0; j < r; j++)
{
if((finish[i] == 0) && ( need[i][j] <= work[j]))
Reg. No. : 190501036 Page No. :
{
c++;
if(c==r)
{
for(k = 0; k < r; k++)
{
work[k] +=
alloc[i][k];
}
finish[i] = 1;
count++;
printf("P%d -> ",i);
flag = 1;
}
}
}
}
if(flag == 0)
break;
}
printf("%d", c1);
if(c1 == n)
{
printf("\n The system is in safe state!!!\n\n");
}
else
{
printf("\n The system is in unsafe state since there is a
deadlock!!!\n\n");
}
}
main()
{
int x;
input();
show();
safety();
}
212
401
Reg. No. : 190501036 Page No. :
020
112
322
902
753
112
2 1 2
4 0 1
0 2 0
1 1 2
Max Matrix
4 3 3
3 2 2
9 0 2
7 5 3
1 1 2
Available Matrix
2 1 0
Need Matrix
3 2 1
1 1 0
5 0 1
7 3 3
0 0 0
RESULT
Hence the program to implement an algorithm for Deadlock Detection has been executed successfully.
AIM
To implement page replacement algorithms.
A) FIFO
ALGORITHM
1) Start.
2) Get the number of pages from the user and the page of i.
3)Get the frame size of initialise each frame as -1.
4) Get the page allocated to the frame and run the loop from 0 to f and
check if frame of k is equal to i then avail is equal to 1.
5) If avail is equal to 0 then frame of j is equal to page of i.
6) J is equal to j added by 1 with a module of f and increment count.
7)Run the loop from 0 to f and get the frame value
8)Print the number of page fault.
9)Stop.
PROGRAM
#include<stdio.h>
main()
{
int avail, count, i, j, n, f, k, page[10], frame[10];
avail=1;
if(avail == 0)
{
frame[j] = page[i];
j = (j + 1) % f;
count++;
for(k = 0; k < f; k++)
printf("%d\t",frame[k]);
}
Reg. No. : 190501036 Page No. :
printf("\n");
}
printf("Number of Page Fault is %d\n\n",count);
}
4 4 -1 -1
1 4 1 -1
2 4 1 2
5 5 1 2
B) LRU
ALGORITHM
1) Start.
2) In struct initialise the values.
3) Get the maximum page number and get the values of pno and cnt from the
loop.
4the number of pages from the user and the page of i.
5)Get the frame size of initialise each frame as -1.
6) Get the page allocated to the frame and run the loop from 0 to f and
check if frame of k is equal to i then avail is equal to 1.
7) If frame of k is equal to page of i then avail if equal to q and m of
frame of k with count incremented.
8) If avail is equal to 0 then small is equal to n added by 1.
9)Run the loop from 0 to f and get the frame value
10) If it is equal to -1 then sind is equal to k and then break.
11) If small is greater then m of x.count small is equal to m of x.count
then sind is equal to k and then break.
12) Then print the sind value and frame of sind as page of i.
13)Initialise x as page of i and m of x with count incremented.
14)Run the for loop from 0 to f and print the frame of k.
15)Print the number of page fault .
16)Stop.
PROGRAM
#include<stdio.h>
main()
{
struct page m[10];
int avail, small, sind, c, count = 0, x, i, j, n, f, k, pmax, page[25], frame[10];
sind = 0;
if(avail == 0)
{
small = n + 1;
for(k = 0; k < f; k++)
{
x = frame[k];
if(x == -1)
{
sind = k;
break;
}
Reg. No. : 190501036 Page No. :
if(small > m[x].cnt)
{
small = m[x].cnt;
sind = k;
}
}
//printf("\nsind :: %d",sind);
frame[sind] = page[i];
x = page[i];
m[x].cnt = c;
count++;
for(k = 0; k < f; k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Number of Page Fault is %d\n\n",count);
}
7 7 -1 -1
5 7 5 -1
9 7 5 9
4 4 5 9
3 3 5 9
7 7 5 9
6 6 5 9
2 2 5 9
1 1 5 9
Reg. No. : 190501036 Page No. :
Number of Page Fault is 9
C) LFU
ALGORITHM
1) Start.
2) In struct initialise the values.
3) Get the maximum page number and get the values of pno and cnt from the
loop.
4the number of pages from the user and the page of i.
5)Get the frame size of initialise each frame as -1.
6) Get the page allocated to the frame and run the loop from 0 to f and
check if frame of k is equal to i then avail is equal to 1.
7) If frame of k is equal to page of i then avail if equal to q and m of
frame of k with count incremented.
8) If avail is equal to 0 then small is equal to n added by 1.
9)Run the loop from 0 to f and get the frame value
10) If it is equal to -1 then sind is equal to k and then break.
11) If small is greater then m of x.count small is equal to m of x.count
then sind is equal to k and then break.
12) Then print the sind value and frame of sind as page of i.
13)Initialise x as page of i and m of x with count incremented.
14)Run the for loop from 0 to f and print the frame of k.
15)Print the number of page fault .
16)Stop.
PROGRAM
#include<stdio.h>
struct page
{
int pno;
int cnt;
};
main()
{
struct page m[10];
int avail, small, sind, count = 0, x, i, j, n, f, k, pmax, page[25], frame[10];
if(avail == 0)
{
small = n + 1;
for(k = 0; k < f; k++)
{
x = frame[k];
if(x == -1)
{
sind = k;
break;
}
if(small > m[x].cnt)
{
small = m[x].cnt;
sind = k;
}
}
//printf("\nsind :: %d",sind);
frame[sind] = page[i];
x = page[i];
m[x].cnt++;
count++;
for(k = 0; k < f; k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Number of Page Fault is %d\n\n",count);
}
1 1 -1 -1
2 1 2 -1
3 1 2 3
4 4 2 3
5 5 2 3
1 5 2 1
4 5 2 4
3 5 2 3
RESULT
Hence the program to implement a page replacement algorithms has been executed successfully.
AIM
To implement Shared memory, message passing and pipes.
A) SHARED MEMORY
ALGORITHM
Client:
1) Start.
2) Initialise all the values.
3) Check if shmid is equal shmget of key, SHMSZ, 066 which are less then
0 then print Cannot attach the data and exit.
4) Check if shm is equal shmat of shmid, NULL, 0 which is equal to char of *
subtracted by 1 0 then print Cannot attach the data and exit.
5) Assign s as shm and run for loop from shm to NULL and increment s
6)Putchar of s and add space then shm as *.
7)Atlast exit with 0.
8)Stop.
Server:
1) Start.
2) Initialise all the values.
3) Check if shmid is equal shmget of key, SHMSZ, IPC_CREAT or of 0666
then print Cannot attach the data and exit.
4) Check if shm is equal shmat of shmid, NULL, 0 which is equal to char of *
subtracted by 1 0 then print Cannot attach the data and exit.
5) Assign c as shm and run for loop from a to less than or equal to z and
increment c
6) S increment is equal to c.
7) While shm is not equal to * the sleep of 1.
8)Stop.
PROGRAM
a.) Client.c
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
#include<stdlib.h>
#define SHMSZ 27
main()
{
int shmid;
key_t key;
char *shm, *s;
key = 5678;
s=shm;
*shm='*';
exit(0);
}
b.) Server.c
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
#include<stdlib.h>
#define SHMSZ 27
main()
{
char c;
int shmid;
key_t key;
char *shm, *s;
key = 5678;
s=shm;
*s='\0';
while(*shm != '*')
sleep(1);
Reg. No. : 190501036 Page No. :
exit(0);
}
Hello
Hello
B) MESSAGE PASSING
ALGORITHM
Sender
Algo:
1) Start
2) Initialise a tydef of structure and values.
3)And then gt all the values in it.
4)Check if msqid is equal to mssget of key msgflg which is less then 0.
5)Else the fprintf of stderr.
6)Initialise mtype as 1 and print the message.
7)Then string ccopy the value and print it.
8) Then calculate the buffer length and check if msgsnd of msqid and sbuf
buffer length and IPC_NOWAIT is less than 0 then print all th values.
9) perror of msgsnd the exit and print the message of sbuf of mtext.
10)Stop
Receiver
Algo:
1) Start.
2) Check is msqid is equal to msgget of key 0666 which is less then 0 and
perror of mssgget then exit.
3) Check if msgrcv of msqid and rbuf MSGSZ, 1,0 which is less then 0 and perror
of msgrcv then exit.
4) Print the rbuf of mtext then exit of 0.
5)Stop
PROGRAM
Message Passing
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#define MSGSZ 128
typedef struct msgbuf
{long mtype;
char mtext[MSGSZ];
} message_buf;
main()
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
Reg. No. : 190501036 Page No. :
message_buf sbuf;
size_t buf_length;
key = 1234;
(void) fprintf(stderr, "\nmsgget: Calling msgget(%#lx,\
%#o)\n", key,
msgflg);
if ((msqid = msgget(key, msgflg )) < 0)
{perror("msgget");
exit(1);
}
else
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
sbuf.mtype = 1;
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
(void) strcpy(sbuf.mtext, "Did you get this?");
(void) fprintf(stderr,"msgget: msgget succeeded: msqid = %d\n", msqid);
buf_length = strlen(sbuf.mtext) + 1 ;
if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {
printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buf_length);
perror("msgsnd");
exit(1);
}
else
printf("Message: \"%s\" Sent\n", sbuf.mtext);
exit(0);
}
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#define MSGSZ 128
typedef struct msgbuf
{long mtype;
char mtext[MSGSZ];
} message_buf;
main()
{
int msqid; key_t
key; message_buf
rbuf;
key = 1234;
if ((msqid = msgget(key, 0666)) < 0)
{perror("msgget");
exit(1);
}
if (msgrcv(msqid, &rbuf, MSGSZ, 1, 0) < 0)
{
perror("msgrcv");
exit(1);
}
printf("%s\n", rbuf.mtext);
exit(0);
}
C) PIPES
ALGORITHM
Reg. No. : 190501036 Page No. :
1) Start
2) Get the string from the pipe.
3) Initialise fork to child check if child is not equal then close the fd
of 10 .
4) Then write fd of 1, a,5 and wait as 0.
5) Else close the fd of 1, read fd of 0, a,5 then print the retrived valu\
for pipe.
6) Stop.
PROGRAM
#include<stdio.h>
int main()
{
int fd[2],child;
char a[10];
printf("\n enter the string to enter into the pipe:");
scanf("%s",a);
pipe(fd);
child=fork();
if(!child)
{ close(fd[10]);
write(fd[1],a,5);
wait(0);
}
else
{
close(fd[1]);
read(fd[0],a,5);
printf("\n the string retrived from pipe is %s\n",a);
}
return 0;
}
RESULT
Hence the program to implement Shared memory, message passing and pipes has been executed successfully.
AIM
To implement Paging and Segmentation Technique of memory management.
A) PAGING
ALGORITHM
1) Start.
2) Get the value of number of pages and the value of frame numbers.
3)Run the for loop from 0 to nop and get the value of page table.
4) Get the page size and logical address.
5) Pageno. is equal to logical addres by page si6)ze.
6)Offset is equal to logical addres module of page size.
7) physical addres is equal to page table of pageno. multiplied by page
size and add it with ofset.
8) Then print physical address.
9)Stop.
PROGRAM
#include <stdio.h>
int main()
{
int pgtable[10];
int psz, nop, i, la, pgno, off, pa;
pgno = la / psz;
off = la % psz;
ALGORITHM
1) Start.
2) Get the value of number of segements.
3) Run the for loop till nos and get the base and limit value of the segment.
4)Then get the logical address of the segment.
5) Check is s of sno. limit is greater then equal to offset.
6) Then calculate pa as s of sno. base and add offset then print pa.
7)Orelse print Invalid address.
8)Stop
PROGRAM
#include <stdio.h>
struct segtable
{
int base;
int limit;
};
int main()
{
struct segtable s[10];
int nos, i, sno, off, pa;
RESULT
Hence the program to implement Paging and Segmentation Technique of memory management has been
executed successfully.
AIM
To implement Threading & Synchronization Applications for Reader Writer Problem
ALGORITHM
1) Start
2) In main initialise all the vlues and call the sem_init function twice
with paramet wrt,0,1 and mutex,0,1.
3) Then call pthread_create function four times with parameters.
4)Call the pthread_join function four times with parameter.
5) In w function call sem_wait function with wrt as parameter.
6) Then get the value and print the shared value and call sem_post with
parameter of wrt.
7) In r function initialise rr as int of a then call sem_wait of mutex.
8)Increment readcount and check if it is equal to 1 then sem_wait of wrt
as a parameter.
9)Then print all the values and again call sem_wait of mutex.
10)Decrement readcount value and if it is equal to 0 then sem_post of wrt
as a paramter.
11)Call sem_post with mutex as a parameter.
12)Stop.
PROGRAM
#include<stdio.h>
#include<sys/types.h>
#include<pthread.h>
#include<semaphore.h>
#include<stdlib.h>
int b;
int readcount;
sem_t mutex,wrt;
void *w()
{
sem_wait(&wrt);
printf("Enter the value :: ");
scanf("%d",&b);
printf("Shared Value (Writer) = %d\n",b);
sem_post(&wrt);
}
void *r(void *a)
{
int *rr=(int *)a;
sem_wait(&mutex);
readcount++;
if(readcount==1)
sem_wait(&wrt);
sem_post(&mutex);
printf("I am Reader %d\t",*rr);
printf("\n Last Updated Shared value (Reader) =%d\n",b);
sem_wait(&mutex);
readcount--;
if(readcount==0)
sem_post(&wrt);
sem_post(&mutex);
Reg. No. : 190501036 Page No. :
}
int main()
{
pthread_t w1,w2,r1,r2;
int rr1=1,rr2=2;
b=20;
readcount=0;
sem_init(&wrt,0,1);
sem_init(&mutex,0,1);
pthread_create(&w1,NULL,w,NULL);
pthread_create(&r1,NULL,r,(void *)&rr1);
pthread_create(&r2,NULL,r,(void *)&rr2);
pthread_create(&w2,NULL,w,NULL);
pthread_join(w1,NULL);
pthread_join(r1,NULL);
pthread_join(r2,NULL);
pthread_join(w2,NULL);
}
RESULT
Hence the program to implement Threading & Synchronization Applications for Reader Writer Problem has
been executed successfully.
AIM
To Study Minix Operating System
MINIX
Minix (from "mini-Unix") is a POSIX-compliant (since version 2.0), Unix-like operating system based on a
microkernel architecture.
Early versions of MINIX were created by Andrew S. Tanenbaum for educational purposes. Starting with
MINIX 3, the primary aim of development shifted from education to the creation of a highly reliable and self-
healing microkernel OS. MINIX is now developed as open-source software.
MINIX was first released in 1987, with its complete source code made available to universities for study in
courses and research. It has been free and open-source software since it was re-licensed under the BSD-3-
Clause license in April 2000
Implementation
Minix 1.0
Andrew S. Tanenbaum created MINIX at Vrije Universiteit in Amsterdam to exemplify the principles conveyed
in his textbook, Operating Systems: Design and Implementation (1987).
An abridged 12,000 lines of the C source code of the kernel, memory manager, and file system of MINIX 1.0
are printed in the book. Prentice-Hall also released MINIX source code and binaries on floppy disk with a
reference manual. MINIX 1 was system-call compatible with Seventh Edition Unix.[11]
Tanenbaum originally developed MINIX for compatibility with the IBM PC and IBM PC/AT 8088
microcomputers available at the time.
Minix 1.5
MINIX 1.5, released in 1991, included support for MicroChannel IBM PS/2 systems and was also ported to the
Motorola 68000 and SPARC architectures, supporting the Atari ST, Commodore Amiga, Apple Macintosh[12]
and Sun SPARCstation computer platforms. There were also unofficial ports to Intel 386 PC compatibles (in
32-bit protected mode), National Semiconductor NS32532, ARM and Inmos transputer processors. Meiko
Scientific used an early version of MINIX as the basis for the MeikOS operating system for its transputer-based
Computing Surface parallel computers. A version of MINIX running as a user process under SunOS and Solaris
was also available, a simulator named SMX (operating system) or just SMX for short.[13][14]
Version 2.0.3 was released in May 2001. It was the first version after MINIX had been relicensed under the
BSD-3-Clause license, which was retroactively applied to all previous versions.[15]
Minix-vmd
Main article: Minix-vmd
Minix-vmd is a variant of MINIX 2.0 for Intel IA-32-compatible processors, created by two Vrije Universiteit
researchers, which adds virtual memory and support for the X Window System.
Minix 3
Main article: MINIX 3
Minix 3 currently supports IA-32 and ARM architecture systems. It is available in a Live CD format that allows
it to be used on a computer without installing it on the hard drive, and in versions compatible with hardware
emulating and virtualizing systems, including Bochs, QEMU, VMware Workstation/Fusion, VirtualBox, and
Microsoft Virtual PC.
Version 3.1.2 was released on 18 April 2006. It was the first version after MINIX had been relicensed under the
BSD-3-Clause license with a new fourth clause.[17]
Version 3.1.5 was released on 5 November 2009. It contains X11, emacs, vi, cc, gcc, perl, python, ash, bash,
zsh, ftp, ssh, telnet, pine, and over 400 other common Unix utility programs. With the addition of X11, this
As of version 3.2.0, the userland was mostly replaced by that of NetBSD and support from pkgsrc became
possible, increasing the available software applications that MINIX can use. Clang replaced the prior compiler
(with GCC now having to be manually compiled), and GDB, the GNU debugger, was ported.[19][20]
Minix 3.4.0RC, Release Candidates became available in January 2016;[21] however, a stable release of MINIX
3.4.0 is yet to be announced.
Minix supports many programming languages, including C, C++, FORTRAN, Modula-2, Pascal, Perl, Python,
and Tcl.
Minix 3 still has an active development community with over 50 people attending MINIXCon 2016, a
conference to discuss the history and future of MINIX.[22]
All Intel chipsets post-2015 are running MINIX 3 internally as the software component of the Intel
Management Engine.[23][24]
Early Linux kernel development was done on a Minix host system, which led to Linux inheriting various
features from Minix, such as the Minix file system.
RESULT
Hence, the Study Minix Operating System has been performed successfully.