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

OS Lab Manual R22 CSM

OS Lab Manual R22 JNTUH

Uploaded by

vanisrirao2025
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)
17 views48 pages

OS Lab Manual R22 CSM

OS Lab Manual R22 JNTUH

Uploaded by

vanisrirao2025
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

Operating System Laboratory Manual

Regulation: R22
Class: II B Tech I Semester CSE(AI & ML)
Academic Year: 2022-23

Department of Computer Science and Engineering(AI & ML)


(Affiliated to Jawaharlal Nehru technological university & Recognized by AICTE)
17-1-209/B, Vinaynagar, Saidabad, Hyderabad-500059

Vision of Institution – BRECW


BRECW develops confident and articulate young women in to 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 team work, 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 Computer Science & Engineering
Vision
To become a world-class technical center for making women into dynamic Computer
Science Engineers of tomorrow.
Mission

 To impart high quality professional training in a challenging and safe environment


with an emphasis on noble values.

 To encourage women engineers to upgrade their programming, analyzing, and


developing skills in building computing algorithms for leading meaningful lives.

 To make learning process exciting and interesting through curricular and co-
curricular program with staff molding them into leaders and entrepreneurs.

 To nurture the students with the required skills to solve technical problems of
modern society with courage and confidence.

Programme Educational Objectives of CSE Department

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

PEO1: Apply the knowledge of mathematics and computer fundamentals to investigate


solutions for realistic problems.
PEO2: Design and develop modern tools to solve complex projects with an ethical
approach to meet the needs of the global society.

PEO3: Demonstrate teamwork and leadership qualities to develop innovative products


utilizing various programming and intellectual skills by engaging in life-long learning.

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: Identify suitable data structures and algorithms to design and develop computing
solutions for real-life problems.

PSO2: Able to excel in various programming, project competitions and technological


challenges laid by professional societies.

Mapping of PEOs and POs/PSOs


PO P P P P P P P P PO PO1 PO1 PS PS
1 O2 O3 O4 O5 O6 O7 O8 O9 10 1 2 O1 O2
PEO √ √ √ √ √ √ √ √ √
1
PEO- √ √ √ √ √ √ √
2
PEO- √ √ √ √ √ √ √
3

INDEX

S. Title Page No.


No
1 Write C programs to simulate the 0
following CPU Scheduling algorithms a)
FCFS b) SJF c) Round Robin d) priority
2 Write programs using the I/O system calls 02
of UNIX/LINUX operating system (open,
read, write, close, fcntl, seek, stat,
opendir, readdir)
3 Write a C program to simulate Bankers 03
Algorithm for Deadlock Avoidance and
Prevention.
4 Write a C program to implement the 04
Producer – Consumer problem using
semaphores using UNIX/LINUX system
calls.

5 Write C programs to illustrate the 06


following IPC mechanisms a) Pipes b)
FIFOs c) Message Queues d) Shared
Memory
6 Write C programs to simulate the 15
following memory management
techniques a) Paging b) Segmentation
7 Write C programs to simulate Page 16
replacement policies a) FCFS b) LRU c)
Optima

Operating System LAB


B.Tech. II Year I Sem. LTPC
003
1.5

Prerequisites
● A course on “Programming for Problem Solving”, A course on “Computer
Organization and Architecture”
Co-requisite
● A course on “Operating Systems”
Course Objectives:
● To provide an understanding of the design aspects of operating system concepts
through simulation
 Introduce basic Unix commands, system call interface for process management,
interprocess communication and I/O in Unix.
Course Outcomes:
● Simulate and implement operating system concepts such as scheduling, deadlock
management, file management and memory management

List of Experiments:
Program 1
Aim: 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("-----------------------------------");
}

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()
{
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");
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;
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];
}
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)
---------------------------------------
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;

}
}
}
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
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
Aim: 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);
}
}

OUTPUT:
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);
}
}

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);
}

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};
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

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);
}
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);
}
OUTPUT:

The char at offset 5=e


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);
}

OUTPUT:

I like OS.This is my OS program.


I am in BRECW
Program 3

Aim: 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");
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])
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");
}
}

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

Aim: Write a C program to implement the Producer – Consumer problem using


semaphores using UNIX/LINUX system calls.

#include<stdio.h>
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

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
Aim: 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]));
}
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

#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 {
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;
}
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)
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
Program 6
Aim: 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

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
Program 7
Aim: 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;
//clrscr();
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;
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


--------------------------------------
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++)
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
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) Optima
#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){
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");
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
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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy