Os Lab Manual PDF
Os Lab Manual PDF
OBJECTIVES
LIST OF EXPERIMENTS
2. Write programs using the following system calls of UNIX operating system
3. Write C programs to simulate UNIX commands like cp, ls, grep, etc
4. Shell Programming
6. Implementation of Semaphores
LRU c) LFU
15. Implementation of the following File Allocation Strategies a) Sequential b) Indexed c) Linked
Ex No. 1. BASIC COMMANDS IN LINUX
1. date
i. date +%m
v. date +%H
2. cal
i. cal year
3. echo “message”
4. who
i. whoami
5. mkdir
6. cd directory name
8. cd
9. ls
10. cat>filename
17. chmod
20. man
21.clear
COMMAND : date
SYNTAX : date
EXPLANATION: This command displays the current system date and time on the screen.
OUTPUT :
COMMAND : date
OUTPUT :
11
COMMAND : date
EXPLANATION: This command displays the name of the current month on the screen
OUTPUT :
[exam1@local host ~]$date +%h
Nov
COMMAND : date
EXPLANATION: This command displays the current system date on the screen.
OUTPUT :
27
COMMAND : date
EXPLANATION: This command displays the current system year on the screen.
OUTPUT :
13
2013
COMMAND : date
EXPLANATION: This command displays the current system time (in hour) on the screen.
OUTPUT :
10
COMMAND : date
SYNTAX : date +%M
EXPLANATION: This command displays system time (in min) on the screen.
OUTPUT :
49
COMMAND : date
EXPLANATION: This command displays the current system time (in sec) on the screen.
OUTPUT :
50
COMMAND : calendar
EXPLANATION: This command displays the calendar of the current month on the screen.
default.)
OUTPUT :
November 2013
SuMo 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
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
COMMAND : cal
OUTPUT :
COMMAND : echo
EXPLANATION: This command display on the screen the argument of the echo command.
OUTPUT :
Hello
COMMAND : who
SYNTAX : who
EXPLANATION : This command lists the information about all the users who have logged on that
system.
OUTPUT :
COMMAND : who
SYNTAX :whoami
EXPLANATION : This command displays the information about the current user of the
system on the screen.
OUTPUT :
exam88
SYNTAX :mkdir
EXPLANATION : This command is used to create a new directory with a specified name.
OUTPUT :
[exam88@local host ~] $ mkdir sun
[exam88@local host ~] $ ls
Palindrome.c palindrome.sh
EXAMPLE : cd sun
OUTPUT :
SYNTAX : cd
OUTPUT :
[exam88@local host ~] $
OUTPUT :
[exam88@local host ~] $ rmdir sun
COMMAND : list
SYNTAX :ls
EXPLANATION : This command display all the files and directories of the system.
OUTPUT :
[exam88@local host ~] $ ls
Palindrome.c palindrome.sh
Morn.text good
COMMAND : cat
EXPLANATION : This command leads to the creation of a new file with the specified filename
content.
OUTPUT :
COMMAND : cat
OUTPUT :
COMMAND : copy
EXPLANATION: This command produces a copy of the source file to be stored in the specified
destination file of overwriting its previous content if exist.
OUTPUT :
EXPLANATION: This command produces a copy of the source file to be stored in the specified
destination file(by creating it) .
COMMAND : move
EXPLANATION : This command moves the content of the source file to destination file. The
content of the source file is deleted.
OUTPUT :
COMMAND : cut
EXPLANATION : This command displays the character of particular column in the specified field.
OUTPUT :
good m
a Nic
COMMAND : remove
EXAMPLE : rm star.txt
EXPLANATION : This command deletes the specified file from the directory.
OUTPUT :
16. TASK : This command prints the line matching the pattern. grep(globally search
a regular expression and print)
COMMAND : grep
OUTPUT :
#include<sys/stat.h>
s=stat("fork1.c",&st);
COMMAND : chmod
COMMAND : sort
EXPLANATION: This command helps in sorting the content of a file in ascending order.
OUTPUT :
EXAMPLE : wc moon.txt
EXPLANATION : This command displays the no.of rows, words and character in the file.
OUTPUT :
2 7 33 moon.txt
COMMAND : man
EXPLANATION : This command formats and displays the online manual pages.
COMMAND : clear
Note:
SYNTAX : vi filename
EXAMPLE : vi fork.c
Ex.No.2 SHELL PROGRAMMING
Arithmetic Operators:
Relational Operators:
-ne Checks if the value of two operands are equal or [ $a -ne $b ] is true.
not, if values are not equal then condition becomes
true.
Note:All the conditional expressions would be put inside square braces with one space around them
Boolean Operators:
PROGRAM:
read n
r=$(($n%2))
if [ $r -eq 0 ]
then
echo "EVEN"
else
echo "ODD"
fi
OUTPUT:
450
EVEN
[exam1@localhost ~]$
Ex.No.2b LINUX USING ECHO COMMAND
PROGRAM:
echo "3.DATE"
read n
case $n in
1) whoami;;
2) who;;
3) date;;
esac
OUTPUT:
1.WHO AM I?
3.DATE
exam1
[exam1@localhost ~]$
Ex.No.2c FACTORIAL OF A NUMBER
PROGRAM:
read num
i=1
fact=1
do
fact=$(($fact*$i))
i=$(($i+1))
done
OUTPUT:
FACTORIAL OF 5 IS 120
Ex.No.2c GREATEST OF THREE NUMBERS
PROGRAM:
read a b c
if [ $a -gt $b -a $a -gt $c ]
then
elif [ $b -gt $c ]
then
else
fi
OUTPUT:
23 98 -1
98 IS GREATEST
Ex.No.3a IMPLEMENTATION OF FCFS SCHEDULING
PROGRAM:
#include<stdio.h>
#define MAX 10
struct process
{
int pro;
int brt;
int tt;
int wt;
}pr[MAX];
// main program
int main()
{
int i,j,n,totwt,tottt;
float avgwt,avgtt;
int prevtt;
printf("\n\t\t First Come First Serve \n");
printf("\t\t ~~~~~ ~~~~ ~~~~~ ~~~~~ \n");
printf("\n\t Enter the no of process:");
scanf("%d",&n);
//Getting Process no. and Burst Time
for(i=1;i<=n;i++)
{
pr[i].pro = i;
printf("\nProcess no: %d ",pr[i].pro);
printf("\tEnter burst time:");
scanf("%d",&pr[i].brt);
}
//calculating Waiting time and Turn Aruond Time
prevtt =0;
for(i=1;i<=n;i++)
{
pr[i].wt=prevtt;
pr[i].tt=pr[i].wt+pr[i].brt;
prevtt = pr[i].tt;
}
printf("\n PROCESS NO \t BURST TIME \t WAITING TIME \t TURN AROUND
TIME");
printf("\n ~~~~~~~~~~ \t ~~~~~~~~~~ \t ~~~~~~~~~~~~ \t ~~~~~~~~~~~~~~~~");
//Sum of Waiting time and Turn Aruond Time
totwt=0;
tottt=0;
for(i=1;i<=n;i++)
{
printf("\n\t%d\t\t%d\t\t%d\t\t%d",pr[i].pro,pr[i].brt,pr[i].wt,pr[i].tt);
totwt=totwt+pr[i].wt;
tottt=tottt+pr[i].tt;
}
//Average of Waiting time and Turn Aruond Time
avgwt=(float)totwt/n;
avgtt=(float)tottt/n;
printf("\n\n Average Waiting time:%6.2f",avgwt);
printf("\n\n Average Turnaround Time:%6.2f",avgtt);
printf("\n\n");
// Top Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
//Gantt Chart
j=pr[1].wt;
printf("%d",j);
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].brt,pr[i].tt);
}
printf("\n");
// Bottom Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
getch();
return 0;
}
OUTPUT:
First Come First Serve
~~~~~ ~~~~ ~~~~~ ~~~~~
Enter the no of process:5
Process no: 1 Enter burst time:8
Process no: 2 Enter burst time:3
Process no: 3 Enter burst time:4
Process no: 4 Enter burst time:2
Process no: 5 Enter burst time:6
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 50
struct process
{
int pro;
int brt;
int tt;
int wt;
}pr[MAX];
// Main program
int main()
{
int i,j,n,t,totwt,tottt;
float avgwt,avgtt;
int prevtt;
clrscr();
printf("\n\t\t\t SHORTEST JOB FIRST");
printf("\n\n Enter the no of process:");
scanf("%d",&n);
//Getting Process no. and Burst Time
for(i=1;i<=n;i++)
{
pr[i].pro = i;
printf("\nProcess no: %d ",pr[i].pro);
printf("\nEnter burst time:");
scanf("%d",&pr[i].brt);
}
// Sorting the Burst Time in Ascending order
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(pr[i].brt> pr[j].brt)
{
t= pr[j].pro;
pr[j].pro= pr[i].pro;
pr[i].pro=t;
t= pr[j].brt;
pr[j].brt= pr[i].brt;
pr[i].brt=t;
}
}
}
//calculating Waiting time and Turn Aruond Time
prevtt =0;
for(i=1;i<=n;i++)
{
pr[i].wt=prevtt;
pr[i].tt=pr[i].wt+pr[i].brt;
prevtt = pr[i].tt;
}
//Sum of Waiting time and Turn Aruond Time
printf("\n PROCESS NO \t BURST TIME \t WAITING TIME \t TURN AROUND
TIME");
printf("\n ------------- \t ------------- \t---------- \t --------------");
totwt=0;
tottt=0;
for(i=1;i<=n;i++)
{
printf("\n\t%d\t\t%d\t\t%d\t\t%d",pr[i].pro,pr[i].brt,pr[i].wt,pr[i].tt);
totwt=totwt+pr[i].wt;
tottt=tottt+pr[i].tt;
}
//Average of Waiting time and Turn Aruond Time
avgwt=(float)totwt/n;
avgtt=(float)tottt/n;
printf("\n\n Average Waiting time:%6.2f",avgwt);
printf("\n\n Average Turnaround Time:%6.2f\n\n",avgtt);
// Top Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
//Gantt Chart
j=pr[1].wt;
printf("%d",j);
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].brt,pr[i].tt);
}
printf("\n");
// Bottom Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
getch();
return 0;
}
OUTPUT:
SHORTEST JOB FIRST
Enter the no of process:4
Process no: 1
Enter burst time:6
Process no: 2
Enter burst time:8
Process no: 3
Enter burst time:7
Process no: 4
Enter burst time:3
RESULT:
Ex.No.3c IMPLEMENTATION OF PRIORITY SCHEDULING
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 50
struct process
{
int pro;
int brt;
int tt;
int wt;
int pri;
}pr[MAX];
// Main program
int main()
{
int i,j,n,t,totwt,tottt;
float avgwt,avgtt;
int prevtt;
clrscr();
printf("\n\t\t\t PRIORITY SCHEDULING");
printf("\n\n Enter the no of process:");
scanf("%d",&n);
//Getting Process no. and Burst Time
for(i=1;i<=n;i++)
{
pr[i].pro = i;
printf("\nProcess no: %d ",pr[i].pro);
printf("\nEnter burst time:");
scanf("%d",&pr[i].brt);
printf("\nEnter Priority:");
scanf("%d",&pr[i].pri);
}
// Sorting the Priority in Ascending order
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(pr[i].pri> pr[j].pri)
{
t= pr[j].pro;
pr[j].pro= pr[i].pro;
pr[i].pro=t;
t= pr[j].brt;
pr[j].brt= pr[i].brt;
pr[i].brt=t;
t= pr[j].pri;
pr[j].pri= pr[i].pri;
pr[i].pri=t;
}
}
}
//calculating Waiting time and Turn Aruond Time
prevtt =0;
for(i=1;i<=n;i++)
{
pr[i].wt=prevtt;
pr[i].tt=pr[i].wt+pr[i].brt;
prevtt = pr[i].tt;
}
//Sum of Waiting time and Turn Aruond Time
printf("\n PROCESS NO \t BURST TIME \t WAITING TIME \t TURN AROUND
TIME");
printf("\n ------------- \t ------------- \t---------- \t --------------");
totwt=0;
tottt=0;
for(i=1;i<=n;i++)
{
printf("\n\t%d\t\t%d\t\t%d\t\t%d",pr[i].pro,pr[i].brt,pr[i].wt,pr[i].tt);
totwt=totwt+pr[i].wt;
tottt=tottt+pr[i].tt;
}
//Average of Waiting time and Turn Aruond Time
avgwt=(float)totwt/n;
avgtt=(float)tottt/n;
printf("\n\n Average Waiting time:%6.2f",avgwt);
printf("\n\n Average Turnaround Time:%6.2f\n\n",avgtt);
// Top Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
//Gantt Chart
j=pr[1].wt;
printf("%d",j);
for(i=1;i<=n;i++)
{
printf("%*d",pr[i].brt,pr[i].tt);
}
printf("\n");
// Bottom Line
for(i=1;i<=pr[n].tt;i++)
{
printf("-");
}
printf("\n");
getch();
return 0;
}
OUTPUT:
PRIORITY SCHEDULING
Enter the no of process:5
Process no: 1
Enter burst time:10
Enter Priority:3
Process no: 2
Enter burst time:1
Enter Priority:1
Process no: 3
Enter burst time:2
Enter Priority:4
Process no: 4
Enter burst time:1
Enter Priority:5
Process no: 5
Enter burst time:5
Enter Priority:2
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 50
struct process
{
int pro;
int brt;
int tt;
int wt;
int remtime;
}pr[MAX];
// Main program
void main()
{
int i,j,n,t,totwt,tottt;
float avgwt,avgtt;
int ts;
int totbrt;
printf("\n Enter the no of process:");
scanf("%d",&n);
printf("\nEnter the time slice:");
scanf("%d",&ts);
//Getting Process no. and Burst Time
totbrt = 0;
for(i=1;i<=n;i++)
{
printf("\nEnter process no: %d \n", i);
pr[i].pro = i;
printf("\nEnter burst time:");
scanf("%d",&pr[i].brt);
pr[i].remtime = pr[i].brt;
totbrt = totbrt + pr[i].brt;
}
//calculating Waiting time and Turn Around Time
tottt = 0;
while (1)
{
if(tottt == totbrt)
{
break;
}
for(i=1;i<=n;i++)
{
if(pr[i].remtime == 0)
{
continue;
}
if(pr[i].remtime > ts)
{
tottt = tottt + ts;
pr[i].remtime = pr[i].remtime - ts;
}
else
{
tottt = tottt + pr[i].remtime;
pr[i].remtime = 0;
}
pr[i].wt = tottt-pr[i].brt;
pr[i].tt = tottt;
}
}
//Sum of Waiting time and Turn Around Time
printf("\n PROCESS NO \t BURST TIME \t WAITING TIME \t TURN AROUND
TIME");
printf("\n ------------- \t ------------- \t---------- \t --------------");
totwt=0;
tottt=0;
for(i=1;i<=n;i++)
{
printf("\n\t%d\t\t%d\t\t%d\t\t%d",pr[i].pro,pr[i].brt,pr[i].wt,pr[i].tt);
totwt=totwt+pr[i].wt;
tottt=tottt+pr[i].tt;
}
//Average of Waiting time and Turn Around Time
avgwt=(float)totwt/n;
avgtt=(float)tottt/n;
printf("\n\n Average Waiting time:%6.2f",avgwt);
printf("\n\n Average Turnaround Time:%6.2f",avgtt);
getch();
}
}
OUTPUT:
Enter the no of process:
3
Enter the time slice:4
Enter process no: 1
Enter burst time:24
Enter process no: 2
Enter burst time:3
Enter process no: 3
Enter burst time:3
PROCESS NO BURST TIME WAITING TIME TURN AROUND TIME
------------- ------------- ---------- --------------
1 24 6 30
2 3 4 7
3 3 7 10
PROGRAM:
#include<stdio.h>
int main()
{
inti,j,n,block[20],start;
printf("Enter the no. of file : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the number of blocks needed for file %d: ", i);
scanf("%d",&block[i]);
}
start=0;
for(i=1;i<=n;i++)
{
printf("File %d Start %d Size %d\n",i,start,block[i]);
start=start+block[i];
}
}
Output:
Enter the no of file:2
Enter the number of blocks needed for file1:3
Enter the no of blocks needed for file2: 2
File1 start 0 size 3
File 2 start 3 size 2
Result:
Ex.No.4b INDEXED ALLOCATION
PROGRAM:
#include<stdio.h>
main()
{
int f[50],i,n,st,len,k;
for(i=0;i<50;i++)
{
f[i]=1;
}
printf("How many blocks are already allocated ?");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the allocated block numbers ");
scanf("%d",&k);
f[k]=0;
}
printf("Enter the starting index block & length");
scanf("%d %d",&st,&len);
i=0;
k=st;
while(i<len)
{
if(f[k]==1)
{
f[k]=0;
printf("\n %d-> file allocating",k);
i++;
}
else
{
printf("\n %d->file is already allocated",k);
}
k++;
}
}
OUTPUT:
[itstu003@localhost ~]$ ./a.out
How many blocks are already allocated ?2
Enter the allocated block numbers 2
Enter the allocated block numbers 4
Enter the starting index block & length2 3
2->file is already allocated
3-> file allocating
4->file is already allocated
5-> file allocating
6-> file allocating
How many blocks are already allocated? 2
Enter the allocated block numbers 2
Enter the allocated block numbers 4
Enter the starting index block & length 2 3
2->file is already allocated
3->file allocating
4-> file is already allocated
5-> file allocating
6-> file allocating
Result:
Ex.No.4c LINKED ALLOCATION
PROGRAM:
#include<stdio.h>
typedefstruct file
{
char name[10];
intstart,size,block[10];
}fil;
main()
{
fil f[10];
inti,j,n;
printf("Enter number of files: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the filename: ");
scanf("%s",f[i].name);
printf("Enter the starting block of the file: ");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter the size of the file: ");
scanf("%d",&f[i].size);
for(j=1;j<f[i].size;j++)
{
printf("Enter the next block number:");
scanf("%d",&f[i].block[j]);
}
}
printf("File \t Start \t Size \t Blocks \n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].name,f[i].start,f[i].size);
for(j=0;j<f[i].size;j++)
printf("%d-->",f[i].block[j]);
printf("-1 \n");
}
printf("\n");
}
Output:
Enter number of files2
Enter the filename: a
Enter the starting block of the file: 5
Enter the size of the file: 5
Enter the next block number: 11
Enter the next block number: 12
Enter the next block number: 14
Enter the next block number: 13
Result:
Ex.No.5 DINING PHILOSOPHER'S PROBLEM USING SEMAPHORE
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define N 5
#define LEFT (j-1)%N
#define RIGHT (j+1)%N
#define THINKING 0
#define HUNGRY 1
#define EATING 2
int state[N];
void test(int);
intlt, rt;
void main()
{
intf,ch=0,i,a,b;
inttake_sticks(int);
intput_sticks(int);
clrscr();
printf("\n \t *** DINING PHILOSOPHER PROBLEM**\n");
printf("ENTER THE NUMBER OF PHILOSOPHER IN THE TABLE IS %d:",N);
printf("\n STATUS OF THE PHILOSOPHER:\n");
for(i=0;i<N;i++)
{
state[i]=THINKING;
printf("\n P%d IS THINKING",i);
}
do
{
printf("\n\t\t MENU\n\n");
printf("\n\t1.START EATING\n\n");
printf("\n\t2.STOP EATING\n\n");
printf("\n\t3.EXIT\n\n");
printf("ENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n ENTER THE PHILOSOPHER WHO IS GOING TO EAT:");
scanf("%d",&a);
f=take_sticks(a);
if(f==EATING)
{
printf("\n THE PHILOSOPHER HAS STARTED EATING...\n\n");
}
else
{
printf("\nTHE PHILOSOPHER IS HUNGRY!!..\n\n");
printf("\n THE CHOPSTICK IS ENGAGED\n\n");
}
for(i=0;i<N;i++)
{
if(state[i]==THINKING)
{
printf("\nP%d IS THINKING",i);
}
else if(state[i]==HUNGRY)
{
printf("\nP%d IS HUNGRY",i);
}
else
{
printf("\nP%d IS EATING",i);
}
}
break;
case 2:
printf("\n ENTER THE PHILOSOPHER NUMBER TO RELEASE THE CHOPSTICK");
scanf("%d",&b);
if(state[b]==EATING)
{
f=put_sticks(b);
printf("\n THE PHILOSOPHER HAS STOPPED EATING!!..\n\n");
}
else
{
printf("\n THE PHILOSOPHER IS NOT EATING!!\n\n");
}
for(i=0;i<N;i++)
{
if(state[i]==THINKING)
{
printf("\nP%d IS THINKING",i);
}
else if(state[i]==HUNGRY)
{
printf("\nP%d IS HUNGRY",i);
}
else
{
printf("\nP%d IS EATING",i);
}
}
break;
case 3:
exit(0);
break;
}
}
while(ch!=3);
getch();
}
inttake_sticks(int j)
{
state[j]=HUNGRY;
test(j);
return state[j];
}
intput_sticks(int j)
{
state[j]=THINKING;
if(j==0) test(N-1); else test(LEFT);
if(j==N-1) test(0); else test(RIGHT);
return state[j];
}
void test(int j)
{
if(j==0) rt = N-1; else rt = LEFT;
if(j==N-1) lt = 0; else lt = RIGHT;
if(state[j]==HUNGRY && state[lt]!=EATING && state[rt]!=EATING)
{
state[j]=EATING;
}
}
OUTPUT:
P0 IS THINKING
P1 IS THINKING
P2 IS THINKING
P3 IS THINKING
P4 IS THINKING
MENU
1.START EATING
2.STOP EATING
3.EXIT
P0 IS THINKING
P1 IS EATING
P2 IS THINKING
P3 IS THINKING
P4 IS THINKING
MENU
1.START EATING
2.STOP EATING
3.EXIT
P0 IS THINKING
P1 IS EATING
P2 IS THINKING
P3 IS EATING
P4 IS THINKING
MENU
1.START EATING
2.STOP EATING
3.EXIT
P0 IS HUNGRY
P1 IS EATING
P2 IS THINKING
P3 IS EATING
P4 IS THINKING
MENU
1.START EATING
2.STOP EATING
3.EXIT
P0 IS EATING
P1 IS THINKING
P2 IS THINKING
P3 IS EATING
P4 IS THINKING
RESULT:
Ex.No.6a SINGLE LEVEL DIRECTORY
Program:
#include<stdio.h>
#include<conio.h>
main()
{
intmaster,s[20];
char f[20][20][20];
char d[20][20];
inti,j;
clrscr();
printf("enter number of directories:");
scanf("%d",&master);
printf("enter names of directories:");
for(i=0;i<master;i++)
scanf("%s",&d[i]);
printf("enter size of directories:");
for(i=0;i<master;i++)
scanf("%d",&s[i]);
printf("enter the file names :");
for(i=0;i<master;i++)
for(j=0;j<s[i];j++)
scanf("%s",&f[i][j]);
printf("\n");
printf(" directory\tsize\tfilenames\n");
printf("*************************************************\n");
for(i=0;i<master;i++)
{
printf("%s\t\t%2d\t",d[i],s[i]);
for(j=0;j<s[i];j++)
printf("%s\n\t\t\t",f[i][j]);
printf("\n");
}
printf("\t\n");
getch();
}
Output:
enter number of directories:2
enter names of directories:a
b
enter size of directories:2
2
enter the file names:ab
bc
cd
ef
directory size filenames
*****************************
a 2 ab
bc
b 2 cd
ef
Result:
Ex.No.6b TWO LEVEL DIRECTORIES
Program:
#include<stdio.h>
#include<conio.h>
structst
{
chardname[10];
charsdname[10][10];
charfname[10][10][10];
intds,sds[10];
}dir[10];
void main()
{
inti,j,k,n;
clrscr();
printf("enter number of directories:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter directory %d names:",i+1);
scanf("%s",&dir[i].dname);
printf("enter size of directories:");
scanf("%d",&dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("enter subdirectory name and size:");
scanf("%s",&dir[i].sdname[j]);
scanf("%d",&dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
{
printf("enter file name:");
scanf("%s",&dir[i].fname[j][k]);
}
}
}
printf("\ndirname\t\tsize\tsubdirname\tsize\tfiles");
printf("\n******************************************************\n");
for(i=0;i<n;i++)
{
printf("%s\t\t%d",dir[i].dname,dir[i].ds);
for(j=0;j<dir[i].ds;j++)
{
printf("\t%s\t\t%d\t",dir[i].sdname[j],dir[i].sds[j]);
for(k=0;k<dir[i].sds[j];k++)
printf("%s\t",dir[i].fname[j][k]);
printf("\n\t\t");
}
printf("\n");
}
getch();
}
Output:
enter number of directories:2
enterdirectory 1 names:a
enter size of directories:1
enter subdirectory name and size:b 1
enter file name:b
enterdirectory 2 names:c
enter size of directories:1
enter subdirectory name and size:d 1
enter file name:e
dirname size subdirname size files
*******************************************
a 1 b 1 b
c 1 d 1 e
Result:
Ex.No.7 BANKER’S ALGORITHM FOR DEADLOCK AVOIDANCE
PROGRAM:
#include<stdio.h>
#include<conio.h>
void matrix_get(int[5][5],int,int);
void main()
{
int max[5][5],all[5][5],total[1][5];
int avail[1][5],f[5],need[5][5],state[5];
int s,i,j,p,r,c,ch,ch1,k=0;
clrscr();
printf("\t\t DEADLOCK -BANKER'S ALGORITHM \n");
printf("ENTER THE NUMBER OF PROCESS:");
scanf("%d",&p);
ch=p;
printf("ENTER THE NUMBER OF RESOURCE TYPE:");
scanf("%d",&r);
printf("ENTER THE MAX MATRIX:\n");
matrix_get(max,p,r);
printf("\nENTER THE ALLOCATED MATRIX:\n");
matrix_get(all,p,r);
printf("ENTER THE TOTAL RESOURCE PRESENT:\n");
matrix_get(total,1,r);
for(i=0;i<r;i++)
{
for(j=0;j<p;j++)
{
if(max[j][i]>total[0][i]||all[j][i]>total[0][i])
{
printf("\nINPUT IS > TOTAL RESOURCE PRESENT\n");
exit(0);
}
}
s=total[0][i];
for(j=0;j<p;j++)
{
s=s-all[j][i];
}
avail[0][i]=s;
}
printf("\n\n AVAILABLE MATRIX \n\n");
for(i=0;i<r;i++)
{
printf("%d\t",avail[0][i]);
}
printf("\n\n NEED MATRIX:\n\n");
for(i=0;i<p;i++)
{
f[i]=0;
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-all[i][j];
if(need[i][j]<0)
{
need[i][j]=0;
}
printf("%d\t",need[i][j]);
}
printf("\n");
}
while(ch>0)
{
ch1=ch;
for(i=0;i<p;i++)
{
if(f[i]==0)
{
c=0;
for(j=0;j<r;j++)
{
if(need[i][j]<=avail[0][j])
{
c++;
}
}
if(c==r)
{
f[i]=1;
state[k++]=i;
ch--;
for(j=0;j<r;j++)
{
avail[0][j]=avail[0][j]+all[i][j];
need[i][j]=0;
}
}
}
}
if(ch==p)
{
printf("\n ALREADY DEADLOCK OCCURRED");
exit(0);
}
if(ch1==ch)
{
printf("\n ALLOCATION LEADS TO DEADLOCK");
exit(0);
}
}
OUTPUT:
DEADLOCK -BANKER'S ALGORITHM
ENTER THE NUMBER OF PROCESS:5
ENTER THE NUMBER OF RESOURCE TYPE:3
ENTER THE MAX MATRIX:
753
322
902
222
433
AVAILABLE MATRIX
3 3 2
NEED MATRIX:
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1
RESULT:
Ex.No.8 IMPLEMENTATION OF DEADLOCK DETECTION
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int found,flag,l,p[4][5],tp,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;
clrscr();
printf("enter total no of processes");
scanf("%d",&tp);
printf("enter clain matrix");
for(i=1;i<=4;i++)
for(j=1;j<=5;j++)
{
scanf("%d",&c[i][j]);
}
printf("enter allocation matrix");
for(i=1;i<=4;i++)
for(j=1;j<=5;j++)
{
scanf("%d",&p[i][j]);
}
printf("enter resource vector:\n");
for(i=1;i<=5;i++)
{
scanf("%d",&r[i]);
}
printf("enter availability vector:\n");
for(i=1;i<=5;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=1;i<=4;i++)
{
sum=0;
for(j=1;j<=5;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=1;i<=4;i++)
{
for(l=1;l<k;l++)
if(i!=m[l])
{
flag=1;
for(j=1;j<=5;j++)
if(c[i][j]>temp[j])
{
flag=0;
break;
}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j<=5;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are:");
for(j=1;j<=tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
getch();
}
INPUT:
enter total no. of processes : 4
enter claim matrix :
01001
00101
00001
10101
enter allocation matrix :
10110
11000
00010
00000
enter resource vector :
21121
enter the availability vector :
00001
OUTPUT :
deadlock causing processes are : 1 2
Result:
Ex.No.9a FIFO PAGE REPLACEMENT
PROGRAM:
#include<stdio.h>
#include<conio.h>
intnf;
int ne;
inthit,miss;
int ref[50];
intfr[15];
void getdata();
void fifo();
int large(int []);
void getdata()
{
inti;
printf("\nEnter number of frames:");
scanf("%d",&nf);
printf("\nEnter number of elements in the reference string");
scanf("%d",&ne);
printf("\nEnter the reference string");
for(i=0;i<ne;i++)
{
scanf("%d",&ref[i]);
}
}
void fifo()
{
inti,j,found=0,rear=-1;
miss=hit=0;
for(i=0;i<nf;i++)
{
fr[i]=0;
}
for(i=0;i<ne;i++)
{
printf("\nThe no to be inserted :%d",ref[i]);
found=search(ref[i]);
if(found==0)
{
miss++;
if(rear==nf-1)
{
rear=-1;
}
rear++;
fr[rear]=ref[i];
}
else
{
hit++;
}
printf("\n");
for(j=0;j<nf;j++)
printf("%d ",fr[j]);
}
printf("\n No. of page faults=%d",miss);
printf("\n No. of Hits=%d",hit);
}
int search(int item)
{
inti;
for(i=0;i<nf;i++)
{
if(fr[i]==item)
{
return(1);
}
}
return 0;
}
void main()
{
getdata();
printf("\n FIFO PAGE REPLACEMENT");
fifo();
getch();
}
Output:
[itstu003@localhost ~]$ cc fifo.c
fifo.c:2:18: conio.h: No such file or directory
fifo.c: In function `main':
fifo.c:93: warning: return type of 'main' is not `int'
[itstu003@localhost ~]$ ./a.out
Number of blocks in memory :3
Size of block:2
Number of jobs :3
Size of job 1:4
Size of job 2:1
Size of job 3:2
job 1 stored in memory block 1
job 1 stored in memory block 2
job 2 stored in memory block 3
RESULT:
Ex.No.9b LRU PAGE REPLACEMENT
PROGRAM:
#include<stdio.h>
intnf;
inti,ne;
int miss=0;
int ref[50];
intfr[15];
void getdata();
void lru();
void getdata()
{
printf("\nEnter the number of frames:");
scanf("%d",&nf);
printf("\nEnter the number of elements in the reference string:\n");
scanf("%d",&ne);
printf("\nEnter the reference string:\n");
for(i=0;i<ne;i++)
{
scanf("%d",&ref[i]);
}
}
void lru()
{
intcheck,j,k;
int count=0,index[5];
printf("\n\n Simulation of LRU page replacement technique!...........\n");
for(i=0;i<nf;i++)
{
fr[i]=-1;
}
for(i=0;i<ne;i++)
{
check=0;
for(j=0;j<nf;j++)
{
if(fr[j]==ref[i])
{
check=1;
}
}
if(check==0)
{
miss+=1;
if(count<nf)
{
fr[count++] = ref[i];
}
else
{
for(j=0;j<i;j++)
{
for(k=0;k<nf;k++)
{
if(ref[j]==fr[k])
{
index[k] = j;
}
}
}
for(j=0;j<nf;j++)
{
if(index[check]>index[j])
{
check=j;
}
}
fr[check]=ref[i];
}
printf("\n");
for(j=0;j<nf;j++)
{
printf("%d\t",fr[j]);
}
}
}
printf("\n Number of page faults:%d",miss);
}
void main()
{
clrscr();
printf("\n\t\t\t LRU PAGE REPLACEMENT");
getdata();
printf("\t\t\t LRU PAGE REPLACEMENT");
lru();
getch();
}
OUTPUT:
LRU PAGE REPLACEMENT
Enter the number of frames:3
Enter the number of elements in the reference string:
6
Enter the reference string:
1
2
3
4
2
1
LRU PAGE REPLACEMENT
Simulation of LRU page replacement technique!...........
1 -1 -1
1 2 -1
1 2 3
4 2 3
4 2 1
Number of page faults:5
RESULT:
Ex.No.9c LFU PAGE REPLACEMENT
PROGRAM:
#include<stdio.h>
int n;
main()
{
intseq[30],fr[5],pos[5],find,flag,max,i,j,m,k,t,s,pf=0;
int count=1,p=0;
float pfr;
clrscr();
printf("enter max limit of the sequence:");
scanf("%d",&max);
printf("enter the sequence:");
for(i=0;i<max;i++)
scanf("%d",&seq[i]);
printf("enter the no of frames:");
scanf("%d",&n);
fr[0]=seq[0];
pf++;
printf("%d\t",fr[0]);
i=1;
while(count<n)
{
flag=1;
p++;
for(j=0;j<i;j++)
{
if(seq[i]==seq[j])
flag=0;
}
if(flag!=0)
{
fr[count]=seq[i];
printf("%d\t",fr[count] );
count++;
pf++;
}
i++;
}
printf("\n");
for(i=p;i<max;i++)
{
flag=1;
for(j=0;j<n;j++)
{
if(seq[i]==fr[j])
flag=0;
}
if(flag!=0)
{
for(j=0;j<n;j++)
{
m=fr[j];
for(k=i;k<max;k++)
{
if(seq[k]==m)
{
pos[j]=k;
break;
}
else
pos[j]=-1;
}
}
for(k=0;k<n;k++)
{
if(pos[k]==-1)
flag=0;
}
if(flag!=0)
s=findmax(pos);
if(flag==0)
{
for(k=0;k<n;k++)
{
if(pos[k]==-1)
{
s=k;
break;
}
}
}
pf++;
fr[s]=seq[i];
for(k=0;k<n;k++)
printf("%d\t",fr[k]);
printf("\n");
}
}
pfr=(float)pf/(float)max;
printf("\n theno of page faults are:%d",pf);
printf("\n page fault rate:%f",pfr);
getch();
}
intfindmax(int a[])
{
intmax,i,k=0;
max=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])
{
max=a[i];
k=i;
}
}
return k;
}
Output:
enter the limit of the sequence:10
enter the sequence: 2 1 3 4 2 1 1 2 3 4
enter the no of frames:2
2 1
2 3
2 4
2 1
3 1
4 1
the no of page faults are:7
page fault rate:0.70000
RESULT:
Ex.No: 10 INTERPROCESS COMMUNICATION USING SHARED MEMORY
PROGRAM:
Server Program:
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define SHMSIZE 25
void main()
{
char c;
intshmid;
key_t key;
char *shm ,*s;
key = 9647;
printf("\n\t SHARED MEMORY SERVER PROGRAM \n");
if((shmid = shmget(key,SHMSIZE,IPC_CREAT|0666))<0)
{
perror("\n SHARED MEMORY CREATE ERROR");
exit(0);
}
if((shm = shmat(shmid,0,0))==(char *) -1)
{
perror("\n SHARED MEMORY ALLOCATED ERROR");
exit(1);
}
s = shm;
for(c='A';c<='Z';c++)
{
*s++ = c;
*s = '\0';
}
exit(0);
}
Client Program:
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#define SHMSIZE 0
void main()
{
char c;
intshmid;
key_t key;
char *shm ,*s;
key = 9647;
printf("\n\t SHARED MEMORY CLIENT PROGRAM\n ");
if((shmid = shmget(key,SHMSIZE,IPC_CREAT|0666))<0)
{
perror("\n SHARED MEMORY CREATE ERROR");
exit(0);
}
if((shm = shmat(shmid,0,0))==(char *) -1)
{
perror("\n SHARED MEMORY ALLOCATED ERROR");
exit(1);
}
for(s=shm;*s!='\0';s++)
{
printf("\t");
putchar(*s);
}
exit(0);
}
Output:
[exam01@localhost ~]$ cc ser_shamem.c
[exam01@localhost ~]$ cc cli_shamem.c
[exam01@localhost ~]$ ./a.out
SHARED MEMORY CLIENT SERVER PROGRAM
A B C D E F G H I
J K L M N O P Q R S
T U V W X Y Z
Result:
Ex.No.10b INTERPROCESS COMMUNICATION USING PIPES
PROGRAM:
#include<stdio.h>
int main()
{
intfd[2],child;
char a[10];
char b[10];
printf("\n Enter the string to enter into the pipe:");
scanf("%s",&a);
pipe(fd);
child=fork();
if(child>0)
{
write(fd[1],a,10);
}
else
{
read(fd[0],b,10);
printf("\n The string retrieved from the pipe is %s\n",b);
}
return 0;
}
OUTPUT:
Enter the string to enter into the pipe: Operating system
The string retrieved from the pipe is Operating system
Result:
Ex.No.11 Memory Management Scheme – PAGING
PROGRAM:
#include<stdio.h>
int main()
{
intnb,sb,nj,i,j;
intjs[10];
printf("Number of blocks in memory :");
scanf("%d",&nb);
printf("Size of block:");
scanf("%d",&sb);
printf("Number of jobs :");
scanf("%d",&nj);
for(j=1;j<=nj;j++)
{
printf("Size of job %d:", j);
scanf("%d",&js[j]);
}
j=1;
for(i=1;i<=nb;i++)
{
printf("job %d stored in memory block %d\n ", j,i);
js[j] = js[j] - sb;
if(js[j] <= 0)
{
j++;
}
if(j >nj)
{
break;
}
}
if(i>nb)
{
printf("Out of memory");
}
return 0;
}
Output:
Number of blocks in memory:2
Size of each block:50
Number of jobs:3
Size of job 1:10
Size of job 2:20
Size of job 3:10
Job1 stored in memory block 1
Job2 stored in memory block 2
Result: