Operating Systems R18-Lab Manual
Operating Systems R18-Lab Manual
AND
TECHNOLOGICAL INSTITUTE
Aushapur, Ghatkesar, Medchal-501301
Designation : ________________________________
Department : ________________________________
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
semaphores using UNIX/LINUX system calls 38
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
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
Course Objectives:
Course Outcomes:
i
Operating systems
ALGORITHM DESCRIPTION:
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
#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
student@NNRG310:~/oslab$ ./a.out
enter no of processes: 3
P3 enter
burst time 3
P1 24 0 24
P2 3 24 27
P3 3 27 30
student@NNRG310:~/oslab$
ALGORITHM DESCRIPTION:
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:
student@NNRG310:~/oslab$ cc sjf.c
student@NNRG310:~/oslab$ ./a.out
enter no of processes: 4
4 3 0 3
1 6 3 9
3 7 9 16
2 8 16 24
student@NNRG310:~/oslab$
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
#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];
}
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++)
{
wt[i]=tat[i]-bt[i];
avgwt=avgwt+wt[i];
avgtat=avgtat+tat[i];
}
printf("P_NO\t B_T\t W_T\t TAT\n");
for(i=0;i<n;i++)
printf("%d\t %d\t %d\t %d\t\n",i+1,bt[i],wt[i],tat[i]);
printf("Avg wait time is %f\n Avg turn around time is
%f\n",avgwt/n,avgtat/n);
}
OUTPUT:
student@NNRG310:~/oslab$ cc rr.c
student@NNRG310:~/oslab$ ./a.out
1 24 6 30
2 3 4 7
3 3 7 10
student@NNRG310:~/oslab$
ALGORITHM DESCRIPTION:
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:
student@NNRG310:~/oslab$ cc priority.c
student@NNRG310:~/oslab$ ./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
student@NNRG310:~/oslab$
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.
b) read ( ) system
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;
OUTPUT:
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()
if (fd1==-1)
perror("c1");
exit(1);
if (close(fd1)==-1)
perror("c1");
exit(1);
return 0;
OUTPUT:
student@NNRG310:~/oslab$ ./a.out
opened the fd = 3
e) fcntl ( ) system
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
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int fd;
printf ("locking\n");
lock.l_type = F_WRLCK;
getchar ();
printf ("unlocking\n");
lock.l_type = F_UNLCK;
&lock);
close (fd);
return 0;
OUTPUT:
Terminal-1
student@NNRG310:~/oslab$ cc lock.c
student@NNRG310:~/oslab$ ./a.out f4.txt
opening f4.txt
locking
locked; hit Enter to unlock...
unlocking
student@NNRG310:~/oslab$
Terminal-2
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>
#include <fcntl.h>
#include <sys/types.h>
#include<stdio.h>
int main()
int file=0;
return 1;
char buffer[19];
printf("%s\n",buffer);
printf("%s\n",buffer);
return 0;
OUTPUT:
Cat> f4.txt
student@NNRG310:~/oslab$ cc
lseek.c
student@NNRG310:~/oslab$ ./a.out
lseek is a system c
student@NNRG310:~/oslab$
DESCRIPTION:
#include <unistd.h>
#include <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;
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
sjf.c
a.out
.~lock.system calls
programs.docx# read.c
write.c
close.c
rr.c
dirsystemcalls.c
priority.c
f4.txt
f3.txt
..
open.c
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];
scanf("%d",&instance);
avail[i]=instance;
}
printf("\n\tEnter Maximum No. of instances of resources that a
Process need:\n");
for(i=0;i<process;i++)
{
printf("\n\t For P[%d]",i);
for(j=0;j<resource;j++)
{
printf("\t");
scanf("%d",&instance);
max[i][j]=instance;
}
}
printf("\n\t Enter no. of instances already allocated to process of a resource:\
n");
for(i=0;i<process;i++)
{
printf("\n\t For P[%d]\t",i);
for(j=0;j<resource;j++)
{
scanf("%d",&instance); allot[i]
[j]=instance;
need[i][j]=max[i][j]-allot[i][j]; //calculating Need of each
process
} }
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:
DESCRIPTION:
fixed size 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
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
int n;
void producer();
void consumer();
int wait(int);
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);
break;
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);
x--;
mutex=signal(mutex);
OUTPUT:
student@NNRG310:~/oslab$ cc
pc.c student@NNRG310:~/oslab$
./a.out 1.Producer
2.Consumer
3.Exit
Buffer is empty!!
Buffer is empty!!
student@NNRG310:~/oslab$
a) Pipes
DESCRIPTION:
PROGRAM:
#include<stdio.h>
#include<unistd.h>
int main() {
int pipefds[2];
int returnstatus;
char readmessage[20];
returnstatus = pipe(pipefds);
if (returnstatus == -1) {
n"); return 1;
return 0;
OUTPUT:
student@NNRG310:~/oslab$ cc pipe.c
student@NNRG310:~/oslab$ ./a.out
student@NNRG310:~/oslab$
b) FIFOs
DESCRIPTION:
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");
Department of Computer Science and Engineering Page 50
Operating systems
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));
OUTPUT:
TERMINAL-I
student@NNRG310:~/oslab$ cc fifoclient.c
student@NNRG310:~/oslab$ ./a.out
version 1
TERMINAL-II
student@NNRG310:~/oslab$ cc fifoserver.c
student@NNRG310:~/oslab$ ./a.out
Data sent to client
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
student@NNRG310:~/oslab$
Receiver.c
#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;
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
student@NNRG310:~/oslab$ cc shwriter.c
student@NNRG310:~/oslab$ ./a.out
Shared memory segment ID is 3047442
shared memory attached at address 0xb7f2a000
Wrote Hello World to the segment
student@NNRG310:~/oslab$
Terminal-II
student@NNRG310:~/oslab$ cc shreader.c
student@NNRG310:~/oslab$ ./a.out
Enter the shared memory id: 3047442
shared memory reattached at address 0x5000000
The contents of the shared memory is:
Hello, world.
student@NNRG310:~/oslab$
a) Paging
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
Memory is Full
student@NNRG310:~/oslab$
b) Segmentation
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
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);
printf("\nInvalid segment");
return 0;
OUTPUT
student@NNRG310:~/oslab$ cc seg123.c
student@NNRG310:~/oslab$ ./a.out
100 50
150 20
130 34
PageNo. BaseAdd.
student@NNRG310:~/oslab$
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++)
{
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
b) Best-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>
#define max 25
int main()
{
int frags[20],b[20],p[20],i,j,nob,nop,temp,highest=0;
static int block[20],process[20];
printf("\n\tMemory Management Scheme - Worst Fit");
printf("\nEnter the number of blocks:");
scanf("%d",&nob);
printf("Enter the number of files:");
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 processes :-\n");
for(i=0;i<nop;i++)
{
printf("File %d:\t",i);
scanf("%d",&p[i]);
}
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;
b[i]=thm[i];
tot=tot+thm[i];
}
avgthm=(float)tot/n;
printf("\n\tTrack traversed \t Difference between tracks \n");
for(i=0;i<n;i++)
printf("\t%d\t%d\t=\t\t%d\n",a[i],a[i+1],b[i]);
printf("\nTotal heam movents\t=%d \n Average Head
Movement\t=:%f",tot,avgthm);
return 0;
}
Output:
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
b) SCAN
PROGRAM:
#include<stdio.h>
int main()
{
int a[20],b[20],n,i,j,temp,p,s,m,x,t=0;
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=1;i<=n+1;i++)
{
if(s==a[i])
x=i;
}
j=0;
if(s<p)
{
for(i=x;i>0;i--)
{
t+=(a[i]-a[i-1]);
b[j++]=a[i];
}
t+=a[x+1]-a[0];
b[j++]=a[0];
for(i=x+1;i<n+1;i++)
{
t+=(a[i+1]-a[i]);
b[j++]=a[i];
}
b[j++]=a[i];
}
else
{
for(i=x;i<n+2;i++)
{
t+=(a[i+1]-a[i]);
b[j++]=a[i];
}
t+=a[n+2]-a[x-1];
b[j++]=a[n+2];
for(i=x-1;i>1;i--)
{
t+=(a[i]-a[i-1]);
b[j++]=a[i];
}
b[j++]=a[i];
}
printf("\nProcessing order:");
for(i=0;i<=n+1;i++)
printf("%d->",b[i]);
printf("\n\nTotal Head Movement:%d",t);
return 0;
}
Output- 01
student@NNRG310:~/oslab$ cc dscan.c
student@NNRG310:~/oslab$ ./a.out
67
Processing order:53->37->14->0->65->67->98->122->124->183->
student@NNRG310:~/oslab$
Output- 02
student@NNRG310:~/oslab$ cc dscan.c
student@NNRG310:~/oslab$ ./a.out
67
Processing order:53->65->67->98->122->124->183->199->37->14->
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];
if(x==0)
{
ar[j]=disk-d[j].num;
if(ar[j]<0){ ar[j]=d[j].num-disk;}
min=ar[j];loc=j;x++; }
else
{
ar[j]=disk-d[j].num;
if(ar[j]<0){ ar[j]=d[j].num-disk;}
}
if(min>ar[j]){ min=ar[j]; loc=j;}
}
}
d[loc].flag=1;
a[i]=d[loc].num-disk;
if(a[i]<0){a[i]=disk-d[loc].num;}
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