Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views48 pages

OS Lab Manual

Bhoj Reddy Engineering College for Women offers an Operating Systems Lab course aimed at introducing students to key operating system concepts and practical applications using Unix. The course objectives include simulating CPU scheduling algorithms and implementing memory management techniques, with specific outcomes tied to engineering knowledge and problem-solving skills. The document outlines the vision and mission of the institution and department, along with program educational objectives and outcomes for students in the Information Technology department.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views48 pages

OS Lab Manual

Bhoj Reddy Engineering College for Women offers an Operating Systems Lab course aimed at introducing students to key operating system concepts and practical applications using Unix. The course objectives include simulating CPU scheduling algorithms and implementing memory management techniques, with specific outcomes tied to engineering knowledge and problem-solving skills. The document outlines the vision and mission of the institution and department, along with program educational objectives and outcomes for students in the Information Technology department.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Bhoj Reddy Engineering College for

Women
(Approved by AICTE and Affiliated to JNTUH, Telangana)
Vinaynagar, IS Sadan Crossroads, Saidabad, Hyderabad – 500 059,
Telangana.

Department of
Information Technology

OPERATING SYSTEMS LAB

R22 Regulation
II B Tech IT II Semester
Vision of Institution - BRECW
BRECW develops confident and articulative young women into dynamic Engineers equipped
with skills, knowledge, values and an attitude to contribute to the society.

Mission of Institution- BRECW

 BRECW is committed to providing a challenging, enriching, safe and supportive


technical learning environment through its core values of responsibility, respect, and
compassion.

 Fosters intellectual, spiritual, and personal development of young women so that they
develop the tools necessary to lead meaningful lives.
 Offers academic curriculum along with an extensive co-curricular program with the
support of dedicated staff who ensure that students identify their strengths and develop
their skills such as teamwork, leadership, creativity, and entrepreneurship.

 Develops independent, adaptable thinkers with a passion for learning, courage to take
risks and initiative to apply what is learned.

Department of Information Technology

Vision

 To emerge as a department of excellence in using cutting–edge technology by empowering women with


sound technical knowledge to meet the future needs and challenges of the society.
Mission
 Department is committed to provide an enriching & conducive environment to build tools through the
values of compassion and respect.
 To provide a holistic learning environment which will help students improve their personal and
professional skills.
 To create an ambience that will contribute in creating self-reliant, innovative and entrepreneurial
professionals, who shall contribute to the growth of technology.
 To develop a passion for learning through practical approach.

Programme Educational Objectives of IT Department

At the end of the program, the women engineers will be able to:

PEO1: Use strong fundamental concepts, analytical ability and programming for problem solving skills for
societal benefits.
PEO2: Develop IT tools exhibiting effective communication and high-quality professional and computer ethics.
PEO3: Engage in lifelong learning and develop the apt tools required for the need of the hour.

Department of Information Technology

Programme Outcomes (POs)

1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals,


and an engineering specialization to the solution of complex engineering problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences, and
engineering sciences.
3. Design-development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research methods
including design of experiments, analysis and interpretation of data, and synthesis of the information to
provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with an
understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal,
health, safety, legal and cultural issues and the consequent responsibilities relevant to the professional
engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering solutions in
societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of the
engineering practice.
9. Individual and teamwork: Function effectively as an individual, and as a member or leader in diverse
teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports and
design documentation, make effective presentations, and give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the engineering and
management principles and apply these to one’s own work, as a member and leader in a team, to
manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.

Programme Specific Outcomes (PSOs)

PSO1: Analysis, design and develop solution in the area of Design and Analysis of Algorithms, Compiler
Constructions, Software Engineering, Operating Systems and Computer Networks for research and development
to solve problems of the society.
PSO2: Able to think logically and apply standard practices and strategies to develop innovative projects for the
current industry needs.
Course Name: Operating Systems Lab

Course Objectives:

● Introduce operating system concepts (i.e., processes, threads, scheduling, synchronization, deadlocks,
memory management, file and I/O subsystems and protection)
● Introduce the issues to be considered in the design and development of operating system
● Introduce basic Unix commands, system call interface for process management, interprocess
communication and I/O in Unix Effective use of Business Intelligence (BI) technology (Tableau) to apply
data visualization

Course Outcomes: At the end of the course a student should be able to

●Simulate and implement operating system concepts such as scheduling, deadlock management, file
management and memory management.
●Able to implement C programs using Unix system calls Will be able to control access to a computer and
the files that may be shared
● Implement various CPU scheduling Algorithms
●Understand and Implement Bankers Algorithm
●Implement IPC mechanism and Memory Management techniques

COs PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2

CO1 2 2 1 2 1 - - - 2 3 2 3 3 1

CO2 2 2 1 2 1 - - - 2 3 2 3 3 1

CO3 2 2 1 2 1 - - - 2 3 2 3 3 0
CO4 2 1 1 2 1 - - - 2 3 2 3 3 0

CO5 1 1 1 2 1 - - - 2 3 2 3 3 0
List of Experiments/Demonstrations:

1. Write C programs to simulate the following CPU Scheduling algorithms a) FCFS b) SJF c) Round
Robin d) priority
2. Write programs using the I/O system calls of UNIX/LINUX operating system (open, read, write,
close, fcntl, seek, stat, opendir, readdir)
3. Write a C program to simulate Bankers Algorithm for Deadlock Avoidance and Prevention.
4. Write a C program to implement the Producer – Consumer problem using semaphores using
UNIX/LINUX system calls.
5. Write C programs to illustrate the following IPC mechanisms a) Pipes b) FIFOs c) Message Queues d)
Shared Memory
6. Write C programs to simulate the following memory management techniques a) Paging b)
Segmentation
7. Write C programs to simulate Page replacement policies a) FCFS b) LRU c) Optimal
OPERATING SYSTEMS LAB DEPT. OF IT

Program 1
Write C programs to simulate the following CPU Scheduling algorithms a) FCFS b) SJF c) Round Robin d)
priority

a) FCFS

#include<stdio.h>
void main()
{
int wt[20]={0}, bt[20]={0}, tat[20]={0}, n, i, swt=0, stat=0;
float awt,atat;
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the burst times: ");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
}
for(i=0;i<n;i++)
{
tat[i]=wt[i]+bt[i];
}
for(i=0;i<n;i++)
{
swt=swt+wt[i];
}
awt=swt/n;
for(i=0;i<n;i++)
{
stat=stat+tat[i];
}
atat=stat/n;
printf("Process\tBT\tTAT\tWT");
printf("\n-----------------------------------");
for(i=0;i<n;i++)
{
printf("\n P%d \t%d\t%d \t%d",i+1,bt[i],tat[i],wt[i]);
}
printf("\n-----------------------------------");
printf("\n \t(ATAT)%f %f(AWT)\n",atat,awt);
printf("-----------------------------------");
}
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

INPUT:

Enter the no of processes:4


Enter the burst times: 24
3
4
12
OUTPUT:

Process BT TAT WT
--------------------------------------------------
P1 24 24 0
P2 3 27 24
P3 4 31 27
P4 12 43 31
----------------------------------------------------
(ATAT)31.000000 20.000000(AWT)

b) SJF

#include<stdio.h>
void main()
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

{
Int wt[20] ={0}, bt[20]={0}, tat[20]={0}, i, swt=0, stat=0, max, j, n, p[20],m;
float awt,atat;
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the burst times");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
printf("Enter the processes");
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[i]>bt[j])
{
max=bt[i];
m=p[i];
bt[i]=bt[j];
p[i]=p[j];
bt[j]=max;
p[j]=m;
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
}
for(i=0;i<n;i++)
{
tat[i]=wt[i]+bt[i];
}
for(i=0;i<n;i++)
{
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=swt/n;
atat=stat/n;
printf("Process\tBT\tTAT\tWT");
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

printf("\n-----------------------------------");
for(i=0;i<n;i++)
{
printf("\n %d \t%d\t%d \t%d",p[i],bt[i],tat[i],wt[i]);
}
printf("\n-----------------------------------");
printf("\n \t(ATAT)%f %f(AWT)\n",atat,awt);
printf("-----------------------------------");
}

INPUT:
Enter the no of processes:4
Enter the burst times6
7
8
3
Enter the processes1
2
3
4
OUTPUT:
Process BT TAT WT
-----------------------------------
4 3 3 0
1 6 9 3
2 7 16 9
3 8 24 16
-----------------------------------
(ATAT)13.000000 7.000000(AWT)
-----------------------------------

c) Round Robin

#include<stdio.h>
void main()
{
int p[20],bt[20]={0},tat[20]={0},wt[20]={0},i,temp=0,ct[20]={0},max,stat=0,swt=0,n,
tmslc,j;
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

float awt,atat;
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the processes:");
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
printf("Enter the burst times:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
ct[i]=bt[i];
}
printf("Enter the tmslc:");
scanf("%d",&tmslc);
max=bt[0];
for(i=1;i<n;i++)
{
if(max<bt[i])
max=bt[i];
}
for(j=0;j<(max/tmslc)+1;j++)
{
for(i=0;i<n;i++)
{
if(bt[i]!=0)
{
if(bt[i]<=tmslc)
{
tat[i]=temp+bt[i];
temp=temp+bt[i];
bt[i]=0;
}
else
{
bt[i]=bt[i]-tmslc;
temp=temp+tmslc;
}
}
}
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-ct[i];
stat+=tat[i];
swt+=wt[i];
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

}
atat=(float)stat/n;
awt=(float)swt/n;
printf("Process\tBT\tTAT\tWT");
printf("\n---------------------------------------");
for(i=0;i<n;i++)
{
printf("\n P%d \t%d\t%d \t%d",p[i],ct[i],tat[i],wt[i]);
}
printf("\n---------------------------------------");
printf("\n \t(ATAT)%f %f(AWT)\n",atat,awt);
printf("\n---------------------------------------");
}

INPUT:

Enter the no of processes:5


Enter the processes:1
2
3
4
5
Enter the burst times:10
1
2
1
5
Enter the tmslc:4

OUTPUT:

Process BT TAT WT
---------------------------------------
P1 10 19 9
P2 1 5 4
P3 2 7 5
P4 1 8 7
P5 5 17 12
---------------------------------------
(ATAT)11.200000 7.400000(AWT)

---------------------------------------

BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN


OPERATING SYSTEMS LAB DEPT. OF IT

d) Priority

#include<stdio.h>
void main()
{
int wt[20]={0},bt[20]={0},tat[20]={0},prty[20]={0},i,swt=0,stat=0,max,j,n,m,p[20],x;
float awt,atat;
printf("Enter the no of processes:");
scanf("%d",&n);
printf("Enter the burst times:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
printf("\nEnter the priorities:");
for(i=0;i<n;i++)
{
scanf("%d",&prty[i]);
}
printf("Enter the processes:");
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{

if(prty[i]>prty[j])
{
max=prty[i];
prty[i]=prty[j];
prty[j]=max;
m=p[i];
p[i]=p[j];
p[j]=m;
x=bt[i];
bt[i]=bt[j];
bt[j]=x;

}
}
}
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
}
for(i=0;i<n;i++)
{
tat[i]=wt[i]+bt[i];
}
for(i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
tat[i]=wt[i]+bt[i];
}
for(i=0;i<n;i++)
{
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=swt/n;
atat=stat/n;
printf("Process\tBT\tTAT\tWT");
printf("\n---------------------------------------");
for(i=0;i<n;i++)
{
printf("\n P%d \t%d\t%d \t%d",p[i],bt[i],tat[i],wt[i]);
}
printf("\n---------------------------------------");
printf("\n \t(ATAT)%f %f(AWT)\n",atat,awt);
printf("\n---------------------------------------");
}

INPUT:
Enter the no of processes:5
Enter the burst times:10
1
2
1
5

Enter the priorities:3


1
4
5
2
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

Enter the processes:1


2
3
4
5

OUTPUT:

Process BT TAT WT
---------------------------------------
P2 1 1 0
P5 5 6 1
P1 10 16 6
P3 2 18 16
P4 1 19 18
---------------------------------------
(ATAT)12.000000 8.000000(AWT)

---------------------------------------
Program 2
Write programs using the I/O system calls of UNIX/LINUX operating system (open, read, write, close, fcntl,
seek, stat, opendir, readdir)

a) Open and write

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
void main()
{
int fd;
fd=creat("n1",0666);
if(fd!=-1)
{
fd=open("n1",O_WRONLY);
if(fd==-1)
{
printf("Cannot open file");
}
write(fd,"This is OS Program",20);
close(fd);
}
}

BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN


OPERATING SYSTEMS LAB DEPT. OF IT

OUTPUT:
Open file n1:
This is OS Program
b) Open and read

include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
void main()
{
int fd,n=1;
char buf;
fd=open("opening.c",O_RDONLY);
if(fd==-1)
{
printf("File doesnot exist");
}
while(n>0)
{
n=read(fd,&buf,1);
printf("%c",buf);
}
}

Opening.c
I like programming.

OUTPUT:
I like programming.
c) Opendir and readdir

#include<stdio.h>
#include<dirent.h>
void main(int argc,char **argv)
{
DIR *dp;
struct dirent *link;
dp=opendir(argv[1]);
printf("Contents of the directory %s are \n",argv[1]);
while((link=readdir(dp))!=0)
{
printf("%s\n",link->d_name);
}
closedir(dp);
}
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

OUTPUT:

Contents of the directory new are


f1
..
f3
.
f2

d) stat

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
void main()
{
struct stat fs;
if(stat("p2.c",&fs)==-1)
{
printf("Error in stat system call");
}
printf("No of links: %ld\n",fs.st_nlink);
printf("Filesize: %ld bytes\n",fs.st_size);
printf("File in ode: %ld\n",fs.st_ino);
}

OUTPUT:

No of links: 1
Filesize: 279 bytes
File in ode: 8128784

e)fcntl

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
void main(int argc,char *argv[])
{
struct flock f1={F_UNLCK,SEEK_SET,0,0,0};
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

int fd;
char buf;
if((fd=open(argv[1],O_RDWR))==-1)
{
printf("Error in opening\n");
}
printf("Press<RETURN> to try to get lock\n");
getchar();
printf("Trying to get lock\n");
f1.l_type=F_WRLCK;
f1.l_pid=getpid();
if(fcntl(fd,F_SETLK,&f1)==-1)
{
printf("fcnl error\n");
}
else if(f1.l_type!=F_UNLCK)
{
printf("File has been exclusively locked by %d\n",f1.l_pid);
}
else
{
printf("File is not locked\n");
}
printf("Press enter to return lock\n");
getchar();
f1.l_type=F_UNLCK;
printf("File has been unlocked\n");
close(fd);
}

