You are on page 1of 35

OPERATING SYSTEMS

LAB MANUAL

II-B.TECH II-SEM

JNTUH-R18

Faculty In-charge:
Mr.A.SATEESH, Asst.Prof.

DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

OPERATING SYSTEMS LAB

LIST OF EXPERIMENTS:

1. Write C programs to simulate the following CPU Scheduling


0algorithms

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

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 1


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 2


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 3


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 4


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

OPERATING SYSTEMS LAB

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.
 Able to implement C programs using Unix system calls.

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 5


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

INSTRUCTIONS FOR LABORATORY

1. Conduct the experiments with interest and an attitude of learning.

2. You need to come well prepared for the experiment.

3. Work quietly and carefully and equally share the work with your partners.

4. Be honest in recording and representing your data.

5. Never make up readings.

6. Tables and calculations should be neatly and carefully done.

7. Bring necessary graph papers for each of the experiment.

8. Graphs should be neatly drawn with pencil.

9. Always label graphs and the axes and display units.

10. Come equipped with calculator, scales and pencils etc.,

11. Do not fiddle idly with the apparatus.

12. Handle instruments with care.

13. Report any breakage to the instructor.

14. Return all the equipment you have signed out for the purpose of your experiment.

15. Maintain an observation note book sequent.

16. Submit the record work with in stipulated time.

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 6


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

Aim:

1. Write C programs to simulate the following CPU Scheduling algorithms

A) FCFS (FIRST COME FIRST SERVE):

include<stdio.h>
#include<conio.h>
void main()
{
int i,j,bt# [10],n,wt[10],tt[10],w1=0,t1=0;
float aw,at;
clrscr();
printf("enter no. of processes:\n");
scanf("%d",&n);
printf("enter the burst time of processes:");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=w1/n;
at=t1/n;
printf("\nbt\t wt\t tt\n");
for(i=0;i<n;i++)
printf("%d\t %d\t %d\n",bt[i],wt[i],tt[i]);
printf("aw=%f\n,at=%f\n",aw,at);
getch();
}
OUTPUT:

Enter no of processes
3
enter bursttime
12
8
20

bt wt tt

12 0 12
8 12 20
20 20 40
aw=10.666670
at=24.00000

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 7


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

B) SJF (SHORTEST JOB FIRST):

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,bt[10],t,n,wt[10],tt[10],w1=0,t1=0;
float aw,at;
clrscr();
printf("enter no. of processes:\n");
scanf("%d",&n);
printf("enter the burst time of processes:");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
if(bt[i]>bt[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
}
}
for(i=0;i<n;i++)
printf("%d",bt[i]);
for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 8
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=w1/n;
at=t1/n;
printf("\nbt\t wt\t tt\n");
for(i=0;i<n;i++)
printf("%d\t %d\t %d\n",bt[i],wt[i],tt[i]);
printf("aw=%f\n,at=%f\n",aw,at);
getch();
}
OUTPUT:

Enter no of processes
3
Enter burst time
12
8
20

bt wt tt
12 8 20
8 0 8
20 20 40
aw=9.33
at=22.64

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 9


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

C) Round Robin:

#include<stdio.h>
#include<conio.h>
void main()
{
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0;
float awt=0.0,atat=0.0;
clrscr();
printf("Enter number of processes:");
scanf("%d",&n);
printf("Enter burst time for sequences:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
st[i]=bt[i];
}
printf("Enter time quantum:");
scanf("%d",&tq);
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)
st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 10


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("Process_no Burst time Wait time Turn around time");
for(i=0;i<n;i++)
printf("\n %d\t %d\t %d\t %d",i+1,bt[i],wt[i],tat[i]);
printf("\n Avg wait time is %f Avg turn around time is %f",awt,atat);
getch();
}

OUTPUT:

Enter no of jobs
4
Enter burst time
5
12
0038
20

Bt wt tt
5 0 5
12 5 13
8 13 25
20 2545

aw=10.75000
at=22.000000

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 11


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

