Operating Systems Lab Manual Guide
Operating Systems Lab Manual Guide
LAB MANUAL
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
a) First-Fit 63
8
b) Best-Fit 66
c) Worst-fit 69
Simulate the following memory allocation algorithms
a) FCFS 72
9
b) SCAN 74
c) SSTF 78
7
a) FIFO
b) LRU
Course Objectives:
Course Outcomes:
i
Operating systems
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:
enter no of processes: 3
P1 enter
burst time 24
P2 enter
burst time 3
P3 enter
burst time 3
P1 24 0 24
P2 3 24 27
P3 3 27 30
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:
enter no of processes: 4
4 3 0 3
1 6 3 9
3 7 9 16
2 8 16 24
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:
1 24 6 30
2 3 4 7
3 3 7 10
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:
5
enter no of processes:
enter process number: 1
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
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.
fd=3
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.
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:
f4.txt
hello linux
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:
opened the fd = 3
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;
close (fd);
return 0;
OUTPUT:
Terminal-1
f4.txt
opening f4.txt
locking
locked; hit Enter to unlock...
unlocking
student@NNRG310:~/oslab$
Terminal-2
f4.txt
opening f4.txt
locking
locked; hit Enter to unlock...
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);
n",buffer);
return 0;
OUTPUT:
lseek is a system c
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:
- -
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:
f1.txt
f2.txt
fcfs.c
sjf.c
a.out
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",&in
stance); allot[i]
[j]=instance;
proce need[i][j]=max[i][j]-allot[i][j]; //calculating Need of each
ss
} }
printf("\n\n \t Safe Sequence is:- \t");
while(count1!=process)
{
count2=count1;
if(count1==count2)
{
printf("\t\t Stop ..After this.....Deadlock \n");
break;
}
}
return 0;
}
OUTPUT:
DESCRIPTION:
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 buffer should
#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);
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:
} it
} (
);
;
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:
1.Producer
2.Consumer
3.Exit
Buffer is empty!!
Buffer is empty!!
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) {
return 1;
return 0;
OUTPUT:
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");
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
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:
#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:
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
Enter the shared memory id: 3047442
shared memory reattached at address 0x5000000
The contents of the shared memory is:
Hello, world.
6. Write C programs to simulate the following memory management
techniques
a) Paging b) Segmentation
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:
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);
printf("\nInvalid segment");
return 0;
}
OUTPUT
100 50
150 20
130 34
1 100 125
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:
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:
0 212 3 300 88
1 417 1 500 83
2 112 2 200 88
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
a) FCFS
PROGRAM:
#include<stdio.h>
int main()
{
int a[20],b[20],n,i,thm[20],tot=0;
float avgthm;
PROGRAM:
#include<stdio.h>
int main()
{
int a[20],b[20],n,i,j,temp,p,s,m,x,t=0;
Output- 01
Output- 02
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:
a)FIFO b) LRU
a) FIFO Program:
#include<stdio.h>
int main()
{
int incomingStream[] = {4, 1, 2, 4, 5};
int pageFaults = 0;
int frames = 3;
int m, n, s, pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
printf("\n");
printf("%d\t\t\t",incomingStream[m]);
for(n = 0; n < frames; n++)
{
if(temp[n] != -1)
printf(" %d\t\t\t", temp[n]);
else
printf(" - \t\t\t");
}
}
#include<stdio.h>
#include<limits.h>
return 0;
}
int main()
{
int n = sizeof(incomingStream)/sizeof(incomingStream[0]);
int frames = 3;
int queue[n];
int distance[n];
int occupied = 0;
int pagefault = 0;
printFrame(queue, occupied);
}
else{
if(queue[j] == incomingStream[k])
break;
}
// find frame item with max distance for LRU
// also notes the index of frame item in queue
// which appears furthest(max distance)
if(distance[j] > max){
max = distance[j];
index = j;
}
}
queue[index] = incomingStream[i];
printFrame(queue, occupied);
pagefault++;
}
printf("\n");
}
return 0;
}
Output:
Page Fault: 8