OUTPUT
In Terminal 1

Press<RETURN> to try to get lock

Trying to get lock


File has been exclusively locked by 4426
Press enter to return lock

In Terminal 2

Press<RETURN> to try to get lock

Trying to get lock


fcnl error
Press enter to return lock

BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN


OPERATING SYSTEMS LAB DEPT. OF IT

f) i) SEEK_SET

#includesys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<stdio.h>
void main()
{
char buf;
int fd=open("my.c",O_RDONLY);
lseek(fd,5,SEEK_SET);
read(fd,&buf,1);
printf("The char at offset 5=%c\n",buf);
close(fd);
}

My.c:
I like OS.This is my OS program.
OUTPUT:

The char at offset 5=e

ii) SEEK_CUR

#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<stdio.h>
void main()
{
char buf;
int fd=open("my.c",O_RDONLY);
lseek(fd,5,SEEK_SET);
read(fd,&buf,1);
printf("The char at offset 5=%c\n",buf);
close(fd);
}

My.c:
I like OS.This is my OS program.

OUTPUT:

The char at offset 5=e


BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

The char at current offset=h

ii) SEEK_END

#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
void main()
{
int fd;
fd=open("my.c",O_WRONLY);
lseek(fd,10,SEEK_END);
write(fd,"I am in BRECW",13);
close(fd);
}

My.c:
I like OS.This is my OS program.

OUTPUT:

I like OS.This is my OS program.


I am in BRECW

Program 3

Write a C program to simulate Bankers Algorithm for Deadlock Avoidance and Prevention

#include<stdio.h>
struct file
{
int all[10];
int max[10];
int need[10];
int flag;
};
void main()
{
struct file f[10];
int fl;
int i,j,k,p,b,n,r,g,cnt=0,id,newr;
int avail[10],seq[10];
printf("Enter the no of processes--");
scanf("%d",&n);
printf("Enter the no of resources");
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

scanf("%d",&r);
for(i=0;i<n;i++)
{
printf("Enter the details for P%d\n",i);
printf("Enter the allocation : ");
for(j=0;j<r;j++)
scanf("%d",&f[i].all[j]);
printf("Enter the max: ");
for(j=0;j<r;j++)
scanf("%d",&f[i].max[j]);
f[i].flag=0;
}
printf("\nEnter Available resources--\t");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
printf("\nEnter new Request details--");
printf("\nEnter the pid\t--");
scanf("%d",&id);
printf("\nEnter the request for resources--\t");
for(i=0;i<r;i++)
{
scanf("%d",&newr);
f[id].all[i]+=newr;
avail[i]=avail[i]-newr;
}
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
f[i].need[j]=f[i].max[j]-f[i].all[j];
if(f[i].need[j]<0)
f[i].need[j]=0;
}
}
cnt=0;
fl=0;
while(cnt!=n)
{
g=0;
for(j=0;j<n;j++)
{
if(f[j].flag==0)
{
b=0;
for(p=0;p<r;p++)
{
if(avail[p]>=f[j].need[p])
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

b=b+1;
else
b=b-1;
}
if(b==r)
{
printf("\nP%d is visited",j);
seq[fl++]=j;
f[j].flag=1;
for(k=0;k<r;k++)
avail[k]=avail[k]+f[j].all[k];
cnt=cnt+1;
printf("(");
for(k=0;k<r;k++)
printf("%3d",avail[k]);
printf(")");
g=1;
}
}
}

if(g==0)
{
printf("\nREQUEST NOT GRANTED__DEADLOCK OCCURRED");
printf("\nSYTEM IS IN UNSAFE STATE");
goto y;
}
}
printf("\nSYSTEM IS IN SAFE STATE");
printf("\nThe safe sequence is __(");
for(i=0;i<fl;i++)
printf("P%d\t",seq[i]);
printf(")");
y:printf("\nProcess\t\tAllocation\t Max\t\t Need\n");
for(i=0;i<n;i++)
{
printf("P%d\t",i);
for(j=0;j<r;j++)
printf("%6d",f[i].all[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].max[j]);
for(j=0;j<r;j++)
printf("%6d",f[i].need[j]);
printf("\n");
}
}

BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN


OPERATING SYSTEMS LAB DEPT. OF IT

INPUT:
Enter the no of processes--5
Enter the no of resources3
Enter the details for P0
Enter the allocation : 0 1 0
Enter the max: 7 5 3
Enter the details for P1
Enter the allocation : 2 0 0
Enter the max: 3 2 2
Enter the details for P2
Enter the allocation : 3 0 2
Enter the max: 9 0 2
Enter the details for P3
Enter the allocation : 2 1 1
Enter the max: 2 2 2
Enter the details for P4
Enter the allocation : 0 0 2
Enter the max: 4 3 3

Enter Available resources-- 3 3 2

Enter new Request details--


Enter the pid --1

Enter the request for resources-- 1 0 2

OUTPUT:
P1 is visited( 5 3 2)
P3 is visited( 7 4 3)
P4 is visited( 7 4 5)
P0 is visited( 7 5 5)
P2 is visited( 10 5 7)
SYSTEM IS IN SAFE STATE
Process Allocation Max Need
P0 0 1 0 7 5 3 7 4 3
P1 3 0 2 3 2 2 0 2 0
P2 3 0 2 9 0 2 6 0 0
P3 2 1 1 2 2 2 0 1 1
P4 0 0 2 4 3 3 4 3 1

Program 4

Write a C program to implement the Producer – Consumer problem using semaphores using UNIX/LINUX
system calls.

#include<stdio.h>
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

void main()
{
int buffer[10],bufsize,in,out,produce,consume,choice;
in=0;
out=0;
bufsize=10;
while(choice!=3)
{
printf("\n 1.produce \t 2. consumer \t 3.Exit");
printf("\n Enter your choice:");
scanf("%d", &choice);
switch(choice)
{
case 1: if((in+1)%bufsize==out)
printf("\n buffer is full");
else
{
printf("\n enter the value:");
scanf("%d", &produce);
buffer[in]=produce;
in=(in+1)%bufsize;
}
break;
case 2: if(in==out)
printf("\n buffer is empty");
else
{
consume=buffer[out];
printf("\n the consumered value is %d", consume);
out=(out+1)%bufsize;
}
break;
}
}
}

OUTPUT:

1.produce 2. consumer 3.Exit


Enter your choice:2

buffer is empty
1.produce 2. consumer 3.Exit
Enter your choice:1

enter the value:100

BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN


OPERATING SYSTEMS LAB DEPT. OF IT

1.produce 2. consumer 3.Exit


Enter your choice:2

the consumered value is 100


1.produce 2. consumer 3.Exit
Enter your choice:3

Program 5
Write C programs to illustrate the following IPC mechanisms
a) Pipes b) FIFOs c) Message Queues d) Shared Memory

a) pipes

#include<stdio.h>
#include<unistd.h>
int main()
{
int pipefds[2];
int returnstatus;
int pid;
char writemessages[2][20]={"Hi", "Hello"};
char readmessage[20];
returnstatus = pipe(pipefds);
if (returnstatus == -1)
{
printf("Unable to create pipe\n");
return 1;
}
pid = fork();

// Child process
if (pid == 0)
{
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Child Process - Reading from pipe – Message 1 is %s\n", readmessage);
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Child Process - Reading from pipe – Message 2 is %s\n", readmessage);
}
else
{
//Parent process
printf("Parent Process - Writing to pipe - Message 1 is %s\n", writemessages[0]);
write(pipefds[1], writemessages[0], sizeof(writemessages[0]));
printf("Parent Process - Writing to pipe - Message 2 is %s\n", writemessages[1]);
write(pipefds[1], writemessages[1], sizeof(writemessages[1]));

BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN


OPERATING SYSTEMS LAB DEPT. OF IT

}
return 0;
}

OUTPUT:
Parent Process - Writing to pipe - Message 1 is Hi
Parent Process - Writing to pipe - Message 2 is Hello
Child Process - Reading from pipe – Message 1 is Hi
Child Process - Reading from pipe – Message 2 is Hello

b) FIFO

fifoheader.h
#include<stdio.h>
#define BUFFMAX 80
#define PERMS 0666
#include<sys/stat.h>
#include<string.h>
#define F1 "F1"
#define F2 "F2"
client(int readfd, int writefd)
{
int n,n1;
char buff[100];
if((n=read(0,buff,80))<0)
printf("error in reading from keyboard");
if((n1=write(writefd,buff,n))==n)
{
n=read(readfd,buff,100);
write(1,buff,n);

}
else
printf("error in writing into pipe");
}

server(int readfd, int writefd)


{
int n;
char buff[100];
n=read(readfd,buff,80);
strcat(buff,"this is the message from server");
write(writefd,buff, strlen(buff));
}

ser1.c
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

#include"fifoheader.h"
main()
{
int readfd,writefd;
if(mknod(FIFO1,PERMS|S_IFIFO,0)<0)
printf("error in creating FIFO1");
if(mknod(FIFO2,PERMS|S_IFIFO,0)<0);
printf("error in creating FIFO2");
readfd=open(FIFO1,0);
writefd=open(FIFO2,1);
server(readfd,writefd);
close(FIFO1);
close(FIFO2);
}

cli1.c
#include"fifoheader.h"
main()
{
int readfd,writefd;
writefd=open(FIFO1,1);
readfd=open(FIFO2,0);
client(readfd,writefd);
}

OUTPUT:

$cc ser1.c -o ser1


$cc cli1.c -o cli1
$./ser1 &
[3] 3260

$./cli1
hi
hi
this is the message from server[3]-exit 255 ./ser1