D) PRIORITY:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,pno[10],prior[10],bt[10],n,wt[10],tt[10],w1=0,t1=0,s;
float aw,at;
clrscr();
printf("enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("The process %d:\n",i+1);
printf("Enter the burst time of processes:");
scanf("%d",&bt[i]);
printf("Enter the priority of processes %d:",i+1);
scanf("%d",&prior[i]);
pno[i]=i+1;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(prior[i]<prior[j])
{
s=prior[i];
prior[i]=prior[j];
prior[j]=s;
s=bt[i];
bt[i]=bt[j];
bt[j]=s;
s=pno[i];
pno[i]=pno[j];
pno[j]=s;
}
}
}
for(i=0;i<n;i++)
{
wt[0]=0;
tt[0]=bt[0];
wt[i+1]=bt[i]+wt[i];
tt[i+1]=tt[i]+bt[i+1];
w1=w1+wt[i];
t1=t1+tt[i];
aw=w1/n;
at=t1/n;
}
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 12
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

14
printf(" \n job \t bt \t wt \t tat \t prior\n");
for(i=0;i<n;i++)
printf("%d \t %d \t %d\t %d\t %d\n",pno[i],bt[i],wt[i],tt[i],prior[i]);
printf("aw=%f \t at=%f \n",aw,at);
getch();
}

OUTPUT:

Enter no of jobs
4
Enter bursttime
10
2
4
7
Enter priority values
4
2
1
3

Bt priority wt tt

4104
2246
7 3 6 13
10 4 13 23
aw=5.750000
at=12.500000

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 13


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

Aim:

2. Write programs using the I/O system calls of UNIX/LINUX operating system

(open, read, write, close, fcntl, seek, stat, opendir, readdir)

(a) opendir, readdir


#include<stdio.h>
#include<sys/types.h>
#include<sys/dir.h>
void main(int age,char *argv[])

{
DIR *dir;
struct dirent *rddir;
printf("\n Listing the directory content\n");
dir=opendir(argv[1]);
while((rddir=readdir(dir))!=NULL)
{
printf("%s\t\n",rddir->d_name);
}
closedir(dir);
}

OUTPUT:
RP roshi.c
first.c
pk6.c
f2
abc
FILE1

(b) callstat( ),read(),write,close(),fcntl& open( ) and Iseek()


#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h> main( )
{
int fd[2];

char buf1[25]= ”just a test\n”;


char buf2[50];

fd[0]=open(“file1”, O_RDWR);
fd[1]=open(“file2”, O_RDWR);
write(fd[0], buf1, strlen(buf1));
printf(“\n Enter the text now….”);
gets(buf1);

write(fd[0], buf1, strlen(buf1));


lseek(fd[0], SEEK_SET, 0);
read(fd[0], buf2, sizeof(buf1));
write(fd[1], buf2, sizeof(buf2));
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 14
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

close(fd[0]);

close(fd[1]);
printf(“\n”);
return0;
}

OUTPUT:
Enter the text now….progress
Cat file1 Just a test progress

Cat file2 Just a test progress

Aim:
3. Write a C program to simulate Bankers Algorithm for Deadlock Avoidance and Prevention

a) Write a C program to simulate Bankers Algorithm for Dead Lock Avoidance


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
int p, r, i, j, process, count;
clrscr();
count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 15
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

printf("\n\nEnter the allocation for each process : ");


for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
for(i = 0; i < p; i++)
for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];
do
{
printf("\n Max matrix:\tAllocation matrix:\n");

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


{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
for(i = 0; i < p; i++)
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 16
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}
if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}
while(count != p && process != -1);
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 17
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
getch();
}
OUTPUT :
Enter the no of processes : 5
Enter the no of resources : 3
Enter the Max Matrix for each process :

For process 1 : 7
5
3

For process 2 : 3
2
2

For process 3 : 7
0
2

For process 4 : 2
2
2

For process 5 : 4
3
3

Enter the allocation for each process :


For process 1 : 0
1
0

For process 2 : 2

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 18


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

0
0

For process 3 : 3
0
2

For process 4 : 2
1
1

For process 5 : 0
0
2

Enter the Available Resources : 3


3
2

Max matrix: Allocation matrix:


753 010
322 200
702 302
222 211
433 002

Process 2 runs to completion!


Max matrix: Allocation matrix:
753 010
000 000
702 302
222 211
433 002

Process 3 runs to completion!


Max matrix: Allocation matrix:
753 010
000 000
000 000
222 211
433 002

Process 4 runs to completion!


Max matrix: Allocation matrix:
753 010
000 000
000 000
000 000
433 002

Process 1 runs to completion!


Max matrix: Allocation matrix:
000 000
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 19
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

000 000
000 000
000 000
433 002

Process 5 runs to completion!


The system is in a safe state!!
Safe Sequence : < 2 3 4 1 5 >

