0% found this document useful (0 votes)
110 views

Os Manual Next Sem

1. The program takes the number of files as input and stores the number of blocks occupied and starting block for each file. 2. It allocates blocks sequentially from the starting block number for each file. 3. The program prints the filename, start block, length and allocated block numbers for each file sequentially.

Uploaded by

swarna_793238588
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

Os Manual Next Sem

1. The program takes the number of files as input and stores the number of blocks occupied and starting block for each file. 2. It allocates blocks sequentially from the starting block number for each file. 3. The program prints the filename, start block, length and allocated block numbers for each file sequentially.

Uploaded by

swarna_793238588
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 64

OS Lab Manual

Objective:

This manual is used to provide an understanding of the design aspects of


operating system.

Recommended Systems/Software Requirements:

 Intel based desktop PC with minimum of 1.66 MHZ or faster processor with at
least 64 MB RAM and 100 MB free disk space

 Turbo C or TC3 complier in Windows XP or Linux Operating System.

Developed By:

V. Swarna,
Assistant ftrofessor,
Department of Information Technology,
Bhoj Reddy Engineering College for
Women

1
List of Programs

S. No Program’s Name Page No

1) Simulate the following CPU Scheduling Algorithms


a) FCFS 1-2
b) SJF 3-4
c) Priority 5-6
d) Round Robin 7-8
2) Simulate all file allocation strategies
a) Sequential 9-10
b) Indexed 11-12
c) Linked 13-14

3) Simulate MVT and MFT 15-18


4) Simulate all File Organization Techniques
a) Single level Directory 19-22
b) Two level directory 23-25
c) Hierarchical directory 26-29

5) Simulate Bankers algorithm for Deadlock Avoidance 30-32


6) Simulate Bankers Algorithm for deadlock Prevention 33-35
7) Simulate all Page Replacement Algorithms
a) FIFO 36-37
b) LRU 38-40
c) LFU 41-46

8) Simulate Paging Technique of Memory Management 47

2
Operating System

3
1) Simulate the following CPU scheduling algorithms
a) FCFS
b) SJF

