Os Lab
Os Lab
Estd 2001
OPERATING SYSTEM
SEMISTER: III–II
BACHELOR OF TECHNOLOGY
IN
COMPUTER SCIENCE ENGINEERING
Prepared By
Mr. Durga Prasad
Assistant Professor, CSE
AVANTHI’S ST. THERESSA INSTITUTE OF ENGG. & TECH
GARIVIDI
Vizianagaram Dist (AP)
Estd 2001
CERTIFICATE
This is to certify that, it is the bonafide record of the work done in ……………….......
………………................................course during………………….……………………….
2
III-II CSE(AIML) OPERATING SYSTEM
EXPERIMENT NO.1
CPU SCHEDULINGALGORITHMS
AIM: To write a c program to simulate the CPU scheduling algorithm First Come First
Serve (FCFS)
DESCRIPTION:
To calculate the average waiting time using the FCFS algorithm first the
waiting time of the first process is kept zero and the waiting time of the second
process is the burst time of the first process and the waiting time of the third process
is the sum of the burst times of the first and the second process and so on. After
calculating all the waiting times the average waiting time is calculated as the average
of all the waiting times. FCFS mainly says first come first serve the algorithm which
came first will be served first.
ALGORITHM:
SOURCE CODE:
#include<stdio.h>
#include<conio.h> main()
{ int bt[20], wt[20],
tat[20], i, n;
float wtavg, tatavg;
clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n); for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
wt[0] = wtavg = 0;
tat[0] = tatavg =
bt[0];
for(i=1;i<n;i++)
{ wt[i] = wt[i-1]
+bt[i-1]; tat[i] =
tat[i-1] +bt[i]; wtavg
= wtavg + wt[i];
tatavg = tatavg +
tat[i];
}
printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
getch();
}
INPUT
Enter the number of processes -- 3
Enter Burst Time for Process 0 -- 24
Enter Burst Time for Process 1 -- 3
Enter Burst Time for Process 2 -- 3
OUTPUT
PROCESS BURST TIME WAITING TIME TURNAROUND
TIME
P0 24 0 24
P1 3 24 27
P2 3 27 30
Average Waiting Time-- 17.000000
Average Turnaround Time -- 27.000000
AIM: To write a program to stimulate the CPU scheduling algorithm Shortest job first
(Non- Preemption)
DESCRIPTION:
To calculate the average waiting time in the shortest job first algorithm the
sorting of the process based on their burst time in ascending order then calculate the
waiting time of each process as the sum of the bursting times of all the process
previous or before to that process.
ALGORITHM:
#include<stdi
o.h>
#include<con
io.h> main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp; float
wtavg, tatavg; clrscr();
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
p[i
]=
i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0]; for(i=1;i<n;i++)
{
wt[i] = wt[i-1]
+bt[i-1]; tat[i] =
tat[i-1] +bt[i];
wtavg = wtavg +
wt[i]; tatavg =
tatavg + tat[i];
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n); getch();}
INPUT
Enter the number of processes -- 4
Enter Burst Time for Process 0 -- 6
Enter Burst Time for Process 1 -- 8
Enter Burst Time for Process 2 -- 7
Enter Burst Time for Process 3 -- 3
OUTPUT
PROCESS BURST WAITING TURNARO
TIME TIME UND TIME
P3 3 0 3
P0 6 3 9
P2 7 9 16
P1 8 16 24
Average Waiting Time -- 7.000000
Average Turnaround Time -- 13.000000
DESCRIPTION:
To aim is to calculate the average waiting time. There will be a time slice, each
process should be executed within that time-slice and if not it will go to the
waiting state so first check whether the burst time is less than the time-slice. If it is
less than it assign the waiting time to the sum of the total times. If it is greater than
the burst-time then subtract the time slot from the actual burst time and increment it
by time-slot and the loop continues until all the processes are completed.
ALGORITHM:
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue and time quantum (or) time
slice
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time
Step 4: Calculate the no. of time slices for each process where No. of time
slice for process (n) = burst time process (n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Consider the ready queue is a circular Q, calculate
a) Waiting time for process (n) = waiting time of process(n-1)+ burst time of process(n-1 )
+ the time difference in getting the CPU from process(n-1)
b) Turnaround time for process(n) = waiting time of process(n) + burst time of
process(n)+ the time difference in getting CPU from process(n).
Step 7: Calculate
c) Average waiting time = Total waiting Time / Number of process
d) Average Turnaround time = Total Turnaround Time / Number of process Step 8: Stop
the process
SOURCE CODE
#include<stdi
o.h> main(){
int
i,j,n,bu[10],wa[10],tat[10],t,ct[10],m
ax; float awt=0,att=0,temp=0;clrscr();
printf("Enter the no of processes --
"); scanf("%d",&n);for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for process %d -- ", i+1);
scanf("%d",&bu[i]);
ct[i]=bu[i];
}
printf("\nEnter the size of time slice -- ");
scanf("%d",&t);ma
x=bu[0];
for(i=1;i<n;i++)
INPUT:
OUTPUT:
PROCESS BURST TIME WAITING TIME TURNAROUNDTIME
1 24 6 30
2 3 4 7
3 3 7 10
The Average Turnaround time is – 15.666667 The
Average Waiting time is ------------ 5.666667
DESCRIPTION:
To calculate the average waiting time in the priority algorithm, sort the burst
times according to their priorities and then calculate the average waiting time of the
processes. The waiting time of each process is obtained by summing up the burst
times of all the previous processes.
ALGORITHM:
INPUT 3 TURNARO
Enter the number of processes -- 5 1 UND TIME
Enter the Burst Time & Priority of Process 0 --- 10 4 1
Enter the Burst Time & Priority of Process 1 --- 1 5 6
Enter the Burst Time & Priority of Process 2 --- 2 2 16
Enter the Burst Time & Priority of Process 3 --- 1 18
Enter the Burst Time & Priority of Process 4 --- 5 19
WAITIN
Experiment – 2
Implementation of fork(), wait(), exec() and exit() System Calls
To write a C program to demonstrate the working of the system calls fork system call, wait system call, exec
system call, and exit system call in a Unix/Linux operating system.
Description
fork()
wait()
• The parent process waits for the child process to finish execution.
exec()
exit()
These system calls help the OS manage process creation, execution, synchronization, and termination.
Program
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
intmain()
{
pid_tpid;
if(pid<0)
{
printf("Fork failed\n");
exit(1);
}
elseif(pid==0)
{
// Child process
printf("Child Process\n");
printf("Child PID: %d\n", getpid());
exit(0);
}
else
{
// Parent process
printf("Parent Process\n");
printf("Parent PID: %d\n", getpid());
exit(0);
}
return0;
}
Compilation
gcc fork_program.c
./[Link]
Sample Output
Parent Process
Parent PID: 2456
Child Process
Child PID: 2457
Executing 'ls' command using exec()
[Link]
fork_program.c
Experiment – 3
Simulation of Multiprogramming with Fixed Tasks (MFT) and Variable Tasks (MVT)
Aim
Description
Multiprogramming allows multiple processes to reside in the main memory simultaneously so that CPU
utilization increases and system efficiency improves.
In this method, the memory is divided into fixed size partitions. Each partition can hold only one process. If
the process size is smaller than the partition size, internal fragmentation occurs.
In this method, memory partitions are created dynamically depending on the size of the processes. Memory
is allocated according to the requirement of the process, reducing internal fragmentation but possibly
causing external fragmentation.
Program
#include<stdio.h>
intmain()
{
intms, ps, nop, np, rem, i;
printf("\n------MFT------\n");
np=ms/ps;
if(nop>np)
{
printf("Memory is Full\n");
}
else
{
rem=ms- (nop*ps);
printf("Memory allocated for %d processes\n",nop);
printf("Remaining memory: %d\n",rem);
}
printf("\n------MVT------\n");
intprocess[10], total=0;
for(i=0;i<nop;i++)
{
printf("Enter memory required for process %d: ",i+1);
scanf("%d",&process[i]);
total=total+process[i];
}
if(total<=ms)
{
printf("Memory allocated for all processes\n");
printf("Remaining memory: %d\n", ms-total);
}
else
{
printf("Memory is not sufficient for all processes\n");
}
return0;
}
Sample Output
------MFT------
Enter total memory size: 1000
Enter block size: 200
Number of partitions available: 5
Enter number of processes: 4
Memory allocated for 4 processes
Remaining memory: 200
------MVT------
Enter number of processes: 3
Enter memory required for process 1: 200
Enter memory required for process 2: 300
Enter memory required for process 3: 100
Memory allocated for all processes
Remaining memory: 400
Experiment – 4
Simulation of Banker’s Algorithm for Deadlock Avoidance
Aim
To write a C program to simulate Banker's Algorithm for Deadlock Avoidance in an operating system.
Deadlock is a situation in which a set of processes are blocked because each process is holding a resource
and waiting for another resource held by another process.
Banker’s Algorithm is a deadlock avoidance algorithm used by operating systems to ensure that the
system will always remain in a safe state.
The algorithm works by checking whether allocating requested resources to a process will lead the system to
an unsafe state.
Program
#include<stdio.h>
intmain()
{
intn, m, i, j, k;
intallocation[10][10], max[10][10], need[10][10];
intavailable[10], finish[10], safeSequence[10];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
need[i][j] =max[i][j] -allocation[i][j];
intcount=0;
while(count<n)
{
for(i=0;i<n;i++)
{
if(finish[i] ==0)
{
intflag=0;
for(j=0;j<m;j++)
{
if(need[i][j] >available[j])
{
flag=1;
break;
}
}
if(flag==0)
{
safeSequence[count++] =i;
for(k=0;k<m;k++)
available[k] +=allocation[i][k];
finish[i] =1;
}
}
}
}
for(i=0;i<n;i++)
printf("P%d ", safeSequence[i]);
return0;
}
Output
Enter number of processes: 5
Enter number of resources: 3
Experiment – 5
Simulation of Deadlock Detection Algorithm
Aim
To write a C program to simulate Deadlock Detection Algorithm for detecting deadlocks in an operating
system.
Description
A deadlock is a situation where a group of processes are blocked because each process is holding a resource
and waiting for another resource held by another process.
Program
#include<stdio.h>
intmain()
{
intallocation[10][10], request[10][10], available[10];
intfinish[10];
inti,j,k,n,m,flag;
for(i=0;i<n;i++)
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
flag=0;
for(j=0;j<m;j++)
{
if(request[i][j] >available[j])
{
flag=1;
break;
}
}
if(flag==0)
{
for(j=0;j<m;j++)
available[j]+=allocation[i][j];
finish[i]=1;
}
}
}
}
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
printf("Deadlock detected at process P%d\n",i);
flag=1;
}
}
if(flag==0)
printf("No Deadlock Detected\n");
return0;
}
Sample Output
Enter number of processes: 3
Enter number of resources: 3
No Deadlock Detected
Experiment – 6
Implementation of Page Replacement Algorithms
Description
FIFO replaces the page that has been in memory for the longest time. When a page fault occurs and memory
is full, the oldest page is replaced.
Program
#include<stdio.h>
intmain()
{
intpages[30], frame[10];
intn, f, i, j, k=0, fault=0, found;
for(i=0;i<f;i++)
frame[i]=-1;
for(i=0;i<n;i++)
{
found=0;
for(j=0;j<f;j++)
{
if(frame[j]==pages[i])
{
found=1;
break;
}
}
if(found==0)
{
frame[k]=pages[i];
k=(k+1)%f;
fault++;
}
return0;
}
Output
Enter number of pages: 12
Enter page reference string:
123412512345
Enter number of frames: 3
Total Page Faults = 9
Result
Thus the program for FIFO Page Replacement Algorithm was executed successfully.
Description
LRU replaces the page that has not been used for the longest period of time.
Program
#include<stdio.h>
intmain()
{
intpages[30], frame[10], time[10];
intn, f, i, j, pos, fault=0, counter=0;
for(i=0;i<f;i++)
frame[i]=-1;
for(i=0;i<n;i++)
{
intfound=0;
for(j=0;j<f;j++)
{
if(frame[j]==pages[i])
{
counter++;
time[j]=counter;
found=1;
break;
}
}
if(found==0)
{
intmin=time[0];
pos=0;
for(j=1;j<f;j++)
{
if(time[j]<min)
{
frame[pos]=pages[i];
counter++;
time[pos]=counter;
fault++;
}
}
return0;
}
Output
Enter number of pages: 12
Enter page reference string:
123412512345
Enter number of frames: 3
Total Page Faults = 8
Result
Thus the program for LRU Page Replacement Algorithm was executed successfully.
Description
Optimal page replacement replaces the page that will not be used for the longest time in the future.
Program
#include<stdio.h>
intmain()
{
intpages[30], frame[10];
intn, f, i, j, k, pos, fault=0;
for(i=0;i<f;i++)
frame[i]=-1;
for(i=0;i<n;i++)
{
intfound=0;
for(j=0;j<f;j++)
{
if(frame[j]==pages[i])
{
found=1;
break;
}
}
if(found==0)
{
intfarthest=-1;
pos=-1;
for(j=0;j<f;j++)
{
for(k=i+1;k<n;k++)
{
if(frame[j]==pages[k])
break;
if(k>farthest)
{
farthest=k;
pos=j;
}
}
frame[pos]=pages[i];
fault++;
}
}
return0;
}
Output
Enter number of pages: 12
Enter page reference string:
123412512345
Enter number of frames: 3
Total Page Faults = 7
Result
Thus the program for Optimal Page Replacement Algorithm was executed successfully.
Description
Program
#include<stdio.h>
intmain()
{
intstart, length, i;
for(i=0;i<length;i++)
{
printf("%d ", start+i);
}
return0;
}
Output
Enter starting block: 10
Enter length of file: 5
Allocated blocks are:
10 11 12 13 14
Result
Thus the program for Sequential File Allocation was implemented and executed successfully.
Description
In linked file allocation, each file block contains a pointer to the next block.
The blocks may be scattered anywhere in the disk. This method eliminates external fragmentation but
access becomes slower.
Program
#include<stdio.h>
intmain()
{
intn,i,block[10];
printf("Linked Allocation:\n");
for(i=0;i<n;i++)
{
if(i<n-1)
printf("%d -> ",block[i]);
else
printf("%d",block[i]);
}
return0;
}
Output
Enter number of blocks: 4
Enter block numbers:
7 12 20 25
Linked Allocation:
7 -> 12 -> 20 -> 25
Result
Thus the program for Linked File Allocation was implemented and executed successfully.
Description
In indexed file allocation, each file has an index block which contains the addresses of all disk blocks of the
file. This allows direct access to any block of the file.
Program
#include<stdio.h>
intmain()
{
intindex, n, i, block[10];
for(i=0;i<n;i++)
scanf("%d",&block[i]);
printf("Blocks allocated:\n");
for(i=0;i<n;i++)
printf("%d ", block[i]);
return0;
}
Output
Enter index block number: 5
Enter number of blocks: 4
Enter block numbers:
11 14 18 21
Index Block: 5
Blocks allocated:
11 14 18 21
Aim
Description
Semaphores are used to control access to the shared buffer and avoid race conditions.
Program
#include<stdio.h>
#include<stdlib.h>
intmutex=1;
intfull=0;
intempty=3;
intx=0;
voidproducer()
{
mutex--;
full++;
empty--;
x++;
printf("Producer produces item %d\n",x);
mutex++;
}
voidconsumer()
{
mutex--;
full--;
empty++;
printf("Consumer consumes item %d\n",x);
x--;
mutex++;
intmain()
{
intn;
while(1)
{
printf("\[Link] [Link] [Link]\n");
printf("Enter your choice: ");
scanf("%d",&n);
switch(n)
{
case1:
if((mutex==1) && (empty!=0))
producer();
else
printf("Buffer is Full\n");
break;
case2:
if((mutex==1) && (full!=0))
consumer();
else
printf("Buffer is Empty\n");
break;
case3:
exit(0);
}
}
}
Output
[Link] [Link] [Link]
Enter your choice: 1
Producer produces item 1
Aim
To write a C program to create a thread using the POSIX Threads and execute a function using Thread.
Description
A thread is the smallest unit of execution within a process. Multiple threads within a process can run
concurrently and share the same memory space.
The Pthreads (POSIX Threads) library provides functions for creating and managing threads in C
programs.
Program
#include<stdio.h>
#include<pthread.h>
void*thread_function()
{
printf("Thread is running...\n");
returnNULL;
}
intmain()
{
pthread_tthread;
pthread_join(thread, NULL);
return0;
}
Output
Thread is running...
Thread execution completed
To write a C program to illustrate concurrent execution of threads using the POSIX Threads and
demonstrate the concept of Thread.
Description
A thread is a lightweight process that allows multiple tasks to execute within a single process. Threads
share the same memory space but execute independently.
Using the Pthreads library, multiple threads can run concurrently. This improves system performance and
CPU utilization.
Program
#include<stdio.h>
#include<pthread.h>
void*thread1()
{
printf("Thread 1 is running\n");
returnNULL;
}
void*thread2()
{
printf("Thread 2 is running\n");
returnNULL;
}
intmain()
{
pthread_tt1, t2;
pthread_create(&t1, NULL, thread1, NULL);
pthread_create(&t2, NULL, thread2, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return0;
}
Output
Thread 1 is running
Thread 2 is running
Both threads have finished execution