c) Message Passing(server)

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// structure for message queue
struct msg_buffer {
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

long msg_type;
char msg[100];
} message;
void main() {
key_t my_key;
int msg_id;
my_key = ftok("progfile", 65); //create unique key
msg_id = msgget(my_key, 0666 | IPC_CREAT); //create message queue and return id
message.msg_type = 1;
printf("Write Message : ");
fgets(message.msg, 100, stdin);
msgsnd(msg_id, &message, sizeof(message), 0); //send message
printf("Sent message is : %s \n", message.msg);
}
OUTPUT:
Write Message : hello this is os lab
Sent message is : hello this is os lab
Message Passing(client)

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>

// structure for message queue


struct mesg_buffer {
long mesg_type;
char mesg_text[100];
} message;
int main()
{
key_t key;
int msgid;

// ftok to generate unique key


key = ftok("progfile", 65);
// msgget creates a message queue
// and returns identifier
msgid = msgget(key, 0666 | IPC_CREAT);

// msgrcv to receive message


msgrcv(msgid, &message, sizeof(message), 1, 0);
// display the message
printf("Data Received is : %s \n",
message.mesg_text);
// to destroy the message queue
msgctl(msgid, IPC_RMID, NULL);
return 0;
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

}
OUTPUT:

Received Message is : hello this is os lab

d) shared Merory
shmhdr.h
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/shm.h>
#include<sys/sem.h>
#define SHMKEY 21250
#define PERMS 0666
#define MAXMESGDATA 1000
#define MESGHDRSIZE (sizeof(mesg)-MAXMESGDATA)
typedef struct{
int mesg_len;
int mesg_type;
char mesg_data[MAXMESGDATA];
}mesg;

shmclient.c
#include"shmhdr.h"
int shmid;
mesg *mesgptr;
main()
{
if((shmid=shmget(SHMKEY,sizeof(mesg),0))<0)
perror("can't get shared memory segment");
if((mesgptr=(mesg *)shmat(shmid,0,0))<0)
perror("can't attach shared memory");
client();
if(shmdt(mesgptr)<0)
perror("can't deattach shared memory");
if(shmctl(shmid,IPC_RMID,0)<0)
perror("can't remove shared memory");
exit(0);
}
client()
{
int n;
if(read(0,mesgptr->mesg_data,80)<0)
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

perror("error in reading from keyboard");


n=strlen(mesgptr->mesg_data);
if(write(1,mesgptr->mesg_data,strlen(mesgptr->mesg_data))!=strlen(mesgptr->mesg_data))
perror("error in writing on to screen");
}
shmserver.c
#include"shmhdr.h"
int shmid;
mesg *mesgptr;
main()
{
if((shmid=shmget(SHMKEY,sizeof(mesg),PERMS|IPC_CREAT))<0)
perror("error in shared memory creation");
if((mesgptr=(mesg *)shmat(shmid,0,0))<0)
perror("can't attach shared memory");
server();
if(shmdt(mesgptr)<0)
perror("can't deattach shared memory");
exit(0);
}
server()
{
strcat(mesgptr->mesg_data," from the server");
mesgptr->mesg_len=strlen(mesgptr->mesg_data);
}
Output:
cc shmclient.c -o shmclient
cc shmclient.c -o shmclient
$cc shmserver.c
$cc shmclient.c
hello
hello
from the server

BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN


OPERATING SYSTEMS LAB DEPT. OF IT

Program 6
Write C programs to simulate the following memory management techniques
a) Paging b) Segmentation

a) Paging

#include<stdio.h>
void main()
{
int memsize=15;
int pagesize,nofpage;
int p[100];
int frameno,offset;
int logadd,phyadd;
int i;
int choice=0;
printf("\nYour memsize is %d ",memsize);
printf("\nEnter page size:");
scanf("%d",&pagesize);

nofpage=memsize/pagesize;

for(i=0;i<nofpage;i++)
{
printf("\nEnter the frame of page%d:",i+1);
scanf("%d",&p[i]);
}

do
{
printf("\nEnter a logical address:");
scanf("%d",&logadd);
frameno=logadd/pagesize;
offset=logadd%pagesize;
phyadd=(p[frameno]*pagesize)+offset;
printf("\nPhysical address is:%d",phyadd);
printf("\nDo you want to continue(1/0)?:");
scanf("%d",&choice);
}while(choice==1);
}

OUTPUT:
Your memsize is 15
Enter page size:100

Enter a logical address:20


BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

Physical address is:20


Do you want to continue(1/0)?:1

Enter a logical address:15

Physical address is:15


Do you want to continue(1/0)?:0

b) Segmentation

#include<stdio.h>
void main()
{
int memsize=15;
int pagesize,nofpage;
int p[100];
int frameno,offset;
int logadd,phyadd;
int i;
int choice=0;
printf("\nYour memsize is %d ",memsize);
printf("\nEnter page size:");
scanf("%d",&pagesize);
nofpage=memsize/pagesize;
for(i=0;i<nofpage;i++)
{
printf("\nEnter the frame of page%d:",i+1);
scanf("%d",&p[i]);
}
do
{
printf("\nEnter a logical address:");
scanf("%d",&logadd);
frameno=logadd/pagesize;
offset=logadd%pagesize;
phyadd=(p[frameno]*pagesize)+offset;
printf("\nPhysical address is:%d",phyadd);
printf("\nDo you want to continue(1/0)?:");
scanf("%d",&choice);
}while(choice==1);
}