a) FCFS:
AIM: A program to simulate the FCFS CPU scheduling algorithm

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
`char pn[10][10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,n;
int totwt=0,tottat=0;
clrscr();
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Process Name, Arrival Time & Burst Time:");
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
}
for(i=0;i<n;i++)
{
if(i==0)
{
star[i]=arr[i];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
else
{
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
}
printf("\nPName Arrtime Burtime Start TAT Finish");

1
for(i=0;i<n;i++)
{
printf("\n%s\t%6d\t\t%6d\t%6d\t%6d\t%6d",pn[i],arr[i],bur[i],star[i],tat[i],finish[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("\nAverage Waiting time:%f",(float)totwt/n);
printf("\nAverage Turn Around Time:%f",(float)tottat/n);
getch();
}

OUTPUT:

Input:

Enter the number of processes: 3


Enter the Process Name, Arrival Time & Burst Time: 1 2 3
Enter the Process Name, Arrival Time & Burst Time: 2 5 6
Enter the Process Name, Arrival Time & Burst Time: 3 6 7

Output:

PName Arrtime Burtime Srart TAT Finish


1 2 3 2 3 5
2 5 6 5 6 4
3 6 7 6 7 10

Average Waiting Time: 3.333


Average Turn Around Time: 7.000

2
b) SJF:
AIM: A program to simulate the SJF CPU scheduling algorithm

PROGRAM:

#inclue<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name, arrival time & execution time:");
flushall();
scanf("%s%d%d",pn[i],&at[i],&et[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(et[i]<et[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
st[i]=at[i];
else
st[i]=ft[i-1];
3
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\twaitingtime\ttatime");
for(i=0;i<n;i++) printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t
%5d",pn[i],at[i],et[i],wt[i],ta[i]); printf("\nAverage waiting time is:
%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}

OUTPUT:

Input:

Enter the number of processes: 3


Enter the Process Name, Arrival Time & Burst Time: 1 4 6
Enter the Process Name, Arrival Time & Burst Time: 2 5 15
Enter the Process Name, Arrival Time & Burst Time: 3 6 11

Output:

Pname arrivaltime executiontime waitingtime tatime


1 4 6 0 6
3 6 11 4 15
2 5 15 16 31

Average Waiting Time: 6.6667


Average Turn Around Time: 17.3333

4
2) Simulate the Priority and Round Robin CPU scheduling
algorithms:

AIM: A program to simulate the priority CPU scheduling algorithm

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
clrscr();
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name,arrivaltime,execution time & priority:");
flushall();
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
5
{

6
if(i==0)
{
st[i]=at[i];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0;i<n;i++) printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t
%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]); printf("\nAverage waiting time is:
%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
getch();
}

OUTPUT:
Input:

Enter the number of processes: 3


Enter the Process Name, Arrival Time, execution time & priority: 1 2 3 1
Enter the Process Name, Arrival Time, execution time & priority: 2 4 5 2
Enter the Process Name, Arrival Time, execution time & priority: 3 5 6 3

Output:

Pname arrivaltime executiontime priority waitingtime tatime


1 2 3 1 0 3
2 4 5 2 1 6
3 5 6 3 5 11

Average Waiting Time: 2.0000


Average Turn Around Time: 6.6667

7
Round Robin:
AIM: A program to simulate the Round Robin CPU scheduling algorithm

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int et[30],ts,n,i,x=0,tot=0;
char pn[10][10];
clrscr();
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the time quantum:");
scanf("%d",&ts);
for(i=0;i<n;i++)
{
printf("enter process name & estimated time:");
scanf("%s %d",pn[i],&et[i]);
}
printf("The processes are:");
for(i=0;i<n;i++)
printf("process %d: %s\n",i+1,pn[i]);
for(i=0;i<n;i++)
tot=tot+et[i];
while(x!=tot)
{
for(i=0;i<n;i++)
{
if(et[i]>ts)
{
x=x+ts;
printf("\n %s -> %d",pn[i],ts);

et[i]=et[i]-ts;
}

else
if((et[i]<=ts)&&et[i]!=0)
{
x=x+et[i];
printf("\n %s -> %d",pn[i],et[i]);
et[i]=0;}
}
8
}
printf("\n Total Estimated Time:%d",x);
getch();
}
OUTPUT:

Input:

Enter the no of processes: 2


Enter the time quantum: 3

Enter the process name & estimated time: p1 12


Enter the process name & estimated time: p2 15

Output:
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p2 -> 3

Total Estimated Time: 27

9
3) Simulate file Allocation strategies
a) Sequential b)Indexed c) Linked

AIM: Simulate the sequential file allocation strategies using file allocation methods

PROGRAM:
#include<stdio.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("blocks occupiedare:");
for(i=0;i<n;i++)
{
printf("fileno%d",i+1); for(j=0;j<b[i];j++)
printf("\t%d",c[i][j]);
printf("\n");
}
getch();
}

10
OUTPUT:
Enter no.of files: 2
Enter no. of blocks occupied by file1 4
Enter the starting block of file1 2
Enter no. of blocks occupied by file2 10
Enter the starting block of file2 5
Filename Start block length
1 2 4
2 5 10

11
b) AIM: Simulate the Indexed file allocation strategies using file allocation methods

PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int n,m[20],i,j,ib[20],b[20][20];
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter index block :",i+1);
scanf("%d",&ib[i]);
printf("Enter blocks occupied by file
%d:",i+1); scanf("%d",&m[i]);
printf("enter blocks of file%d:",i+1);
for(j=0;j<m[i];j++)
scanf("%d",&b[i][j]);
} printf("\nFile\t index\tlength\n");
for(i=0;i<n;i++)

printf("%d\t%d\t%d\n",i+1,ib[i],m[i]);
printf("blocks occupiedare:");
for(i=0;i<n;i++)
{ printf("fileno%d",i+1); for(j=0;j<m[i];j+
+)
printf("\t%d--->%d\n",ib[i],b[i][j]);
printf("\n");
}
getch();
}

12
OUTPUT:

Enter no. of files: 2 Enter index


block 3
Enter blocks occupied by file1: 4 enter
blocks of file1:9
4 67
Enter index block 5
Enter blocks occupied by file2:2 enter
blocks of file2: 10 8
File index length 1 3
4
2 5 2
blocksoccupiedare: file1
3--->9
3--->4
3--->6
3--->7
file2
5--->10
5--->8

13
c) AIM: Simulate the Linked file allocation strategies using file allocation methods

PROGRAM:
#include<stdio.h>
#include<conio.h>
struct file
{
char fname[10];
int start,size,block[10];
}f[10];
main()
{
int i,j,n;
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=0;j<f[i].size;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}

14
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
4
3
2
File start size block
venkat 20 6 20--->4--->12--->15--->45--->32--->25
rajesh 12 5 12--->6--->5--->4--->3--->2

15
4) Write a C program to simulate the following file organization
techniques
a) Single level directory b) Two level directory c) Hierarchical

PROGRAM
1.     SINGLE LEVEL DIRECTORY ORGANIZATION

#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir;
void main()
{
int i,ch;
char f[30];
clrscr();
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);
while(1)
{
printf("\n\n 1. Create File\t2. Delete File\t3. Search File \n 4. Display Files\t5. Exit\nEnter
your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter the name of the file -- ");
scanf("%s",dir.fname[dir.fcnt]);
dir.fcnt++;
break;
case 2: printf("\n Enter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
16
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\n Enter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{

printf("File %s is found ", f);


break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\n Directory Empty");
else
{
printf("\n The Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
getch();
}
OUTPUT:

Enter name of directory -- CSE

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 1

Enter the name of the file -- A

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 1

17
Enter the name of the file -- B

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 1

Enter the name of the file -- C

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 4

The Files are -- A B C

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit Enter your choice – 3

Enter the name of the file – ABC

File ABC not found


1. Create File 2. Delete File 3. Search File
4. Display Files 5. Exit Enter your choice – 2

Enter the name of the file – B


File B is deleted

1. Create File 2. Delete File 3. Search File


4. Display Files 5. Exit
Enter your choice – 5

18
b)   TWO LEVEL DIRECTORY ORGANIZATION

PROGRAM:

#include<stdio.h>
struct
{
char dname[10],fname[10][10];
int fcnt;
}dir[10];
void main()
{
int i,ch,dcnt,k;
char f[30], d[30];
clrscr();
dcnt=0;
while(1)
{
printf("\n\n 1. Create Directory\t 2. Create File\t 3. Delete File");
printf("\n 4. Search File \t \t 5. Display \t 6. Exit \t Enter your choice -- ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\n Enter name of directory -- ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt=0;
dcnt++;
printf("Directory created");
break;
case 2: printf("\n Enter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;
19
}
if(i==dcnt)
printf("Directory %s not found",d);
break;
case 3: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{
if(strcmp(d,dir[i].dname)==0)
{
printf("Enter name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is deleted ",f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);
goto jmp;
}
}
printf("Directory %s not found",d);
jmp : break;
case 4: printf("\nEnter name of the directory -- ");
scanf("%s",d);
for(i=0;i<dcnt;i++)
{

if(strcmp(d,dir[i].dname)==0)
{
printf("Enter the name of the file -- ");
scanf("%s",f);
for(k=0;k<dir[i].fcnt;k++)
{
if(strcmp(f, dir[i].fname[k])==0)
{
printf("File %s is found ",f);
goto jmp1;
}
}
printf("File %s not found",f);
goto jmp1;
}
20
}
printf("Directory %s not found",d);
jmp1: break;
case 5: if(dcnt==0)
printf("\nNo Directory's ");
else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;
default:exit(0);
}
}
getch();
}

OUTPUT:
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 1

Enter name of directory -- DIR1


Directory created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 1

Enter name of directory -- DIR2

Directory created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory – DIR1


Enter name of the file -- A1
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

21
Enter name of the directory – DIR1
Enter name of the file -- A2
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 2

Enter name of the directory – DIR2


Enter name of the file -- B1
File created

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 5
Directory Files
DIR1 A1 A2
DIR2 B1

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 4

Enter name of the directory – DIR


Directory not found

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice -- 3
Enter name of the directory – DIR1
Enter name of the file -- A2

File A2 is deleted

1. Create Directory 2. Create File 3. Delete File


4. Search File 5. Display 6. Exit Enter your choice – 6

22
3.     HIERARCHICAL DIRECTORY ORGANIZATION
#include<stdio.h>
#include<graphics.h>
struct tree_element
{
char name[20];
int x,y,ftype,lx,rx,nc,level;
struct tree_element *link[5];
};
typedef struct tree_element
node; void main()
{
int gd=DETECT,gm;
node *root;
root=NULL;
clrscr();
create(&root,0,"root",0,639,320);
clrscr();
initgraph(&gd,&gm,"c:\\tc\\BGI");
display(root);
getch();
closegraph();
}
create(node **root,int lev,char *dname,int lx,int rx,int x)
{
int i,gap;
if(*root==NULL)
{
(*root)=(node *)malloc(sizeof(node));
printf("Enter name of dir/file(under %s) :",dname);
fflush(stdin);
gets((*root)->name);
printf("enter 1 for Dir/2 forfile :");
scanf("%d",&(*root)->ftype);
(*root)->level=lev;
(*root)->y=50+lev*50;
(*root)->x=x;
(*root)->lx=lx;
(*root)->rx=rx;
for(i=0;i<5;i++)
(*root)->link[i]=NULL;
if((*root)->ftype==1)
{
printf("No of sub directories/files(for %s):",(*root)->name); scanf("%d",&(*root)->nc);
if((*root)->nc==0)
gap=rx-lx;
23
else gap=(rx-lx)/(*root)->nc;
for(i=0;i<(*root)->nc;i++)
create(&((*root)->link[i]),lev+1,(*root)->name,lx+gap*i,lx+gap*i+gap,lx+gap*i+gap/2);
}
else (*root)->nc=0;
}
}
display(node *root)
{
int i;
settextstyle(2,0,4);
settextjustify(1,1);
setfillstyle(1,BLUE);
setcolor(14); if(root!=NULL)
{
for(i=0;i<root->nc;i++)
{
line(root->x,root->y,root->link[i]->x,root->link[i]->y);
}
if(root->ftype==1) bar3d(root->x-20,root->y-10,root->x+20,root->y+10,0,0); else
fillellipse(root->x,root->y,20,20);
outtextxy(root->x,root->y,root->name); for(i=0;i<root->nc;i++)
{
display(root->link[i]);
}
}
}

24
OUTPUT:
Enter Name of dir/file (under root): ROOT
Enter 1 for Dir / 2 For File : 1
No of subdirectories / files (for ROOT) :2
Enter Name of dir/file (under ROOT):USER 1
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for USER 1):1
Enter Name of dir/file (under USER 1):SUBDIR
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for SUBDIR):2
Enter Name of dir/file (under USER 1):
JAVA Enter 1 for Dir /2 for file:1
No of subdirectories /files (for JAVA): 0
Enter Name of dir/file (under SUBDIR):VB
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for VB): 0

Enter Name of dir/file (under ROOT):USER2


Enter 1 for Dir /2 for file:1
No of subdirectories /files (for USER2):2
Enter Name of dir/file (under ROOT):A
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under USER2):SUBDIR 2
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for SUBDIR 2):2

Enter Name of dir/file (under SUBDIR2):PPL


Enter 1 for Dir /2 for file:1
No of subdirectories /files (for PPL):2
Enter Name of dir/file (under PPL):B
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under PPL):C
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under SUBDIR):AI
Enter 1 for Dir /2 for file:1
No of subdirectories /files (for AI): 2
Enter Name of dir/file (under AI):D
Enter 1 for Dir /2 for file:2
Enter Name of dir/file (under AI):E
Enter 1 for Dir /2 for file:2

25
26
5) Aim: Write a C program to copy the contents of one file to another using system
calls.

#include <syscall.h>

#include <unistd.h>

#include <sys/types.h>

#include <fcntl.h>

#include <sys/uio.h>

#include <sys/stat.h>

#include <stdio.h>

int main(int argc,char * argv[])

        int fd;

        fd=open(argv[1],O_CREAT | O_RDONLY);

        if(fd==-1)

    {

                printf("error opening the file");

    }

        void *buf = (char*) malloc(120);

        int count=read(fd,buf,120);

        printf("count : %d",count);

        printf("%s",buf);

        close(fd);

        int f1;

        f1=open(argv[2],O_CREAT | O_WRONLY);

        if(f1==-1)

    {

27
                printf("error opening the file");

    }

        int c;

        while(count=read(fd,buf,120)>0)

    {

                c=write(f1,buf,120);

    }

        if(c==-1)

                printf("error writing to the file");

    }

        close(f1);

28
6)Simulate Bankers Algorithm for Deadlock Avoidance.

AIM: A program to simulate the Bankers Algorithm for Deadlock Avoidance.

PROGRAM:
//Bankers algorithm for deadlock avoidance.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,i,j,k,p,u=0,s=0,m;
int block[10],run[10],active[10],newreq[10];
int max[10][10],resalloc[10][10],resreq[10][10];
int totalloc[10],totext[10],simalloc[10];
clrscr();
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the no of resource classes:");
scanf("%d",&r);
printf("Enter the total existed resource in each class:");
for(k=1;k<=r;k++)
scanf("%d",&totext[k]);
printf("Enter the allocated resources:");
for(i=1;i<=n;i++)
for(k=1;k<=r;k++)
scanf("%d",&resalloc);
printf("Enter the process making the new request:");
scanf("%d",&p);
printf("Enter the requested resource:");
for(k=1;k<=r;k++)
scanf("%d",&newreq[k]);
printf("Enter the processes which are n blocked or running:");
for(i=1;i<=n;i++)
{
if(i!=p)
{
printf("process %d:\n",i);
scanf("%d%d",&block[i],&run[i]);
}
}
29
block[p]=0;
run[p]=0;
for(k=1;k<=r;k++)
{

30
j=0;
for(i=1;i<=n;i++)
{
totalloc[k]=j+resalloc[i][k];
j=totalloc[k];
}
}
for(i=1;i<=n;i++)
{
if(block[i]==1||run[i]==1)
active[i]=1;
else
active[i]=0;
}
for(k=1;k<=r;k++)
{
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
}
for(k=1;k<=r;k++)
{
if(totext[k]-totalloc[k]<0)
{
u=1;break;
}
}
if(u==0)
{
for(k=1;k<=r;k++)
simalloc[k]=totalloc[k];
for(s=1;s<=n;s++)
for(i=1;i<=n;i++)
{
if(active[i]==1)
{ j=
0;
for(k=1;k<=r;k++)
{
if((totext[k]-simalloc[k])<(max[i][k]-resalloc[i][k]))
{
j=1;break;
}
}
}
if(j==0)
{
31
active[i]=0;
for(k=1;k<=r;k++)
simalloc[k]=resalloc[i][k];
}
}
m=0;
for(k=1;k<=r;k++) resreq[p]
[k]=newreq[k];
printf("Deadlock willn't occur");
}
else
{
for(k=1;k<=r;k++)
{
resalloc[p][k]=newreq[k];
totalloc[k]=newreq[k];
}
printf("Deadlock will occur");
}
getch();
}

OUTPUT:
Input:

Enter the no of resources: 4


Enter the no of resource classes: 3
Enter the total existed resources in each class: 3 2 2
Enter the allocated resources: 1 0 0 5 1 1 2 1 1 0 0 2
Enter the process making the new request: 2
Enter the requested resource: 1 1 2
Enter the processes which are n blocked or running:
Process 1: 1 2
Process 3: 1 0
Process 4: 1 0

Output:

Deadlock will occur

32
7)Simulate Bankers Algorithm for Deadlock Prevention.

AIM: A program to simulate Bankers Algorithm for Deadlock Prevention.

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int cl[10][10],al[10][10],av[10],i,j,k,m,n,ne[10][10],flag=0;
clrscr();
printf("\nEnter the matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the claim matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&cl[i][j]);
}
}
printf("\nEnter allocated matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&al[i][j]);
}
}
printf("\nThe need matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
ne[i][j]=cl[i][j]-al[i][j];
printf("\t%d",ne[i][j]);
}
printf("\n");
}
printf("\nEnter avaliable matrix");
for(i=0;i<n;i++)
scanf("%d",&av[i]);
printf("Claim matrix:\n");
for(i=0;i<m;i++)
33
{
for(j=0;j<n;j++)
{
printf("\t%d",cl[i][j]);
}
printf("\n");
}
printf("\n Allocated matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",al[i][j]);
}
printf("\n");
}
printf("Available matrix:\n");
for(i=0;i<n;i++)
{
printf("%d\t",av[i]);
}
//for(k=0;k<m;k++)
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(av[j]>=ne[i][j])
flag=1;
else
flag=0;
}
}
if(flag==0)
printf("Unsafe State");
else
printf("Safe State");
getch();
}

34
OUTPUT:
Input:

Enter the claim matrix:3 2 2 6 1 3 3 1 4 4 2 2


Enter allocated matrix:1 0 0 5 1 1 2 1 1 0 0 2
The need matrix:
2 2 2
1 0 2
1 0 3
4 2 0

Enter available matrix1 1 2

Output:

Claim matrix:
3 2 2
6 1 3
3 1 4
4 2 2

Allocated matrix:
1 0 0
5 1 1
2 1 1
0 0 2

Available matrix:
1 12 Safe State

35
8)Simulate all Page Replacement Algorithms
a. FIFO
b. LRU
c. LFU

a) FIFO:
AIM: A program to simulate FIFO Page Replacement Algorithm

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],b[20],n,p=0,q=0,m=0,h,k,i,q1=1;
char f='F';
clrscr();
printf("Enter the Number of Pages:");
scanf("%d",&n);
printf("Enter %d Page Numbers:",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{if(p==0)
{
if(q>=3)
q=0;
a[q]=b[i];
q++;
if(q1<3)
{
q1=q;
}
}
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3))
{
printf("-->%c",f);
m++;
}
p=0;
36
for(k=0;k<q1;k++)

37
{
if(b[i+1]==a[k])
p=1;
}
}
printf("\nNo of faults:%d",m);
getch();
}

OUTPUT:
Input:

Enter the Number of Pages: 12


Enter 12 Page Numbers:
232152453252

Output:

2 2-> F
3 23-> F
2 23
1 231-> F
5 531-> F
2 521-> F
4 524-> F
5 524
3 324-> F
2 324
5 354-> F
2 352-> F

No of faults: 9

38
b) LRU:
AIM: A program to simulate LRU Page Replacement Algorithm

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int g=0,a[5],b[20],p=0,q=0,m=0,h,k,i,q1=1,j,u,n;
char f='F';
clrscr();
printf("Enter the number of pages:");
scanf("%d",&n);
printf("Enter %d Page Numbers:",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{if(p==0)
{
if(q>=3)
q=0;
a[q]=b[i];
q++;
if(q1<3)
{
q1=q;
//g=1;
}
}
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3))
{
printf("-->%c",f);
m++;
}
p=0;
g=0;
if(q1==3)
{
for(k=0;k<q1;k++)
{

39
if(b[i+1]==a[k])

40
p=1;
}
for(j=0;j<q1;j++)
{ u=
0;
k=i;
while(k>=(i-1)&&(k>=0))
{
if(b[k]==a[j])
u++;
k--;
}
if(u==0)
q=j;
}
}
else
{
for(k=0;k<q;k++)
{
if(b[i+1]==a[k])
p=1;
}
}
}
printf("\nNo of faults:%d",m);
getch();
}

41
OUTPUT:
Input:

Enter the Number of Pages: 12


Enter 12 Page Numbers:
232152453252

Output:

2 2-> F
3 23-> F
2 23
1 231-> F
5 251-> F
2 251
4 254-> F
5 254
3 354-> F
2 352-> F
5 352
2 352

No of faults: 7

42
c) LFU Page Replacement algorithm

AIM: A program to simulate LFU Page Replacement Algorithm

Program:

#include< stdio.h >

#include< conio.h >

struct frame

int val;

int freq;

int pos;

}f[30];

int no_frame,no_page,page_seq[100],page_fault=0;

void input()

int i;

printf(“\n\n least freqently used\n”);

printf(“\n enter no_of frame”);

scanf(“%d”,&no_frame)

printf(“\n enter no_of pages”);

scanf(“%d”,&no_page);
 
printf(“enter page sequence”);
43
for(i=0;i

scanf(“%d”,&page_seq[i]);

printf(“\n”);

for(i=0;i

f[i].pos=-1;

f[i].val=-1;

f[i].freq=0;

void display()

static int preev_page_fault=0;

int i;

for(i=0;i

if(f[i].val==-1)

printf(“0”);

else

printf(“%d”,f[i].val);

44
}

if(preev_page_fault!=page_fault)

printf(“F”);

preev_page_fault=page_fault;

printf(“\n”);

int search(int i)

int j;

for(j=0;j

if(f[j].val==page_seq[i])

return j;

return -1;

int position()

int i,j=0,k;

for(i=0;i

if(f[i].pos==-1)

45
return i;

for(i=0;i

if(f[j].freq>f[i].freq)

j=i;

k=j;

for(i=0;i

if(f[j].freq==f[i].freq&&j!=i)

if(f[j].pos>f[i].pos)

j=i;

k=j;

return k;

return 0;

void LFU()

46
int i,k;

input();

for(i=0;i

k=search(i);

if(k!=-1)

f[k].freq++;

f[k].pos=i;

if(k==-1)

k=position();

f[k].pos=i;

f[k].val=page_seq[i];

f[k].freq=1;

page_fault++;

display();

void main()

clrscr();

47
LFU();

printf(“\n no.of page faults%d”,page_fault);

getch();

 OUT PUT:

Least frequently used

Enter no of frame 3

Enter no of pages  4

Enter  page sequence 4

400F

450F

458F

No of page faults  3

48
9)Simulate Paging technique of Memory Management.

AIM: A program to simulate Paging technique of memory management.

PROGRAM:

#include<stdio.h>
#include<conio.h>
main()
{
int np,ps,i;
int *sa;
clrscr();
printf("Enter how many pages\n");
scanf("%d",&np);
printf("Enter the page size \n");
scanf("%d",&ps); for(i=0;i<np;i+
+)
{
sa[i]=(int)malloc(ps);
printf("Page%d\t Address %u\n",i+1,sa[i]);
}
getch();
}

OUTPUT:

Input:

Enter how many pages: 5


Enter the page size: 4

Output:

Page1 Address: 1894


Page2 Address: 1902
Page3 Address: 1910
Page4 Address: 1918
Page5 Address: 1926

49
b) Simulate Segmentation technique of memory
management.
Aim: To write a LINUX/UNIX C Program for the Implementation of Segmentation.

SOURCE CODE:
#include<stdio.h>
#include<conio.h>
struct list
{
int seg;
int base;
int limit;
struct list *next;
} *p;
void insert(struct list *q,int base,int limit,int seg)
{
if(p==NULL)
{
p=malloc(sizeof(Struct list));
p->limit=limit;
p->base=base;
p->seg=seg;
p->next=NULL;
}
else
{
while(q->next!=NULL)
{
Q=q->next;
Printf(“yes”)
}
q->next=malloc(sizeof(Struct list));
q->next ->limit=limit;
q->next ->base=base;
q->next ->seg=seg;
q->next ->next=NULL;
}
}
int find(struct list *q,int seg)
{
while(q->seg!=seg)
{
q=q->next;
}
return q->limit;
}
int search(struct list *q,int seg)
{
while(q->seg!=seg)
{
q=q->next;
}

50
return q->base;
}
main()
{
p=NULL;
int seg,offset,limit,base,c,s,physical;
printf(“Enter segment table/n”);
printf(“Enter -1 as segment value for termination\n”);
do
{
printf(“Enter segment number”);
scanf(“%d”,&seg);
if(seg!=-1)
{
printf(“Enter base value:”);
scanf(“%d”,&base);
printf(“Enter value for limit:”);
scanf(“%d”,&limit);
insert(p,base,lmit,seg);
}
}
while(seg!=-1)
printf(“Enter offset:”);
scanf(“%d”,&offset);
printf(“Enter bsegmentation number:”);
scanf(“%d”,&seg);
c=find(p,seg);
s=search(p,seg);
if(offset<c)
{
physical=s+offset;
printf(“Address in physical memory %d\n”,physical);
}
else
{
printf(“error”);
}
}

OUTPUT:
[examuser56@localhost ~]$ cc seg.c
[examuser56@localhost ~]$ ./a.out

Enter segment table


Enter -1 as segmentation value for termination
Enter segment number:1
Enter base value:2000
Enter value for limit:100
Enter segment number:2
Enter base value:2500
Enter value for limit:100
Enter segmentation number:-1
Enter offset:90

51
Enter segment number:2
Address in physical memory 2590

[examuser56@localhost ~]$ ./a.out

Enter segment table


Enter -1 as segmentation value for termination
Enter segment number:1
Enter base value:2000
Enter value for limit:100
Enter segment number:2
Enter base value:2500
Enter value for limit:100
Enter segmentation number:-1
Enter offset:90
Enter segment number:1
Address in physical memory 2090

52
10) Pipe Example (Unnamed Pipe)
Aim: Write a program for pipes. Use unnamed pipe

Program

#include <stdio.h>

/* The index of the "read" end of the pipe */


#define READ 0

/* The index of the "write" end of the pipe */


#define WRITE 1

char *phrase = "Stuff this in your pipe and smoke it";

main () {
int fd[2], bytesRead;

char message [100]; /* Parent process message buffer */

pipe ( fd ); /*Create an unnamed pipe*/

if ( fork ( ) == 0 ) {
/* Child Writer */
close (fd[READ]); /* Close unused end*/
write (fd[WRITE], phrase, strlen ( phrase) +1); /* include NULL*/
close (fd[WRITE]); /* Close used end*/
printf("Child: Wrote '%s' to pipe!\n", phrase);

} else {

/* Parent Reader */
close (fd[WRITE]); /* Close unused end*/
bytesRead = read ( fd[READ], message, 100);
printf ( "Parent: Read %d bytes from pipe: %s\n", bytesRead, message);
close ( fd[READ]); /* Close used end */
}
}

53
11) Simulate Dining Philosopher’s problem

Aim: Write a C program to solve the Dining- Philosopher problem using semaphores.

PROGRAM

# include<stdio.h>

# include<pthread.h>

# include<stdlib.h>

# include<unistd.h>

# include<ctype.h>

# include<sys/types.h>

# include<sys/wait.h>

# include<semaphore.h>

# include<sys/sem.h>

sem_t chopstick[100];

int n;

void *thread_func(int no)

int i,iteration=5;

for(i=0;i<iteration;++i)

sem_wait(&chopstick[no]);

sem_wait(&chopstick[(no+1)%n]);

printf(“\nPhilosopher %d –> Begin eating”,no);

54
sleep(1);

printf(“\nPhilosopher %d –> Finish eating\n”,no);

sem_post(&chopstick[no]);

sem_post(&chopstick[(no+1)%n]);

pthread_exit(NULL);

int main()

int i,res;

pthread_t a_thread[100];

printf(“\nEnter the number of philosopher :”);

scanf(“%d”,&n);

for(i=0;i<n;++i)

res=sem_init(&chopstick[i],0,0);

if(res==-1)

perror(“semaphore initialization failed”);

exit(1);

for(i=0;i<n;++i)

res=pthread_create(&a_thread[i],NULL,thread_func,(int*) i);

55
if(res!=0)

perror(“semaphore creation failed”);

exit(1);

sem_post(&chopstick[i]);

for(i=0;i<n;++i)

res=pthread_join(a_thread[i],NULL);

if(res!=0)

perror(“semaphore join failed”);

exit(1);

printf(“\n \n thread join succesfull\n”);

for(i=0;i<n;++i)

res=sem_destroy(&chopstick[i]);

if(res==-1)

perror(“semaphore destruction failed”);

56
exit(1);

exit(0);

OUTPUT

$ gcc dining_sem.c -o dining_sem_op -lpthread

$ ./dining_sem_op

Begin eating :0

Begin eating :2

Finish eating:0

Finish eating:2

Begin eating :4

Begin eating :1

Finish eating:4

Finish eating:1

Begin eating :3

Begin eating :0

Finish eating:3

Finish eating:0

Begin eating :2

Finish eating:3

Begin eating :4

Finish eating:2

57
Finish eating:4

Begin eating :1

Begin eating :3

Finish eating:1

Finish eating:3

Begin eating :0

Begin eating :2

Finish eating:0

Finish eating:2

Begin eating :2

Begin eating :4

Finish eating:4

Finish eating:1

Begin eating :3

Begin eating :0

Finish eating:3

Finish eating:0

Begin eating :2

Begin eating :4

Finish eating:2

Finish eating:4

Begin eating :1

Begin eating :3

Finish eating:1

Finish eating:3

58
Begin eating :0

Begin eating :2

Finish eating:0

Finish eating:2

Begin eating :4

Begin eating :1

Finish eating:4

Finish eating:1

Begin eating :3

Finish eating:3

Thread join successful

59
12) Simulate IPC
Aim: Write a C programs that illustrate communication between two unrelated
processes using named pipe.

Program:
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<unistd.h>
int main()
{
int pfds[2];
char buf[30];
if(pipe(pfds)==-1)
{
perror("pipe");
exit(1);
}
printf("writing to file descriptor #%d\n", pfds[1]);
write(pfds[1],"test",5);
printf("reading from file descriptor #%d\n ", pfds[0]);
read(pfds[0],buf,5);
printf("read\"%s\"\n" ,buf);
}
Output:
[student@gcet ~]$ vi pipes1.c
[student@gcet ~]$ cc pipes1.c
[student@gcet ~]$ ./a.out
writing to file descriptor #4
reading from file descriptor #3
read"test"

60
61

You might also like