Operating Systems R18-Lab Manual
Operating Systems R18-Lab Manual
AND
TECHNOLOGICAL INSTITUTE
Aushapur, Ghatkesar, Medchal-501301
Designation : ________________________________
Department : ________________________________
OPERATING SYSTEMS LAB MANUAL
INDEX
S.No Name of the Experiment Page No.
1 Course Outcomes i
a) FCFS
1
2 b) SJF
4
c) Round Robin
8
d) Priority
12
Write programs using the I/O system calls of UNIX/LINUX operating
3 system 17
Write a C program to simulate Bankers Algorithm for Deadlock Avoidance
4 34
and Prevention.
Write a C program to implement the Producer – Consumer problem using
5 38
semaphores using UNIX/LINUX system calls
a) Pipes
42
6 b) FIFOs
44
c) Message Queues
48
d) Shared Memory
53
Write C programs to simulate the following memory management
techniques:
7 a) Paging
56
b) Segmentation
59
OPERATING SYSTEMS LAB MANUAL
a) First-Fit 63
1
b) Best-Fit 66
c) Worst-fit 69
Simulate the following memory allocation algorithms
a) FCFS 72
2
b) SCAN 74
c) SSTF 78
OPERATING SYSTEMS LAB MANUAL
Course Objectives:
▪ To write programs in Linux environment using the I/O system calls of UNIX/LINUX
operating system.
system calls.
Course Outcomes:
DESCRIPTION:
For FCFS scheduling algorithm, read the number of processes/jobs in the system,
their CPU burst times. The scheduling is performed on the basis of arrival time of the
processes irrespective of their other parameters. Each process will be executed
according to its arrival time. Calculate the waiting time and turnaround time of each
of the processes accordingly.
ALGORITHM:
Step 1: Start
Step 2: Define a structure process with elements p,bt,wt,tat. Step 3:
Read the Processes details p, & bt
Step 4: Initialize
wt[0]=avgwt=0;
avgtat=tat[0]=bt[0];
Step 5: for i=1 to i<n do till step6
Step 6: wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
Step7: for i=0 to n do step 8
Step8: Print the output with the FCFS Fashion and Calculating bt,wt,&tat
Step 9: End
PROGRAM: FCFS CPU SCHEDULING ALGORITHM
#include<stdio.h>
int main( )
{
char p[10][10];
int bt[10],wt[10],tat[10],i,n; float
avgwt,avgtat; printf("enter no of
processes:"); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process %d name:\t",i+1);
scanf("%s",p[i]);
printf("enter burst time\t");
scanf("%d",&bt[i]);
}
wt[0]=avgwt=0;
avgtat=tat[0]=bt[0];
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
}
printf("p_name\t B_time\t w_time\t turnarounftime\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\t%d\n",p[i],bt[i],wt[i],tat[i]); printf("\navg
waiting time=%f", avgwt/n);
printf("\navg tat time=%f\n", avgtat/n);
return 0; }
OUTPUT:
student@NNRG310:~/oslab$ cc fcfs.c
no of processes: 3
P1 24 0 24
P2 3 24 27
P3 3 27 30
tat time=27.000000
student@NNRG310:~/oslab$
b) SJF CPU SCHEDULING ALGORITHM
DESCRIPTION:
For SJF(Shortest Job First) scheduling algorithm, read the number of processes/jobs
in the system, their CPU burst times. Arrange all the jobs in order with respect to
their burst times. There may be two jobs in queue with the same execution time,
and then FCFS approach is to be performed. Each process will be executed acco ding
to the length of its burst time. Then calculate the waiting time and turnaround time
of each of the processes accordingly.
ALGORITHM:
Step 1: Start
Step 2: Define a structure process with elements p,bt,wt,tat Step3:
Read process name P,burst time bt of the process Step4:
for i=0 to n go to step 6
Step:5 for j=0;j< i do
if(bt[i]<bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
k=p[i];
p[i]=p[j];
p[j]=k;
}
Step6: else avgwt=wt[0]=0;
avgtat=tat[0]=bt[0]; Step
7: for i=1;i<n do
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
Step 8: Print the output with the SJF Fashion and Calculating
Pid,bt,wt,&tat
Step 9: End
#include<stdio.h>
int main()
{
int i,j,k,n,temp;
int p[10],bt[10],wt[10],tat[10]; float
avgtat,avgwt;
printf("enter no of processes: \t");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process name:\t");
scanf("%d",&p[i]);
printf("enter burst time \t");
scanf("%d",&bt[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
if(bt[i]<bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
k=p[i];
p[i]=p[j];
p[j]=k;
}
}
}
avgwt=wt[0]=0;
avgtat=tat[0]=bt[0];
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
}
printf("p_name\t B_time\t w_time\t turnarounftime\n");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\t%d\n",p[i],bt[i],wt[i],tat[i]); printf("\navg
waiting time=%f\n", avgwt/n);
printf("avg tat time=%f\n", avgtat/n);
}
OUTPUT:
enter no of processes: 4
4 3 0 3
1 6 3 9
3 7 9 16
2 8 16 24
tat time=13.000000
student@NNRG310:~/oslab$
c) ROUND ROBIN CPU SCHEDULING ALGORITHM
DESCRIPTION:
For round robin scheduling algorithm, read the number of processes/ jobs in the
system, their CPU burst times, and the size of the time slice. Time slices are assigned
to each process in equal portions and in circular order, handling all processes
execution. This allows every process to get an equal chance.
ALGORITHM:
Step 1: Start
Step 2: Define a structure process with elements st,bt,wt,tat,n,tq Step 3:
Read i,n.tq
Step 4: Read the Processes details n & bt Step
5: for i=0 to i<n do st[i]=bt[i]
Step 6: for i=0,count=0;i<n; do till step 7
Step 7: check 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;
Step 8: if (n= =count) break;
Step 9: else wt[i]=tat[i]-bt[i];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
Step 10: Print the output with the RoundRobin Fashion and
Calculating Pid,bt,wt,&tat
Step 11: End
PROGRAM: ROUND ROBIN CPU SCHEDULING ALGORITHM
#include<stdio.h>
#include<stdlib.h>
int main()
{
int p[10],st[10],bt[10],wt[10],tat[10],n,tq; int
i,count=0,temp,sq=0;
float avgwt=0.0,avgtat=0.0;
system("clear");
printf("Enter number of processes:\t");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process number:\t");
scanf("%d",&p[i]);
printf("enter burst time:\t");
scanf("%d",&bt[i]);
st[i]=bt[i];
}
student@NNRG310:~/oslab$ cc rr.c
student@NNRG310:~/oslab$ ./a.out
process number: 1
process number: 2
1 24 6 30
2 3 4 7
3 3 7 10
student@NNRG310:~/oslab$
d) PRIORITY CPU SCHEDULING ALGORITHM
DESCRIPTION:
For priority scheduling algorithm, read the number of processes/jobs in the system,
their CPU burst times, and the priorities. Arrange all the jobs in order with respect to
their priorities. There may be two jobs in queue with the same priority, and then
FCFS approach is to be performed. Each process will be executed according to its
priority. Calculate the waiting time and turnaround time of each of the processes
accordingly.
ALGORITHM:
Step1: Start
Step2: Define a structure process with elements p,bt,wt,tatSte Step3:
Read the Processes details pid, & bt
Step4: for i=0 to i<n do still step5
Step5: for j=0 to j<n do till step 6
Step6: if(pr[i]>pr[j])
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
Step7: initialize avgwt=wt[0]=0;
avgtat=tat[0]=bt[0];
Step8: for i=1;i<n do till step 9
Step9: wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
Step10: Print the output with the FCFS Fashion and Calculating
Pid,bt,wt,&tat
Step11 : End
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,n,temp;
int p[10],pr[10],bt[10],wt[10],tat[10]; float
avgtat,avgwt;
system ("clear");
printf("enter no of processes:\t");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process number:\t");
scanf("%d",&p[i]);
printf("enter burst time:\t");
scanf("%d",&bt[i]);
printf("enter priority:\t");
scanf("%d",&pr[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(pr[i]<pr[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pr[i];
pr[i]=pr[j];
pr[j]=temp;
}
}
}
avgwt=wt[0]=0;
avgtat=tat[0]=bt[0];
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
}
printf("p_name\t B_time\t w_time\t turnarounftime\n");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\t%d\n",p[i],bt[i],wt[i],tat[i]); printf("\navg
waiting time=%f\n", avgwt/n);
printf("avg tat time=%f\n", avgtat/n);
}
OUTPUT:
./a.out
enter no of processes: 5
enter priority: 3
enter priority: 1
enter priority: 4
enter priority: 5
enter priority: 2
4 1 0 1
3 2 1 3
1 10 3 13
5 5 13 18
2 1 18 19
avg waiting time=7.000000 avg
tat time=10.800000
student@NNRG310:~/oslab$
2. Write programs using the I/O system calls of UNIX/LINUX operating system
a) open ( ) system call
DESCRIPTION:
Used to open the file for reading, writing or both. This function returns the file
descriptor or in case of an error -1. The number of arguments that this function can
have is two or three. The third argument is used only when creating a new file. When
we want to open an existing file only two arguments are used.
call DESCRIPTION:
fd = open("f3.txt", O_RDONLY); if
(fd==-1)
{
perror("r1");
exit(1);
}
sz=read(fd,c,13);
printf("called read(%d, c, 10). returned that" " %d bytes were read.\n", fd, sz);
c[sz] = '\0';
printf("Those bytes are as follows: %s\n", c); return
0; }
OUTPUT:
ca> f3.txt
From the file indicated by the file descriptor fd, the read() function reads cnt
bytes of input into the memory area indicated by buf.
student@NNRG310:~/oslab$ cc read.c
student@NNRG310:~/oslab$ ./a.out
called read(3, c, 10). returned that 13 bytes were read. Those
bytes are as follows: From the file
c) write ( ) system
call DESCRIPTION:
// C program to illustrate
// write system Call
#include<stdio.h>
#include <fcntl.h>
#include<stdlib.h>
#include <unistd.h>
#include<string.h> int
main( )
{
int sz;
student@NNRG310:~/oslab$ cc write.c
student@NNRG310:~/oslab$ ./a.out
called write(3, "hello linux", 11). It returned 11
student@NNRG310:~/oslab$ cat f4.txt
hello linux
d) close ( ) system
call DESCRIPTION:
#include<stdio.h>
#include <fcntl.h>
#include<stdlib.h>
#include <unistd.h>
int main()
(fd1==-1)
perror("c1");
exit(1);
(close(fd1)==-1)
perror("c1");
exit(1);
}
0;
OUTPUT:
student@NNRG310:~/oslab$ ./a.out
opened the fd = 3
call DESCRIPTION:
The fcntl system call is the access point for several advanced operations on file
descriptors. The first argument to fcntl is an open file descriptor, and the second is a
value that indicates which operation is to be performed. For some operations, fcntl
takes an additional argument. We'll describe here one of the most useful fcntl
operations, file locking
<stdio.h> #include
<string.h> #include
<unistd.h>
int fd;
printf ("locking\n");
F_WRLCK;
/* Place a write lock on the file. */ fcntl
getchar ();
printf ("unlocking\n");
close (fd);
return 0;
}
OUTPUT:
Terminal-1
Terminal-2
f ) seek ( ) system call
DESCRIPTION:
The lseek() function allows the file offset to be set beyond the end of the file (but this
does not change the size of the file). If data is later written at this point, subsequent
reads of the data in the gap (a "hole") return null bytes (’\0’) until data is actually
written into the gap.
#include <unistd.h>
<sys/types.h>
#include<stdio.h>
int main()
< -1)
return 1; char
buffer[19];
printf("%s\n",buffer);
printf("%s\n",buffer);
return 0;
}
OUTPUT:
Cat> f4.txt
lseek is a system call that is used to change the location of the read/write pointer of
a file descriptor. The location can be set either in absolute or relative terms.
student@NNRG310:~/oslab$ cc lseek.c
a system c
student@NNRG310:~/oslab$
g ) stat ( ) system call
DESCRIPTION:
These functions return information about a file. No permissions are required on the
file itself, but — in the case of stat() and lstat() — execute (search) permission is
required on all of the directories in path that lead to the file.
<stdio.h> #include
<sys/stat.h> #include
<sys/types.h>
if(argc!=2) return
1;
if(stat(argv[1],&fileStat) < 0)
return 1;
printf(" -\n");
printf("\n\n");
return 0;
}
OUTPUT:
student@NNRG310:~/oslab$ cc stat.c
- -
Number of Links: 1
DESCRIPTION:
#include <stdio.h>
int main(void)
DIR *d;
= opendir(".");
if (d)
printf("%s\n", dir->d_name);
closedir(d);
return(0);
}
OUTPUT:
student@NNRG310:~/oslab$ cc dirsystemcalls.c
student@NNRG310:~/oslab$ ./a.out
f1.txt
f2.txt
fcfs.c
a.out
read.c
write.c
close.c
rr.c
dirsystemcalls.c
priority.c
f4.txt
f3.txt
..
open.c
3. Write a C program to simulate Bankers Algorithm for Deadlock
Avoidance and Prevention.
ALGORITHM:
Safety Algorithm
1. Work and Finish be the vector of length m and n respectively,
Work=Available and Finish[i] =False.
2. Find an i such that both
a. Finish[i] =False
b. Need<=Work
If no such I exists go to step 4.
3. work=work+Allocation, Finish[i] =True;
4. if Finish[1]=True for all I, then the system is in safe state.
PROGRAM:
#include<stdio.h>
int main()
{
int process,resource,instance,j,i,k=0,count1=0,count2=0;
int avail[10] , max[10][10], allot[10][10],need[10][10],completed[10];
} }
printf("\n\n \t Safe Sequence is:- \t");
while(count1!=process)
{
count2=count1;
for(i=0;i<process;i++)
{
for(j=0;j<resource;j++)
{
if(need[i][j]<=avail[j])
{
k++;
}
}
if(k==resource && completed[i]==0 )
{
printf("P[%d]\t",i);
completed[i]=1;
for(j=0;j<resource;j++)
{
avail[j]=avail[j]+allot[i][j];
}
count1++;
}
k=0; }
if(count1==count2)
{
printf("\t\t Stop ..After this.....Deadlock \n");
break;
}
}
return 0;
}
OUTPUT:
student@NNRG310:~/oslab$ cc rr.c
student@NNRG310:~/oslab$ ./a.out
Enter number of processes: 3 enter
process number: 1
enter burst time: 24 enter
process number: 2
enter burst time: 3
enter process number: 3
enter burst time: 3
Enter time quantum:4
P_NO B_T W_T TAT
1 24 6 30
2 3 4 7
3 3 7 10
Avg wait time is 5.666667
Avg turnaround time is 15.666667
student@NNRG310:~/oslab$
4. Write a C program to implement the Producer – Consumer problem using
semaphores using UNIX/LINUX system calls
DESCRIPTION:
buffer and the producer produces items and enters them into the buffer. The consumer
removes the items from the buffer and consumes them. A producer should not produce
items into the buffer when the consumer is consuming an item from the buffer and vice
versa. So the buffer should only be accessed by the producer or consumer at a time.
#include<stdio.h>
#include<stdlib.h>
main()
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit"); while(1)
scanf("%d",&n);
switch(n)
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit
(0);
} bre
} ak;
return 0;
int wait(int s)
return (--s);
}
int signal(int s)
return(++s);
void producer()
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
mutex=signal(mutex);
void consumer()
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
mutex=signal(mutex);
}
OUTPUT:
student@NNRG310:~/oslab$ cc pc.c
student@NNRG310:~/oslab$ ./a.out
1.Producer
2.Consumer
3.Exit
choice:2
Buffer is empty!!
Buffer is empty!!
student@NNRG310:~/oslab$
5. Write C programs to illustrate the following IPC mechanisms
a) Pipes
DESCRIPTION:
PROGRAM:
#include<stdio.h>
#include<unistd.h>
int main() {
returnstatus;
readmessage[20];
returnstatus = pipe(pipefds);
if (returnstatus == -1) {
return 1;
writemessages[0], sizeof(writemessages[0]));
read(pipefds[0], readmessage, sizeof(readmessage)); printf("Reading from
readmessage); return 0;
OUTPUT:
student@NNRG310:~/oslab$ cc pipe.c
student@NNRG310:~/oslab$ ./a.out
Message 2 is Hello
student@NNRG310:~/oslab$
b) FIFOs
DESCRIPTION:
Pipes were meant for communication between related processes. Can we use pipes
for unrelated process communication, say, we want to execute client program from
one terminal and the server program from another terminal? The answer is No. Then
how can we achieve unrelated processes communication, the simple answer is
Named Pipes. Even though this works for related processes, it gives no meaning to
use the named pipes for related process communication.
PROGRAM:
fifoclient.c
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
FILE *file1;
int fifo_server,fifo_client;
char str[256];
char *buf; int
choice=1;
printf("Choose the request to be sent to server from options below"); printf("\n\t\t
Enter 1 for O.S.Name \n \
Enter 2 for Distribution \n \ Enter
3 for Kernel version \n");
scanf("%d",&choice);
fifo_server=open("fifo_server",O_RDWR);
if(fifo_server < 0) {
printf("Error in opening file");
exit(-1);
}
write(fifo_server,&choice,sizeof(int));
fifo_client=open("fifo_client",O_RDWR);
if(fifo_client < 0) {
printf("Error in opening file");
exit(-1);
}
buf=malloc(10*sizeof(char));
read (fifo_client,buf,10*sizeof(char));
printf("\n ***Reply from server is %s***\n",buf); close(fifo_server);
close(fifo_client);
return 0;
}
fifoserver.c
PROGRAM:
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
int main( )
{
FILE *file1;
int fifo_server,fifo_client; int
choice;
char *buf;
fifo_server = open("fifo_server",O_RDWR);
if(fifo_server<1) {
printf("Error opening file");
}
read(fifo_server,&choice,sizeof(int));
sleep(10);
fifo_client = open("fifo_client",O_RDWR);
if(fifo_server<1) {
printf("Error opening file");
}
switch(choice) {
case 1:
buf="Linux";
write(fifo_client,buf,10*sizeof(char));
printf("\n Data sent to client \n"); break;
case 2:
buf="Fedora";
write(fifo_client,buf,10*sizeof(char));
printf("\nData sent to client\n"); break;
case 3:
buf="2.6.32";
write(fifo_client,buf,10*sizeof(char));
printf("\nData sent to client\n");
}
close(fifo_server);
close(fifo_client);
}
OUTPUT:
TERMINAL-I
TERMINAL-II
c) Message Queues:
DESCRIPTION:
A message queue is a linked list of messages stored within the kernel and identified
by a message queue identifier. A new queue is created or an existing queue opened
by msgget().New messages are added to the end of a queue by msgsnd(). Every
message has a positive long integer type field, a non-negative length, and the
actual data bytes (corresponding to the length), all of which are specified to
msgsnd() when the message is added to a queue. Messages are fetched from a queue
by msgrcv(). We don’t have to fetch the messages in a first-in, first-out order.
Instead, we can fetch messages based on their type field.
PROGRAM:
Sender.c
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h>
#include<stdlib.h>
#define SIZE 2000 void
main()
{
int mfd,mfd2,mfd3; struct
{
double mtype; char
mtext[2000];
}s1,s2,s3;
if((mfd=msgget(1000,IPC_CREAT|0666))==-1)
{
perror("msgget:");
exit(1);
}
s1.mtype=1;
sprintf(s1.mtext,"%s","Hi friends... My name is message1");
if(msgsnd(mfd,&s1,1000,0)==-1)
{
perror("msgsnd");
exit(1);
}
if((mfd2=msgget(1000,IPC_CREAT|0666))==-1)
{
perror("msgget:");
exit(1);
}
s2.mtype=1;
sprintf(s2.mtext,"%s","Hi friends... My name is message2");
if(msgsnd(mfd2,&s2,1000,0)==-1)
{
perror("msgsnd");
exit(1);
}
if((mfd3=msgget(1000,IPC_CREAT|0666))==-1)
{
perror("msgget:");
exit(1);
}
s3.mtype=1;
sprintf(s3.mtext,"%s","Hi friends... My name is message3");
if(msgsnd(mfd3,&s3,1000,0)==-1)
{
perror("msgsnd");
exit(1);
}
printf("Your message has been sent successfully...\n"); printf("Please visit
another (receiver's) terminal...\n"); printf("Thank you.... For using
LINUX\n");
}
Output:
student@NNRG310:~/oslab$ cc mqsender.c
student@NNRG310:~/oslab$ ./a.out
#include<stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/types.h> #define
SIZE 40
void main()
{
int mfd,mfd2,mfd3; struct
{
long mtype; char
mtext[6];
}s1,s2,s3;
if((mfd=msgget(1000,0))==-1)
{
perror("msgget");
exit(1);
}
if(msgrcv(mfd,&s1,SIZE,0,IPC_NOWAIT|MSG_NOERROR)==-1)
{
perror("msgrcv");
exit(1);
}
printf("Message from client is :%s\n",s1.mtext);
if((mfd2=msgget(1000,0))==-1)
{
perror("msgget");
exit(1);
}
if(msgrcv(mfd2,&s2,SIZE,0,IPC_NOWAIT|MSG_NOERROR)==-1)
{
perror("msgrcv");
exit(1);
}
printf("Message from client is :%s\n",s2.mtext);
if((mfd3=msgget(1000,0))==-1)
{
perror("msgget");
exit(1);
}
if(msgrcv(mfd3,&s3,SIZE,0,IPC_NOWAIT|MSG_NOERROR)==-1)
{
perror("msgrcv");
exit(1);
}
printf("Message from sender is :%s\n",s3.mtext);
}
Output:
student@NNRG310:~/oslab$ cc mqclient.c
student@NNRG310:~/oslab$ ./a.out
student@NNRG310:~/oslab$
d) Shared Memory:
DESCRIPTION:
PROGRAM:
shwriter.c #include
<stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h> int
main ( )
{
int segment_id;
char bogus;
char* shared_memory; struct
shmid_ds shmbuffer; int
segment_size;
const int shared_segment_size = 0x6400;
/* Allocate a shared memory segment. */
segment_id = shmget (IPC_PRIVATE, shared_segment_size, IPC_CREAT
| IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
/* Attach the shared memory segment. */
printf("Shared memory segment ID is %d\n", segment_id);
shared_memory = (char*) shmat (segment_id, 0, 0);
printf ("shared memory attached at address %p\n", shared_memory);
/* Determine the segment's size. */
/*
shmctl (segment_id, IPC_STAT, &shmbuffer);
segment_size = shmbuffer.shm_segsz;
printf ("segment size: %d\n", segment_size);
*/
/* Write a string to the shared memory segment. */ sprintf
(shared_memory, "Hello, world.");
/* Detach the shared memory segment. */ shmdt
(shared_memory);
printf("Wrote Hello World to the segment\n");
}
PROGRAM:
shreader.c #include
<stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h> int
main ()
{
int segment_id;
char bogus;
char* shared_memory; struct
shmid_ds shmbuffer;
int segment_size;
const int shared_segment_size = 0x6400;
printf("Enter the shared memory id: "); scanf("%d",
&segment_id);
/* Reattach the shared memory segment, at a different address. */ shared_memory
= (char*) shmat (segment_id, (void*) 0x5000000, 0); printf ("shared memory
reattached at address %p\n", shared_memory);
/* Print out the string from shared memory. */
printf ("The contents of the shared memory is:\n%s\n",
shared_memory);
/* Detach the shared memory segment. */ shmdt
(shared_memory);
return 0;
}
OUTPUT:
Terminal-I
Terminal-II
6. Write C programs to simulate the following memory management techniques
a) Paging b) Segmentation
a) Pagin
PROGRAM:
#include<stdio.h>
int main()
{
int ms, ps, nop, np, rempages, i, j, x, y, pa, offset; int
s[10], fno[10][20];
OUTPUT:
student@NNRG310:~/oslab$ ./a.out
number of processes -- 3
student@NNRG310:~/oslab$
b) Segmentation
ALGORITHM:
int 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=1;i<=n;i++)
{
scanf("%d",&b[i]);
scanf("%d",&l[i]);
scanf("%d",&d);
scanf("%d",&s);
for(i=1;i<=n;i++)
if(i==s)
if(d<l[i])
{
pa=b[i]+d;
a=b[i];
exit(0);
else
exit(0);
0;
}
OUTPUT
./a.out
50
150 20
130 34
segment number:1
100 125
student@NNRG310:~/oslab$
1. Simulate the following memory allocation algorithms
a) First-
Fit
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<math.h> int
main()
{
static int block[10],process[10]; int
frags[10], b[10], p[10];
int i, j, nob, nop, temp;
printf("\nEnter the Total Number of Blocks:\t"); scanf("%d",
&nob);
printf("Enter the Total Number of process:\t"); scanf("%d",
&nop);
printf("\nEnter the Size of the Blocks:\n"); for(i =
0; i < nob; i++)
{
printf("Block size.:\t");
scanf("%d", &b[i]);
}
printf("Enter the Size of the proces:\n"); for(i
= 0; i < nop; i++)
{
printf("proces size\t" );
scanf("%d", &p[i]);
}
for(i = 0; i < nop; i++)
{
for(j = 0; j < nob; j++)
{
if(block[j] != 1)
{
temp=abs(b[j]-p[i]);
if(temp >= 0)
{
process[i] = j;
break;
}
}
}
frags[i] = temp;
block[process[i]] = 1;
}
printf("\n process Number\tBlock Number\tBlock Size\tProcess Size\tFragment");
for(i = 0; i < nop; i++)
{
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", i, process[i],
b[process[i]],p[i], frags[i]);
}
printf("\n");
return 0;
}
Output:
student@NNRG310:~/oslab$ cc firstfit.c
student@NNRG310:~/oslab$ ./a.out
Fit
PROGRAM:
#include<stdio.h>
int main()
{
int frags[20],b[20],p[20],i,j,nob,nop,temp,lowest=9999; static
int block[20],process[20];
printf("\n\t\t\tMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:\t"); scanf("%d",&nob);
printf("Enter the number of processes:\t");
scanf("%d",&nop);
printf("\nEnter the size of the blocks:-\n");
for(i=0;i<nob;i++)
{
printf("Block size:\t");
scanf("%d",&b[i]);
}
printf("\nEnter the size of the processes :-\n");
for(i=0;i<nop;i++)
{
printf("Process size:\t");
scanf("%d",&p[i]);
}
for(i=0;i<nop;i++)
{
for(j=0;j<nob;j++)
{
if(block[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(lowest>temp)
{
process[i]=j;
lowest=temp;
}
}
}
frags[i]=lowest;
block[process[i]]=1; lowest=10000;
}
printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment");
for(i=0;i<nop && process[i]!=0;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],process[i],b[process[i]],fra
gs[i]);
}
Output:
student@NNRG310:~/oslab$ ./a.out
0 212 3 300 88
1 417 1 500 83
2 112 2 200 88
student@NNRG310:~/oslab$
c) Worst
-fit
PROGRAM:
#include<stdio.h>
for(i=0;i<nop;i++)
{
for(j=0;j<nob;j++)
{
if(block[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-p[i];
if(temp>=0)
if(highest<temp)
{
process[i]=j;
highest=temp;
}
}
}
frags[i]=highest;
block[process[i]]=1;
highest=0;
}
printf("\nProcess_no:\tProcess_size
:\tBlock_no:\tBlock_size:\tFragement");
for(i=0;i<nop;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],process[i],b[process[i]],frags
[i]);
return 0;
}
Output
student@NNRG310:~/oslab$ cc wrostfit.c
student@NNRG310:~/oslab$ ./a.out
a) FCFS
PROGRAM:
#include<stdio.h>
int main()
{
int a[20],b[20],n,i,thm[20],tot=0;
float avgthm;
student@NNRG310:~/oslab$ cc dfcfs.c
student@NNRG310:~/oslab$ ./a.out Enter
head pointer position: 53
Enter number of processes: 8
Enter processes in request order 98
183 37 122 14 124 65 67
Track traversed Difference between tracks
53 98 = 45
98 183 = 85
183 37 = 146
37 122 = 85
122 14 = 108
14 124 = 110
124 65 = 59
65 67 = 2
PROGRAM:
#include<stdio.h>
int main()
{
int a[20],b[20],n,i,j,temp,p,s,m,x,t=0;
Output- 01
student@NNRG310:~/oslab$ cc dscan.c
Processing order:53->37->14->0->65->67->98->122->124->183->
student@NNRG310:~/oslab$
Output- 02
student@NNRG310:~/oslab$ cc dscan.c
Processing order:53->65->67->98->122->124->183->199->37->14->
student@NNRG310:~/oslab$
c) SSTF
PROGRAM:
#include<stdio.h>
struct di
{
int num; int
flag;
};
int main()
{
int i,j,sum=0,n,min,loc,x,y; struct di
d[20];
int disk;
int ar[20],a[20];
disk=d[loc].num;
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("\nmovement of total cylinders %d",sum);
return 0;
}
Output:
student@NNRG310:~/oslab$ cc dsstf.c
student@NNRG310:~/oslab$ ./a.out enter size
of queue 8
enter position of head 53
enter elements of disk queue: 98 183 37 122 14 124 65 67