OUTPUT:
Enter the no of peges in memory: 4
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

Enter page size: 15


Enter no of frames: 2
Enter the page table
(Enter frame no as -1 if that page is not present in any frame)

pageno frameno
------- -------
0 1
1 -1
2 10
3 6

Enter the logical address(i.e,page no & offset):2 20


Physical address(i.e,frame no & offset):10,20
Do you want to continue(1/0)?:1
Enter the logical address(i.e,page no & offset):1 10
The required page is not available in any of frames
Do you want to continue(1/0)?:0

Program 7
Write C programs to simulate Page replacement policies a) FCFS b) LRU c) Optima
a) FCFS
#include<stdio.h>
#include<conio.h>
int fsize;
int frm[15];
void display();
void main()
{
int pg[100],nPage,i,j,pf=0,top=-1,temp,flag=0;
printf("\n Enter frame size:");
scanf("%d",&fsize);
printf("\n Enter number of pages:");
scanf("%d",&nPage);
for(i=0;i< nPage;i++)
{
printf("\n Enter page[%d]:",i+1);
scanf("%d",&pg[i]);
} for(i=0;i< fsize;i++)
frm[i]=-1;
printf("\n page | \t Frame content ");
printf("\n--------------------------------------");
for(j=0;j< nPage;j++)
{ flag=0;
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

for(i=0;i< fsize;i++)
{
if(frm[i]==pg[j])
{
flag=1;
break;
}
}
if(flag==0)
{
if(top==fsize-1)
{
top=-1;
}
pf++;
top++;
frm[top]=pg[j];
}
printf("\n %d |",pg[j]);
display();
}
printf("\n--------------------------------------");
printf("\n total page fault:%d",pf);
getch();
}
void display()
{
int i;
for(i=0;i< fsize;i++)
printf("\t %d",frm[i]);
}
Output:
Enter frame size:3
Enter number of pages:12
Enter page[1]:1
Enter page[2]:2
Enter page[3]:3
Enter page[4]:4
Enter page[5]:1
Enter page[6]:2
Enter page[7]:5
Enter page[8]:1
Enter page[9]:2
Enter page[10]:3
Enter page[11]:4
Enter page[12]:5
page | Frame content
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

--------------------------------------
1 | 1 -1 -1
2 | 1 2 -1
3 | 1 2 3
4 | 4 2 3
1 | 4 1 3
2 | 4 1 2
5 | 5 1 2
1 | 5 1 2
2 | 5 1 2
3 | 5 3 2
4 | 5 3 4
5 | 5 3 4
--------------------------------------
total page fault:9

b) LRU
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{ c1=0;
for(j=0;j<f;j++)
{ if(p[i]!=q[j])
c1++;
}
if(c1==f)
{ c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}
Output:
Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

7 5
7 5 9
4 5 9
4 3 9
4 3 7
9 3 7
9 6 7
9 6 2
1 6 2
The no of page faults is 10
Process returned 0 (0x0) execution time : 24.088 s

c) Optimal
#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k, pos, max,
faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);

printf("Enter number of pages: ");


scanf("%d", &no_of_pages);

printf("Enter page reference string: ");

for(i = 0; i < no_of_pages; ++i){


scanf("%d", &pages[i]);
}

for(i = 0; i < no_of_frames; ++i){


frames[i] = -1;
}

for(i = 0; i < no_of_pages; ++i){


flag1 = flag2 = 0;

for(j = 0; j < no_of_frames; ++j){


if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;
}
}

if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}

if(flag2 == 0){
flag3 =0;

for(j = 0; j < no_of_frames; ++j){


temp[j] = -1;

for(k = i + 1; k < no_of_pages; ++k){


if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}
}

for(j = 0; j < no_of_frames; ++j){


if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}

if(flag3 ==0){
max = temp[0];
pos = 0;

for(j = 1; j < no_of_frames; ++j){


if(temp[j] > max){
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}

printf("\n");
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

for(j = 0; j < no_of_frames; ++j){


printf("%d\t", frames[j]);
}
}

printf("\n\nTotal Page Faults = %d", faults);

return 0;
}

Output:

Enter number of frames: 3


Enter number of pages: 18
Enter page reference string: 1
2
3
4
5
6
1
2
3
4
5
1
1
2
3
4
5
6

1 -1 -1
1 2 -1
1 2 3
1 2 4
1 2 5
1 2 6
1 2 6
1 2 6
1 2 3
1 2 4
1 2 5
1 2 5
1 2 5
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

1 2 5
3 2 5
4 2 5
4 2 5
6 2 5

Total Page Faults = 12


Process returned 0 (0x0) execution time : 31.135 s

Program 8:
Write a C program to simulate the following file organization techniques
a) Single level directory b) Two level directory

a) Single level directory


#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);
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

