Os Lab Manual R20
Os Lab Manual R20
List of Experiments
Part-A
1 Simulate the following CPU scheduling algorithms a) Round Robin b) Shortest Job First c) Priority
a) ROUND ROBIN:
DESCRIPTION:
· Round Robin is the preemptive process scheduling algorithm.
· Each process is provided a fix time to execute, it is called a quantum.
· Once a process is executed for a given time period, it is preempted and other process executes for a
given time period.
· Context switching is used to save states of preempted processes.
PROGRAM:
#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|TurnaroundTime|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum&& rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
return 0;
}
OUTPUT:
Enter Total Process: 4
Enter Arrival Time and Burst Time for Process ProcessNumber 1:0
9
Enter Arrival Time and Burst Time for Process ProcessNumber 2:1
5
Enter Arrival Time and Burst Time for Process ProcessNumber 3:2
3
Enter Arrival Time and Burst Time for Process ProcessNumber 4:3
4
Enter Time Quantum: 5
Process Turnaround Time Waiting Time
P[2] 9 4
P[3] 11 8
P[4] 14 10
P[1] 21 12
b) SJF:
DESCRIPTION:
· This is also known as shortest job first, or SJF
· This is a non-preemptive, pre-emptive scheduling algorithm.
· Best approach to minimize waiting time.
· Easy to implement in Batch systems where required CPU time is known in advance.
· Impossible to implement in interactive systems where required CPU time is not known.
· The processer should know in advance how much time process will take.
PROGRAM:
#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}
OUTPUT:
EX-2
Multiprogramming-Memory management- Implementation of fork (), wait (), exec() and exit(), System
calls?
FORK():
Fork system call use for creates a new process, which is called child process, which runs concurrently
with process (which process called system call fork) and this process is called parent process. After a
new child process created, both processes will execute the next instruction following the fork() system
call. A child process uses the same pc(program counter), same CPU registers, same open files which use
in the parent process.
EXEC():
The exec family of functions replaces the current running process with a new process. It can be used to
run a C program by using another C program. It comes under the header file unistd.h. There are many
members in the exec family which are shown below with examples.
· execvp : Using this command, the created child process does not have to run the same program as the
parent process does. The exec type system calls allow a process to run any program files, which include
a binary executable or a shell script .
Syntax:
file: points to the file name associated with the file being executed.
argv: is a null terminated array of character pointers.
· execv : This is very similar to execvp() function in terms of syntax as well. The syntax of execv() is as
shown below:
Syntax:
Syntax:
int execlp(const char *file, const char *arg,.../* (char *) NULL */);
int execl(const char *path, const char *arg,.../* (char *) NULL */);
· execvpe and execle : These two also serve the same purpose but the syntax of them are a bit different
from all the above members of exec family. The synatxes of both of them are shown below :
Syntax:
Syntax:
int execle(const char *path, const char *arg, .../*, (char *) NULL,
WAIT() :
A call to wait() blocks the calling process until one of its child processes exits or a signal is received. After
child process terminates, parent continues its execution after wait system call instruction.
Child process may terminate due to any of these:
· It calls exit();
· It receives a signal (from the OS or another process) whose default action is to terminate.
EXIT():
It terminates the calling process without executing the rest code which is after the exit()
function.
FORK():
PROGRAM:
/* C Program to implement Fork() system calls */
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void forkexample()
if (fork() == 0)
else
int main()
forkexample();
return 0;
OUTPUT:
PROGRAM:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
int main()
pid_t
pid;
int ret = 1;
int status;
pid = fork();
if (pid == -1)
exit(EXIT_FAILURE);
}
else if (pid == 0)
execv("ls",argv_list);
exit(0);
else{
if (WEXITSTATUS(status) == 127)
printf("execv failed\n");
else
else
else
printf("waitpid() failed\n");
exit(0);
return 0;
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
int main()
pid_tcpid;
if (fork()== 0)
exit(0);
else
cpid = wait(NULL);
return 0;
EXIT():
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("START");
exit(0);
printf("End of program");
OUTPUT:
START
EX-3
Simulate the following a) Multiprogramming with a fixed number of tasks (MFT) b) Multiprogramming
with a variable number of tasks (MVT)
DESCRIPTION:
MFT : Multiprogramming with a Fixed number of Tasks is one of the old memory management
techniques in which the memory is partitioned into fixed size partitions and each job is assigned to a
partition. The memory assigned to a partition does not change.
MVT : Multiprogramming with a Variable number of Tasks is the memory management technique in
which each job gets just the amount of memory it needs. That is, the partitioning of memory is dynamic
and changes as jobs enter and leave the system. MVT is a more ``efficient'' user of resources. MFT
suffers with the problem of internal fragmentation and MVT suffers with external fragmentation.
PROGRAM
#include<stdio.h>
#include<conio.h>
main()
{
int ms, bs, nob, ef,n, mp[10],tif=0;
int i,p=0;
clrscr();
printf("Enter the total memory available (in Bytes) -- ");
scanf("%d",&ms);
printf("Enter the block size (in Bytes) -- ");
scanf("%d", &bs);
nob=ms/bs;
ef=ms - nob*bs;
printf("\nEnter the number of processes -- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter memory required for process %d (in Bytes)-- ",i+1);
scanf("%d",&mp[i]);
}
printf("\nNo. of Blocks available in memory -- %d",nob);
printf("\n\nPROCESS\tMEMORY REQUIRED\t ALLOCATED\tINTERNAL FRAGMENTATION");
for(i=0;i<n && p<nob;i++)
{
printf("\n %d\t\t%d",i+1,mp[i]);
if(mp[i] > bs)
printf("\t\tNO\t\t---");
else
{
printf("\t\tYES\t%d",bs-mp[i]);
tif = tif + bs-mp[i];
p++;
}
}
if(i<n)
printf("\nMemory is Full, Remaining Processes cannot be accomodated");
printf("\n\nTotal Internal Fragmentation is %d",tif);
printf("\nTotal External Fragmentation is %d",ef);
getch();
}
OUTPUT:
MVT :
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
int ms,mp[10],i, temp,n=0;
char ch = 'y';
clrscr();
printf("\nEnter the total memory available (in Bytes)-- ");
scanf("%d",&ms);
temp=ms;
for(i=0;ch=='y';i++,n++)
{
printf("\nEnter memory required for process %d (in Bytes) -- ",i+1);
scanf("%d",&mp[i]);
if(mp[i]<=temp)
{
printf("\nMemory is allocated for Process %d ",i+1);
temp = temp - mp[i];
}
else
{
printf("\nMemory is Full");
break;
}
printf("\nDo you want to continue(y/n) -- ");
scanf(" %c", &ch);
}
printf("\n\nTotal Memory Available -- %d", ms);
printf("\n\n\tPROCESS\t\t MEMORY ALLOCATED ");
for(i=0;i<n;i++)
printf("\n \t%d\t\t%d",i+1,mp[i]);
printf("\n\nTotal Memory Allocated is %d",ms-temp);
printf("\nTotal External Fragmentation is %d",temp);
getch();
}
OUTPUT
PROGRAM:
#include <stdio.h>
#include <stdlib.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;
count = 0;
scanf("%d", &p);
do
{
printf("\n Max matrix:\tAllocation matrix:\n");
process = -1;
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);
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
OUTPUT:
For process 1 : 7
5
3
For process 2 : 3
For process 3 : 7
For process 4 : 2
For process 5 : 4
For process 1 : 0
For process 2 : 2
For process 3 : 3
For process 4 : 2
1
1
For process 5 : 0
753 010
322 200
702 302
222 211
433 002
753 010
000 000
702 302
222 211
433 002
000 000
000 000
222 211
433 002
753 010
000 000
000 000
000 000
433 002
000 000
000 000
000 000
000 000
433 002
SimulateBankersAlgorithmforDeadLockPrevention.
DESCRIPTION: We can prevent Deadlock by eliminating any of the above four condition.
· Eliminate Mutual Exclusion : It is not possible to dis-satisfy the mutual exclusion because some
resources, such as the tap drive and printer, are inherently non-shareable.
· Eliminate Hold and wait: Allocate all required resources to the process before start of its execution,
this way hold and wait condition is eliminated but it will lead to low device utilization.
· Eliminate No Preemption: Preempt resources from process when resources required by other high
priority process.
· Eliminate Circular Wait: Each resource will be assigned with a numerical number. A process can
request for the resources only in increasing order of numbering.
PROGRAM:
#include<stdio.h>
void main()
int max[10][10],a1[10][10],av[10],i,j,k,m,n,ne[10][10],flag=0;
scanf("%d%d",&m,&n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&max[i][j]);
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a1[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
ne[i][j]=max[i][j]-a1[i][j];
printf("\t%d",ne[i][j]);
printf("\n");
for(i=0;i<n;i++)
scanf("%d",&av[i]);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",max[i][j]);
printf("\n");
EX-6
Simulate the following page replacement algorithms. a) First In First Out b) Least Recently Used
a) FIFO :
This is the simplest page replacement algorithm. In this algorithm, operating system keeps track
of all pages in the memory in a queue, oldest page is in the front of the queue. When a page needs to be
replaced page in the front of the queue is selected for removal.
PROGRAM:
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
OUTPUT:
Page Fault Is 15
b)LRU:
On a page fault, the frame that was least recently used in replaced.
PROGRAM:
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}
OUTPUT:
Enter no of pages:10
Enter the reference string:7 5 9 4 3 7 9 6 2 1
Enter no of frames:3
7
7 5
7 5 9
4 5 9
4 3 9
4 3 7
9 3 7
9 6 7
9 6 2
1 6 2
EX-7
Simulate the following File allocation strategies a) Sequenced b) Indexed
Hege
AIM: Write a C Program to implement Sequential File Allocation method.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of memory partition and their sizes.
Step 3: Get the number of processes and values of block size for each process.
Step 4: First fit algorithm searches all the entire memory block until a hole which is big enough is
encountered. It allocates that memory block for the requesting process.
Step 5: Best-fit algorithm searches the memory blocks for the smallest hole which can be allocated to
requesting process and allocates if.
Step 6: Worst fit algorithm searches the memory blocks for the largest hole and allocates it to the
process.
Step 7: Analyses all the three memory management techniques and display the best algorithm which
utilizes the memory resources effectively and efficiently.
Step 8: Stop the program.
INDEXED FILE ALLOCATION
AIM: Write a C Program to implement Indexed File Allocation method.
Algorithm:
Step 1: Start.
Step 2: Let n be the size of the buffer
Step 3: check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer
Step 6: If the buffer is full the producer has to wait
Step 7: Check there is any cosumer.If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer
Step 9: If the buffer is empty, the consumer has to wait.
Step 10: Repeat checking for the producer and consumer till required
Step 11: Terminate the process.