Académique Documents
Professionnel Documents
Culture Documents
in
0032
OBJECTIVES:
The student should be made to:
Learn shell programming and the use of filters in the UNIX environment.
Be exposed to programming in C using system calls.
Learn to use the file system related system calls.
Be exposed to process creation and inter process communication.
Be familiar with implementation of CPU Scheduling Algorithms, page replacement
algorithms and Deadlock avoidance
LIST OF EXPERIMENTS:
1. Basics of UNIX commands.
2. Shell Programming.
3. Implement the following CPU scheduling algorithms
a) Round Robin b) SJF c) FCFS d) Priority
4. Implement all file allocation strategies
a) Sequential b) Indexed c) Linked
5. Implement Semaphores
6. Implement all File Organization Techniques
a) Single level directory b) Two level c) Hierarchical d) DAG
7. Implement Bankers Algorithm for Dead Lock Avoidance
8. Implement an Algorithm for Dead Lock Detection
9. Implement e all page replacement algorithms
a) FIFO b) LRU c) LFU
10. Implement Shared memory and IPC
11. Implement Paging Technique of memory management.
12. Implement Threading & Synchronization Applications
TOTAL: 45 PERIODS
OUTCOMES:
At the end of the course, the student should be able to
Implement deadlock avoidance, and Detection Algorithms
Compare the performance of various CPU Scheduling Algorithm
Critically analyze the performance of the various page replacement algorithms
Create processes and implement IPC
REFERENCE:
spoken-tutorial.org
List of Experiments
1.
2.
Shell Programming
3.
4.
5.
7.
8.
6.
10.
11.
12.
13.
Model Test
9.
Ex.No : 1
UNIX COMMANDS
I. Basic Commands:
1. Echo Command :
This command is used to print the arguments on the screen .
Syntax : $echo <text>
Multi line echo command :
To have the output in the same line , the following commands can be used.
Syntax : $echo <text\>text
To have the output in different line, the following command can be used.
Syntax : $echo text
>line2
>line3
2. who Command :
It is used to display who are the users connected to our computer currently.
- grep command searches the given file for lines containing a match to the given strings or
words. By default, grep prints the matching lines. Use grep to search for lines of text that match
one or many regular expressions, and outputs only the matching lines. The name, "grep", derives
from the command used to perform a similar operation, using the Unix/Linux text editor ed:
g/re/p
Syntax
grep 'word' filename
grep 'string1 string2' filename
cat otherfile | grep 'something'
command | grep 'something'
2. SORT : This command is used to sort the datas in some order.
Syntax: $sort<filename>
vi EDITOR
DESCRIPTION :
The Vi editor is a visual editor used to create and edit text, files, documents and
programs. It displays the content of files on the screen and allows a user to add, delete or
change part of text . There are three modes available in the Vi editor , they are
1.Command mode
2.Input (or) insert mode.
Starting Vi :
The Vi editor is invoked by giving the following commands in UNIX prompt.
Syntax : $vi <filename> (or)
$vi
This command would open a display screen with 25 lines and with tilt (~) symbol at the
start of each line. The first syntax would save the file in the filename mentioned and for the
next the filename must be mentioned at the end.
Options :
1.vi +n <filename> - this would point at the nth line (cursor pos).
2.vi n <filename> - This command is to make the file to read only to change from one
mode to another press escape key.
INSERTING AND REPLACING COMMANDS :
To move editor from command node to edit mode, you have to press the <ESC> key.
For inserting and replacing the following commands are used.
1.ESC a Command :
This command is used to move the edit mode and start to append after the current
character.
Syntax : <ESC> a
2.ESC A COMMAND :
This command is also used to append the file , but this command append at the end of
current line.
Syntax : <ESC> A
3.<ESC> j :
This command is used to move down a single line or a number of lines.
Syntax :
<ESC> j single down movement.
<ESC> nj n times down movement.
4.<ESC> k :
This command is used to move up a single line or a number of lines.
SHELL PROGRAMMING
10
11
12
Ex. No:3(a)
Aim: Write a C program to implement the various process scheduling mechanisms such as
Round Robin Scheduling.
13
14
15
Process ID
BurstTime
Waiting Time
TurnaroundTime
10
15
25
15
25
40
20
25
45
25
20
45
16
17
12
18
19
11
20
21
1
2
3
7
4
6
2
5
0
4
10
12
4
10
12
17
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,b[20],sb[20],t[20],x,c[20][20];
clrscr();
printf("Enter no.of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of blocks occupied by file%d",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\n");
for(i=0;i<n;i++)
printf("%d\t %d \t%d\n",i+1,t[i],b[i]);
printf("Enter file name:");
scanf("%d",&x);
printf("File name is:%d",x);
printf("length is:%d",b[x-1]);
printf("blocks occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
}
OUTPUT:
Enter no.of files: 2
Enter no. of blocks occupied by file1 4
Enter the starting block of file1 2
23
24
25
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}
OUTPUT:
Enter no. of files:2
Enter file name:venkat
Enter starting block:20
Enter no.of blocks:6
Enter block numbers: 4
12
15
45
32
25
Enter file name:rajesh
Enter starting block:12
Enter no.of blocks:5
Enter block numbers:6
5
26
4
3
2
File start size
venkat 20
6
rajesh 12
5
block
4--->12--->15--->45--->32--->25
6--->5--->4--->3--->2
Result:
Thus the program for various Memory Allocation Techniques has been implemented
successfully.
Ex. No. 5
Semaphores
27
28
Algorithm:
1. Initialize values gd =DETECT,gm,count,I,j,mid,cir_x,fname[10][20];
2..Read number of files in count.
3. Read file name in the loop.
4. calculate the mid value
5. Insert the file in the root directory.
6. Continue the step 5 until count =0
7.Stop the execution.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
Void main()
{
int gd=DETECT,gm,count,I,j,mid,cir_x;
char fname[10][20];
clrscr();
initgraph(&gd,&gm,c:\\tc\\bgi);
cleardevice();setbkcolor(Green);
puts(Enter no of files do u have?);
scanf(%d,&count);
for(i=0;i<count;i++)
{
Cleardevice();setbkcolor(Green);
Printf(Enter file %d name,i+1);
Scanf(%s,fname[i]);
Setfilstyle(1,MAGENTA);
Mid=640/count;
Cir_x=mid/3;
Bar3d(270,100,370,150,0,0);
Settextstyle(2,0,4);
Settextjustify(1,1);
Outtextxy(320,125,Root
Directory);setcolor(BLUE);
For(j=0;j<=I;j++,cir_x+=mid)
{
Line(320,150,cir_x,250);
Fillellipse(cir_x,250,30,30);
Outtextxy(cir_x,250,fname[j]);
}
getch();
}
}
OUTPUT:
Enter no of files do u
have?3
Enter file 1 name:1.c
29
1
.
ROOT
1
.
2
.
3
1
2
.
.
.
Result:
Thus the program to implement File Organization Technique - Single level directory
was executed and verified successfully
Algorithm:
1. Initialize values gd =DETECT,gm,count,I,j,mid,cir_x,fname[10][20];
2..Read number of files in count.
3. Read file name in the loop.
4. calculate the mid value
5. Insert the file in the root directory.
6. Continue the step 5 until count =0
7.Stop the execution.
Program:
#include<stdio.h>
main()
{
int nod,file[30][30],nof[10],i,j;
printf("\nEnter the numner of directory under root:\t");
scanf("%d",&nod);
for(i=0;i<nod;i++)
{
printf("Enter directory-%d name:\t",i+1);
scanf("%d",&file[i][0]);
printf("Enter number of files in the direcotyr:%d",i+1);
scanf("%d",&nof[i]);
for(j=1;j<nof;j++)
30
Result:
Thus the program to implement File Organization Technique - Two level directory
was executed and verified successfully
Algorithm:
1.
2.
3.
4.
5.
6.
Program:
#include<stdio.h>
main()
{
int n,nod,dir[20],nof,file[20],i,m,f;
a:
printf("If the current directory has subdir, Press !");
scanf("%d",&n);
if(n==1)
{
printf("Enter the number of directory");
scanf("%d",&nod);
for(i=0;i<nod;i++)
{
printf("Enter the directory-%d name:\t",i+1);
scanf("%d",&dir[i]);
}
}
printf("If the current directory has subdir, Press 2");
scanf("%d",&f);
if(f==2)
31
Result: Thus the program to implement File Organization Technique - Hierarchical directory was
executed and verified successfully
Ex. No: 7
BANKERS ALGORITHM
Data structures
Safety Algorithm
32
/* BANKERS ALGORITHM */
Program:
#include<stdio.h>
#include<conio.h>
struct da
{
int max[10],a1[10],need[10],before[10],after[10];
}p[10];
void main()
{
int i,j,k,l,r,n,tot[10],av[10],cn=0,cz=0,temp=0,c=0;
clrscr();
33
34
35
OUTPUT:
//TEST CASE 1:
ENTER THE NO. OF PROCESSES:4
ENTER THE NO. OF RESOURCES:3
PROCESS 1
MAXIMUM VALUE FOR RESOURCE 1:3
MAXIMUM VALUE FOR RESOURCE 2:2
MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:1
ALLOCATED FROM RESOURCE 2:0
ALLOCATED FROM RESOURCE 3:0
PROCESS 2
MAXIMUM VALUE FOR RESOURCE 1:6
MAXIMUM VALUE FOR RESOURCE 2:1
MAXIMUM VALUE FOR RESOURCE 3:3
ALLOCATED FROM RESOURCE 1:5
ALLOCATED FROM RESOURCE 2:1
ALLOCATED FROM RESOURCE 3:1
PROCESS 3
MAXIMUM VALUE FOR RESOURCE 1:3
MAXIMUM VALUE FOR RESOURCE 2:1
MAXIMUM VALUE FOR RESOURCE 3:4
ALLOCATED FROM RESOURCE 1:2
ALLOCATED FROM RESOURCE 2:1
ALLOCATED FROM RESOURCE 3:1
PROCESS 4
MAXIMUM VALUE FOR RESOURCE 1:4
MAXIMUM VALUE FOR RESOURCE 2:2
MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:0
ALLOCATED FROM RESOURCE 2:0
ALLOCATED FROM RESOURCE 3:2
ENTER TOTAL VALUE OF RESOURCE 1:9
ENTER TOTAL VALUE OF RESOURCE 2:3
ENTER TOTAL VALUE OF RESOURCE 3:6
RESOURCES ALLOCATED NEEDED
P1
322
100
222
P2
613
511
102
P3
314
211
103
P4
422
002
420
P2
P1
P3
TOTAL AVAIL
936 112
36
Ex.No.: 8
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++)
{
38
39
OUTPUT :
deadlock causing processes are : 1 2
Program:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
void main()
{
clrscr();
printf("\n \t\t\t FIFI PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of frames....");
scanf("%d",&nof);
printf("Enter number of reference string..\n");
scanf("%d",&nor);
printf("\n Enter the reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\nThe given reference string:");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
frm[i]=-1;
printf("\n");
40
5
5
5
5
2
2
-1
6
6
6
6
3
-1
-1
4
4
4
4
-1
-1
-1
1
1
1
Ex. No:9(b)
41
Program:
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],lrucal[50],count=0;
int lruvictim();
void main()
{
clrscr();
printf("\n\t\t\t LRU PAGE REPLACEMENT ALGORITHM");
printf("\n Enter no.of Frames....");
scanf("%d",&nof);
printf(" Enter no.of reference string..");
scanf("%d",&nor);
printf("\n Enter reference string..");
for(i=0;i<nor;i++)
scanf("%d",&ref[i]);
printf("\n\n\t\t LRU PAGE REPLACEMENT ALGORITHM ");
printf("\n\t The given reference string:");
printf("\n..");
for(i=0;i<nor;i++)
printf("%4d",ref[i]);
for(i=1;i<=nof;i++)
{
frm[i]=-1;
lrucal[i]=0;
}
for(i=0;i<10;i++)
recent[i]=0;
printf("\n");
for(i=0;i<nor;i++)
{
42
43
6
6
6
2
2
2
-1
5
5
5
3
3
-1
-1
4
4
4
1
Program
#include<stdio.h>
#include<conio.h>
int i,j,nof,nor,flag=0,ref[50],frm[50],pf=0,victim=-1;
int recent[10],optcal[50],count=0;
int optvictim();
void main()
{
clrscr();
printf("\n OPTIMAL PAGE REPLACEMENT ALGORITHN");
printf("\n.................................");
printf("\nEnter the no.of frames");
scanf("%d",&nof);
printf("Enter the no.of reference string");
scanf("%d",&nor);
printf("Enter the reference string");
for(i=0;i<nor;i++)
44
45
OUTPUT:
OPTIMAL PAGE REPLACEMENT ALGORITHM
Enter no.of Frames....3
Enter no.of reference string..6
Enter reference string..
654231
OPTIMAL PAGE REPLACEMENT ALGORITHM
The given reference string:
. 6 5 4 2 3 1
Reference NO 6->
Reference NO 5->
Reference NO 4->
Reference NO 2->
Reference NO 3->
Reference NO 1->
6
6
6
2
2
2
-1
5
5
5
3
3
-1
-1
4
4
4
1
Ex. No: 10
DEVELOPING APPLICATION USING INTER PROCESS COMMUNICATION -Pipes
and Shared memory
Aim:
To write a C program to implement inter process communication using pipes and shared
memory.
46
IPC - Pipe
Conceptually a pipe can be thought of much like a hose-pipe, in that it is a conduit
where we pour data in at one end and it flows out at the other. A pipe looks a lot like a file in that
it is treated as a sequential data stream. Unlike a file, a pipe has two ends, so when we create a
pipe we get two end points back in return. We can write to one end point and read from the other.
Also unlike a file, there is no physical storage of data when we close a pipe, anything that was
written in one end but not read out from the other end will be lost.
We can illustrate the use of pipes as conduits between processes in the following diagram:
Here we see two processes which I've called Parent and Child, for reasons that will become
apparent shortly. The Parent can write to Pipe A. and read from Pipe B. The Child can read from
Pipe A and write to Pipe B.
Algorithm:
1
Start the program.
2
Declare the file pointer fp, f1, f2.
3
Read the choice.
4
In case 1 open a file and display the list of files and directories.
5
In case 2 open a file and ptr bin\pwd for read mode it displays the personnel
working directory.
6
In case 3 open a file in write mode.
7
In case 4 Exit command is execute.
8
Stop the program.
Program
#include<stdio.h>
main(int argc,char * argv[])
{
int p1[2],p2[2],p3[2],p4[2];
int pid1,pid2,pid3,pid4;
47
48
CHARACTER COUNT: 60
LINE COUNT: 6
SENTENCE COUNT: 8
WORD COUNT: 20
Result:
Thus the Inter process communication has been implemented successfully using shared
memory and pipes.
49
Ex. No: 11
PAGING
50
OUTPUT:
51
FrameNo.
5
6
7
2
PresenceBit
1
1
1
1
PageNo
0
32555
1
32555
2
3
3
32555
4
32555
5
0
6
1
7
2
Process to create the Physical Address
Enter the Base Address: 1000
Enter theLogical Address: 3
The Physical Address where the instruction present: 1013
Result:
Thus the program for Paging has been implemented successfully
52
Ex.No.12
Implementation of Threading & Synchronization
Applications
Aim:
Program:
#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
#define N 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
#define LEFT (ph_num+4)%N
#define RIGHT (ph_num+1)%N
sem_t mutex;
sem_t S[N];
void * philospher(void *num);
void take_fork(int);
void put_fork(int);
void test(int);
int state[N];
int phil_num[N]={0,1,2,3,4};
int main()
{
int i;
pthread_t thread_id[N];
sem_init(&mutex,0,1);
for(i=0;i<N;i++)
sem_init(&S[i],0,0);
for(i=0;i<N;i++)
{
pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);
printf("Philosopher %d is thinking\n",i+1);
}
for(i=0;i<N;i++)
pthread_join(thread_id[i],NULL);
}
void *philospher(void *num)
{
while(1)
{
int *i = num;
sleep(1);
take_fork(*i);
sleep(0);
53
54
Best Fit
The allocator places a process in the smallest block of unallocated memory in which it will fit.
Problems:
It requires an expensive search of the entire free list to find the best hole.
More importantly, it leads to the creation of lots of little holes that are not big enough to
satisfy any requests. This situation is called fragmentation, and is a problem for all memorymanagement strategies, although it is particularly bad for best-fit.
Solution:One way to avoid making little holes is to give the client a bigger block than it asked
for. For example, we might round all requests up to the next larger multiple of 64 bytes. That
doesn't make the fragmentation go away, it just hides it.
Worst Fit
55
First Fit
Another strategy is first fit, which simply scans the free list until a large enough hole is found.
Despite the name, first-fit is generally better than best-fit because it leads to less fragmentation.
Aim
To write a c program to implement memory management techniques (first fit, bestfit and worst
fit).
Algorithm
1.
Enter the five free memories and five processes.
2.
Enter the choice.
3.
In first fit, allocate the process to free memory by sequential order if free memory
size>=process size.
4.
In best fit, allocate the process to free memory, if the memory size is first, equal to free
memory then go for greater memory size.
5.
In worst fit, allocate the process to free memory, if the memory size is greater then go for
equal memory size.
Sample Output:
[cse98@cselinux ~]$ cc fit.c
[cse98@cselinux ~]$ ./a.out
Enter five freememory
25 45 65 20 10
Enter five processes
45 20 95 100 60
1.First fit
2.Best fit
3.Worst fit
Enter your choice:1
First fit
process 20 is allocated for free memory 25
process 45 is allocated for free memory 45
process 60 is allocated for free memory 65
process size 95 is not allocated for any memory
process size 100 is not allocated for any memory[cse98@cselinux ~]$
[cse98@cselinux ~]$ cc fit.c
[cse98@cselinux ~]$ ./a.out
Enter five freememory
25 45 65 20 10
Enter five processes
45 20 95 100 60
56
Result:
Thus the program for memory management technique (first fit, best fit and worst fit) has
been implemented successfully.
57