strcpy(dir.fname[i],dir.fname[dir.fcnt-1]);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
else
dir.fcnt--;
break;
case 3: printf("\n Enter the name of the file -- ");
scanf("%s",f);
for(i=0;i<dir.fcnt;i++)
{
if(strcmp(f, dir.fname[i])==0)
{
printf("File %s is found ", f);
break;
}
}
if(i==dir.fcnt)
printf("File %s not found",f);
break;
case 4: if(dir.fcnt==0)
printf("\n Directory Empty");
else
{
printf("\n The Files are -- ");
for(i=0;i<dir.fcnt;i++)
printf("\t%s",dir.fname[i]);
}
break;
default: exit(0);
}
}
getch();
}

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

BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN


OPERATING SYSTEMS LAB DEPT. OF IT

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

b) TWO LEVEL DIRECTORY ORGANIZATION

#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);
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

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;
}
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 -- ");
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

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;
}
}
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
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

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


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

Enter name of directory -- DIR2

Directory created

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


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

Enter name of the directory – DIR1


Enter name of the file -- A1
File created

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


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

Enter name of the directory – DIR1


Enter name of the file -- A2
File created

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


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

Enter name of the directory – DIR2


Enter name of the file -- B1
File created

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


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

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


4. Search File 5. Display 6. Exit Enter your choice -- 4
Enter name of the directory – DIR
Directory not found
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice -- 3
Enter name of the directory – DIR1
Enter name of the file -- A2
File A2 is deleted
1. Create Directory 2. Create File 3. Delete File
4. Search File 5. Display 6. Exit Enter your choice – 6
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

Program 9:
Write a C program to simulate the concept of Dining-Philosophers problem

#include<stdio.h>
int tph, philname[20], status[20], howhung, hu[20], cho;
main()
{
int i;
printf("\n\nDINING PHILOSOPHER PROBLEM");
printf("\nEnter the total no. of philosophers: ");
scanf("%d",&tph);
for(i=0;i<tph;i++)
{
philname[i] = (i+1);
status[i]=1;
}
printf("How many are hungry : ");
scanf("%d", &howhung);
if(howhung>=tph)
{
printf("\nAll are hungry..\nDead lock stage will occur");
printf("\nExiting..");
}
else
{
for(i=0;i<howhung;i++)
{
printf("Enter philosopher %d position: ",(i+1));
scanf("%d", &hu[i]);
status[hu[i]]=2;
}
do
{
printf("1.One can eat at a time\t2.Two can eat at a time\t3.Exit\nEnter your choice:");
scanf("%d", &cho);
switch(cho)
{
case 1: one();
break;
case 2: two();
break;
case 3: exit(0);
default: printf("\nInvalid option..");
}
}while(1);
}
}
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

one()
{
int pos=0, x, i;
printf("\nAllow one philosopher to eat at any time\n");
for(i=0;i<howhung; i++, pos++)
{
printf("\nP %d is granted to eat", philname[hu[pos]]);
for(x=pos+1;x<howhung;x++)
printf("\nP %d is waiting", philname[hu[x]]);
}
}
two()
{
int i, j, s=0, t, r, x;
printf("\n Allow two philosophers to eat at same time\n");
for(i=0;i<howhung;i++)
{
for(j=i+1;j<howhung;j++)
{
if(abs(hu[i]-hu[j])>=1 && abs(hu[i]-hu[j])!=4)
{
printf("\n\ncombination %d \n", (s+1));
t=hu[i];
r=hu[j];
s++;
printf("\nP %d and P %d are granted to eat", philname[hu[i]], philname[hu[j]]);
for(x=0;x<howhung;x++)
{
if((hu[x]!=t)&&(hu[x]!=r))
printf("\nP %d is waiting", philname[hu[x]]);
}
}
}
}
}
INPUT
DINING PHILOSOPHER PROBLEM
Enter the total no. of philosophers: 5
How many are hungry : 3
Enter philosopher 1 position: 2
Enter philosopher 2 position: 4
Enter philosopher 3 position: 5
OUTPUT
1.One can eat at a time 2.Two can eat at a time 3.Exit
Enter your choice: 1
Allow one philosopher to eat at any time
P 3 is granted to eat
BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN
OPERATING SYSTEMS LAB DEPT. OF IT

P 3 is waiting
P 5 is waiting
P 0 is waiting
P 5 is granted to eat
P 5 is waiting
P 0 is waiting
P 0 is granted to eat
P 0 is waiting47
1.One can eat at a time 2.Two can eat at a time 3.Exit
Enter your choice: 2
Allow two philosophers to eat at same time
combination 1
P 3 and P 5 are granted to eat
P 0 is waiting
combination 2
P 3 and P 0 are granted to eat
P 5 is waiting
combination 3
P 5 and P 0 are granted to eat
P 3 is waiting
1.One can eat at a time 2.Two can eat at a time 3.Exit
Enter your choice: 3

BHOJ REDDY ENGINEERING COLLEGE FOR WOMEN

You might also like