Aim:
3) b) Write a C program to simulate Bankers Algorithm for Dead Lock Prevention
#include<stdio.h>
#include<conio.h>
void main()
{
int allocated[15][15],max[15][15],need[15][15],avail[15],tres[15],work[15],flag[15];
int pno,rno,i,j,prc,count,t,total;
count=0;
clrscr();
printf("\n Enter number of process:");
scanf("%d",&pno);
printf("\n Enter number of resources:");
scanf("%d",&rno);
for(i=1;i<= pno;i++)
{
flag[i]=0;
}
printf("\n Enter total numbers of each resources:");
for(i=1;i<= rno;i++)
scanf("%d",&tres[i]);
printf("\n Enter Max resources for each process:");
for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i);
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 20
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

for(j=1;j<= rno;j++)
scanf("%d",&max[i][j]);
}
printf("\n Enter allocated resources for each process:");
for(i=1;i<= pno;i++)
{
printf("\n for process %d:",i);
for(j=1;j<= rno;j++)
scanf("%d",&allocated[i][j]);
}
printf("\n available resources:\n");
for(j=1;j<= rno;j++)
{
avail[j]=0;
total=0;
for(i=1;i<= pno;i++)
{
total+=allocated[i][j];
}
avail[j]=tres[j]-total;
work[j]=avail[j];
printf("%d \t",work[j]);
}
do
{
for(i=1;i<= pno;i++)
{
for(j=1;j<= rno;j++)
{
need[i][j]=max[i][j]-allocated[i][j];
}
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 21
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

}
printf("\n Allocated matrix Max need");
for(i=1;i<= pno;i++)
{
printf("\n");
for(j=1;j<= rno;j++)
{
printf("%4d",allocated[i][j]);
}
printf("|");
for(j=1;j<= rno;j++)
{
printf("%4d",max[i][j]);
}
printf("|");
for(j=1;j<= rno;j++)
{
printf("%4d",need[i][j]);
}
}
prc=0;
for(i=1;i<= pno;i++)
{
if(flag[i]==0)
{
prc=i;
for(j=1;j<= rno;j++)
{
if(work[j]< need[i][j])
{
prc=0;
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 22
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

break;
}
}
}
if(prc!=0)
break;
}
if(prc!=0)
{
printf("\n Process %d completed",i);
count++;
printf("\n Available matrix:");
for(j=1;j<= rno;j++)
{
work[j]+=allocated[prc][j];
allocated[prc][j]=0;
max[prc][j]=0;
flag[prc]=1;
printf(" %d",work[j]);
}
}
}while(count!=pno&&prc!=0);
if(count==pno)
printf("\nThe system is in a safe state!!");
else
printf("\nThe system is in an unsafe state!!");
getch();
}
OUTPUT :
Enter number of process:5

Enter number of resources:3


KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 23
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

Enter total numbers of each resources:10 5 7

Enter Max resources for each process:

for process 1:7 5 3

for process 2:3 2 2

for process 3:9 0 2

for process 4:2 2 2

for process 5:4 3 3

Enter allocated resources for each process:

for process 1:0 1 0

for process 2:3 0 2


for process 3:3 0 2

for process 4:2 1 1

for process 5:0 0 2

available resources:
2 3 0

Allocated matrix Max need


0 1 0| 7 5 3| 7 4 3
3 0 2| 3 2 2| 0 2 0
3 0 2| 9 0 2| 6 0 0
2 1 1| 2 2 2| 0 1 1
0 0 2| 4 3 3| 4 3 1
Process 2 completed
Available matrix: 5 3 2
Allocated matrix Max need
0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
2 1 1| 2 2 2| 0 1 1
0 0 2| 4 3 3| 4 3 1
Process 4 completed
Available matrix: 7 4 3
Allocated matrix Max need
0 1 0| 7 5 3| 7 4 3
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 1 completed
Available matrix: 7 5 3
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 24
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

Allocated matrix Max need


0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
3 0 2| 9 0 2| 6 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1
Process 3 completed

Available matrix: 10 5 5

Allocated matrix Max need


0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 0| 0 0 0| 0 0 0
0 0 2| 4 3 3| 4 3 1

Process 5 completed
Available matrix: 10 5 7
The system is in a safe state!!
Aim:

4. Write a C program to implement the Producer – Consumer problem using semaphores using

UNIX/LINUX system calls


#include<s
tdio.h>
void
main()
{
int buffer[10], bufsize, in, out, produce,
consume, choice=0; in = 0;
out = 0;
bufsize = 10;
while(choice !=3)
{
printf(“\n1. Produce \t 2.
Consume \t3. Exit”); printf(“\
nEnter your choice: ”);
scanf(“%d
”,
&choice);
switch(ch
oice) {
case 1: if((in+1)%bufsize==out)
printf(“\nBuffer is Full”);
else
{
printf(“\nEnter the value: “);
scanf(“%d”, &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
}
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 25
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

Break;
case 2: if(in == out)
printf(“\nBuffer is Empty”);
else
{
consume = buffer[out];
printf(“\nThe consumed value is %d”, consume);
out = (out+1)%bufsize;
}
break;
} } }
OUTPUT
1. Produce 2. Consume 3. Exit Enter your choice: 2
Buffer is Empty
1. Produce 2. Consume 3. Exit Enter your choice: 1
Enter the value: 100
1. Produce 2. Consume 3. Exit Enter your choice: 2
The consumed value is 100
1. Produce 2. Consume 3. Exit Enter your choice: 3
Aim:

5. Write C programs to illustrate the following IPC mechanisms

a) Pipes b) FIFOs c) Message Queues d) Shared Memory

5) a) Pipes

#include <stdio.h>
#include <sys/types.h>
#include<stdlib.h>
#include <unistd.h>
int main()
{
int fd[2];
if(pipe(fd)<0)
exit(1);
if(fork())
{
close(fd[0]);
write(fd[1],"Message from KITS COLLEGE",27);
}
else
{
char buf[100];
close(fd[1]);
read(fd[0],buf,100);
printf("Received by Students of KITS COLLEGE: %s\n",buf);
fflush(stdout);
}
exit(0);
return 0;
}

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 26


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

Run the program :

[srinivas@localhost ~]$ cc prog22.c

OUTPUT :

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

Received by Students of KITS COLLEGE:

Message from KITS COLLEGE

5) b) FIFOs

/* reader program reader.c */


#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
int fd;
char str[100];
unlink("aPipe"); /* named pipe if it already exists */
mknod("aPipe",S_IFIFO,0); /*Create a named pipe */
chmod("aPipe",0660); /* Change its permissions */
fd=open("aPipe",O_RDONLY); /*open it for reading */
while(readLine(fd,str)); /*display received messages */
printf("%s\n",str);
close(fd); /* close pipe */
return 0;
}
readLine(int fd,char* str)
{
/* read a single line into str from fd */
/* return 0 when the end-of input is reached and 1 otherwise */
int n;
do /* read characters until NULL or end-of-input */
{
n=read(fd,str,1); /* read one character */
}
while(n>0 && *str++!=NULL);
return (n>0); /* return false if end-of-inputs */
}
/* Writer Program writer.c */
#include<stdio.h>
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 27
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

#include<fcntl.h>
#include<string.h>
int main()
{
int fd,messageLen,i;
char message[100];
/* prepare message */
sprintf(message,"Hello from PID %d", getpid());
messageLen=strlen(message)+1;
do /* keep trying to open the file until successful */
{
fd=open("aPipe",O_WRONLY); /* open named pipe for writing */
if(fd==-1)
sleep(1); /* try again in 1 second */
}while(fd==-1);

for(i=1;i<=3;i++) /* send 3 messages */


{
write(fd,message,messageLen); /* write message down pipe */
sleep(3); /* pause a while */
}
close(fd); /* close pipe descriptor */
return 0;
}

INPUT :
[srinivas@localhost ~]$ cc reader.c
[srinivas@localhost ~]$ cc writer.c -o b.out
srinivas@localhost ~]$ ./a.out & ./b.out & ./b.out &
OUTPUT :
[1] 3472 /* reader process */
[2] 3473 /* first writer process */
[3] 3474 /* second writer process */
Hello from PID 3473
Hello from PID 3474
Hello from PID 3473
Hello from PID 3474
Hello from PID 3473
Hello from PID 3474
[1] Done ./a.out /* reader exits */
[2]- Done ./b.out /* second writer exits */
[3]+ Done ./b.out /* first writer exits */

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 28


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

c) Message Queues

#include <stdio.h>
#include <sys/ipc.h>
#include <fcntl.h>
#include<stdlib.h>
#include<string.h>
#define MAX 255
struct mesg
{
long type;
char mtext[MAX];
} *mesg;
char buff[MAX];
main()
{
int mid,fd,n,count=0;;
if((mid=msgget(1006,IPC_CREAT | 0666))<0)
{
printf("\n Can.t create Message Q");
exit(1);
}
printf("\n Queue id:%d", mid);
mesg=(struct mesg *)malloc(sizeof(struct mesg));
mesg ->type=6;
fd=open("fact",O_RDONLY);
while(read(fd,buff,25)>0)
{
strcpy(mesg ->mtext,buff);
if(msgsnd(mid,mesg,strlen(mesg ->mtext),0)== -1)
printf("\n Message Write Error");
}

if((mid=msgget(1006,0))<0)
{
printf("\n Can.t create Message Q");
exit(1);
}
while((n=msgrcv(mid,&mesg,MAX,6,IPC_NOWAIT))>0)
write(1,mesg,n);
count++;
if((n==-1)&&(count==0))
printf("\n No Message Queue on Queue:%d", mid);

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 29


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

5) d) Shared Memory

#include <stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<stdlib.h>
#include<string.h>

#define SEGSIZE 100

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


{
int shmid,cntr;
key_t key;
char *segptr;
char buff[]="Hello world";
key=ftok(".",'s');
if((shmid=shmget(key,SEGSIZE,IPC_CREAT |
IPC_EXCL | 0666))== -1)
{
if((shmid=shmget(key,SEGSIZE,0))== -1)
{
perror("shmget");
exit(1);
}
}
else
{
printf("Creating a new shared memory seg \n");
printf("SHMID:%d", shmid);
}
system("ipcs -m");
if((segptr=shmat(shmid,0,0))==(char*)-1)
{
perror("shmat");
exit(1);
}
printf("Writing data to shared memory..\n");
strcpy(segptr,buff);
printf("DONE\n");
printf("Reading data from shared memory..\n");
printf("DATA:-%s\n",segptr);
printf("DONE\n");
printf("Removing shared memory Segment..\n");
if(shmctl(shmid,IPC_RMID,0)==-1)
printf("Can't Remove Shared memory Segment..\n");
else
printf("Removed Successfully");
}

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 30


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

Run the program :


[srinivas@localhost ~]$ cc prog28.c
[srinivas@localhost ~]$ ./a.out
OUTPUT :
Creating a new shared memory seg

------ Shared Memory Segments --------


key shmid owner perms bytes nattch status
0x00000000 65537 root 644 40 2
0x00000000 98306 root 644 16384 2
0x00000000 131075 root 644 268 2
0x73000a36 229382 srinivas 666 100 0

SHMID:229382Writing data to shared memory..


DONE
Reading data from shared memory..n
DATA:-Hello world
DONE
Removing shared memory Segment..
Removed Successfully.

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 31


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

Aim:

6. Write C programs to simulate the following memory management techniques

a) Paging b) Segmentation

6) a) Paging

#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); sa=(int*)malloc(2*np); for(i=0;i<np;i++)
{
sa[i]=(int)malloc(ps);
printf("page%d\t address %u\n",i+1,sa[i]);
}
getch();
}
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

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 32


OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

6) b) Segmentation :
#include<stdio.h>
#include<unistd.h>
void main()
{
int b[20],l[20],n,i,pa,s,a,d;
printf(“\nProgram for segmentation”);
printf(“\nEnter the number of segments:”);
scanf(“%d”,&n);
printf(“\nEnter the base address and limit register:”);
for(i=0;i<n;i++)
{
scanf(“%d”,&b[i]);
scanf(“%d”,&l[i])
}
printf(“\nEnter the logical address:”);
scanf(“%d”,&d);
for(i=0;i<n;i++)
{
if(i==s)
{
if(d<l[i])
{
pa=b[i]+d;
a=b[i];
printf(“(“\n\tPageNo.\t BaseAdd. PhysicalAdd. \n\t %d \t %d \t
%d \t ”,s,a,pa);
exit(0);
}
else
{
KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 33
OPERATING SYSTEMS LAB MANUAL COMPUTER SCIENCE & ENGINEERING

printf(“\nPage size exceeds”);


exit(0);
}
}
}
printf(“\nInvalid segment”);
}
OUTPUT:

Program for segmentation


Enter the number of segments:3
Enter the base address and limit register:
100 50
150 20
130 34
Enter the Logical address:25
Enter the segment number:1

KHAMMAM INSTITUTE OF TECHNOLOGY & SCIENCES, KHAMMAM 34

You might also like