Os Practical File
Os Practical File
PRACTICAL REPORT
Roll no:-
List of Experiments
Exp. Experiment Name Remark
No.
1 a) Write Basic Commands for VI-Editor used in Linux.
b) Write a basic program on VI Editor in Linux.
2 Write a program to show the process ID and parent process ID of a process.
3 d) Write a program to create a child process using fork () system call.
e) Write a program to create child process and verify the child’s parent ID and
parent process ID.
4 Write a program to show the orphan process concept.
main ()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( n%2 == 0 )
printf("Even\n");
else
printf("Odd\n");
return 0;
}
LAB -2
Objective: Write a program to show the process ID and parent process ID of a process.
#include <stdio.h>
#include <sys/types.h>
main ()
{
int pid;
pid=getpid();
ppid=getppid();
printf("id of the process is=%d\n", getpid());
main ()
{
int pid;
pid=fork();
printf("id of the process is=%d\n", getpid());
printf(“hello”);
}
LAB -3.2
Objective: Write a program to create child process and verify the child’s parent ID and parent
process ID.
#include <stdio.h>
#include <sys/types.h>
main ()
{
int pid;
pid=fork();
if(pid==0)
{
printf("id of the child process is=%d\n", getpid());
A process whose parent process no more exists i.e. either finished or terminated without waiting for
its child process to terminate is called an orphan process.
a child process whose parent process terminates before it does becomes an orphan process. Such
situations are typically handled with a special "root" (or "init") process, which is assigned as the new
parent of a process when its parent process exits. This special process detects when an orphan process
terminates and then retrieves its exit status, allowing the system to deallocate the terminated child
process.
Parent finishes execution and exits while the child process is still executing and is called an orphan
process now.
However, the orphan process is soon adopted by init process, once its parent process dies.
LAB-5
Algorithm:
A process which has finished the execution but still has entry in the process table to report to its parent
process is known as a zombie process. A child process always first becomes a zombie before being
removed from the process table. The parent process reads the exit status of the child process which
reaps off the child process entry from the process table.
Objective: Write a program to show the concurrent execution of child and parent process using
fork() system call.
Algorithm:
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.
It takes no parameters and returns an integer value. Below are different values returned by fork().
Negative Value: creation of a child process was unsuccessful.
Zero: Returned to the newly created child process.
Positive value: Returned to parent or caller. The value contains process ID of newly created child
process.
Objective: Write a program to simulate FCFS scheduling algorithm without arrival time
Algorithm/Pseudocode:
Given n processes with their burst times, the task is to find average waiting time and average
turnaround time using FCFS scheduling algorithm.
First in, first out (FIFO), also known as first come, first served (FCFS), is the simplest scheduling
algorithm. FIFO simply queues processes in the order that they arrive in the ready queue.
In this, the process that comes first will be executed first and next process starts only after the
previous gets fully executed.
Below we have a few shortcomings or problems with the FCFS scheduling algorithm:
1. It is Non Pre-emptive algorithm, which means the process priority doesn't matter. If a process
with very least priority is being executed, more like daily routine backup process, which takes
more time, and all of a sudden some other high priority process arrives, like interrupt
to avoid system crash, the high priority process will have to wait, and hence inthis case, the
system will crash, just because of improper process scheduling.
2. Not optimal Average Waiting Time.
3. Resources utilization in parallel is not possible, which leads to Convoy Effect, and hence poor
resource(CPU, I/O etc.) utilization.
Objective: Write a program to simulate FCFS scheduling algorithm with arrival time
Algorithm/Pseudocode:
We have already discussed FCFS Scheduling of processes with same arrival time. In this post,
scenario when processes have different arrival times are discussed. Given n processes with their burst
times and arrival times, the task is to find average waiting time and average turnaround time using
FCFS scheduling algorithm.
FIFO simply queues processes in the order they arrive in the ready queue. Here, the process that
comes first will be executed first and next process will start only after the previous gets fully
executed.
1. Completion Time: Time at which process completes its execution.
2. Turn Around Time: Time Difference between completion time and arrival time. Turn Around
Time = Completion Time – Arrival Time
3. Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time
Ago:
Objective: Write a program to simulate Shortest Job First (SJF) scheduling algorithm.
Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with
the smallest execution time to execute next. SJN is a non-preemptive algorithm.
Shortest Job first has the advantage of having minimum average waiting time among all scheduling
algorithms.
It is a Greedy Algorithm.
It may cause starvation if shorter processes keep coming. This problem can be solved using the concept
of aging.
It is practically infeasible as Operating System may not know burst time and therefore may not sort
them. While it is not possible to predict execution time, several methods can be used to estimate the
execution time for a job, such as a weighted average of previous execution times. SJF can be used in
specialized environments where accurate estimates of running time are available.
Algorithm:
1- Sort all the processes in increasing order
according to burst time.
2- Then simply, apply FCFS.
Objective: Write a program to simulate Priority Scheduling Algorithm without arrival time.
Algorithm:
Priority scheduling is a non-preemptive algorithm and one of the most common scheduling
algorithms in batch systems. Each process is assigned a priority. Process with the highest priority is to
be executed first and so on.
Processes with the same priority are executed on first come first served basis. Priority can be decided
based on memory requirements, time requirements or any other resource requirement.
First input the processes with their burst time
and priority.
2- Sort the processes, burst time and priority
according to the priority.
3- Now simply apply FCFS algorithm.
LAB -8
Objective: Write a Program to simulate FCFS,LRU and OPTIMAL Page Replacement Algorithm
Given n processes with their burst times, the task is to find average waiting time and average turn
around time using FCFS scheduling algorithm.
First in, first out (FIFO), also known as first come, first served (FCFS), is the simplest scheduling
algorithm. FIFO simply queues processes in the order that they arrive in the ready queue.
In this, the process that comes first will be executed first and next process starts only after the
previous gets fully executed.
Here we are considering that arrival time for all processes is 0.
How to compute below times in Round Robin using a program?
1. Completion Time: Time at which process completes its execution.
2. Turn Around Time: Time Difference between completion time and arrival time. Turn
Around Time = Completion Time – Arrival Time
3. Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time
4. In operating systems that use paging for memory management, page replacement algorithm
are needed to decide which page needed to be replaced when new page comes in.
Whenever a new page is referred and not present in memory, page fault occurs and
Operating System replaces one of the existing pages with newly needed page. Different
page replacement algorithms suggest different ways to decide which page to replace. The
target for all algorithms is to reduce number of page faults.
5. In Least Recently Used (LRU) algorithm is a Greedy algorithm where the page to be replaced
is least recently used. The idea is based on locality of reference, the least recently used page is
not likely
6. Let say the page reference string 7 0 1 2 0 3 0 4 2 3 0 3 2 . Initially we have 4 page slots
empty.
Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already their so —> 0 Page fault.
when 3 came it will take the place of 7 because it is least recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already
available in the memory.
In operating systems, whenever a new page is referred and not present in memory, page fault
occurs and Operating System replaces one of the existing pages with newly needed page. Different
page replacement algorithms suggest different ways to decide which page to replace. The target for
all algorithms is to reduce number of page faults.
In this algorithm, OS replaces the page that will not be used for the longest period of time in future.
Examples :
Input : Number of frames, fn = 3
Reference String, pg[] = {7, 0, 1, 2,
0, 3, 0, 4, 2, 3, 0, 3, 2, 1,
2, 0, 1, 7, 0, 1};
Output : No. of hits = 11
No. of misses = 9
Algorithm:
Round Robin is a CPU scheduling algorithm where each process is assigned a fixed time slot in a
cyclic way.
● It is simple, easy to implement, and starvation-free as all processes get fair share of CPU.
● One of the most commonly used technique in CPU scheduling as a core.
● It is preemptive as processes are assigned CPU only for a fixed slice of time at most.
● The disadvantage of it is more overhead of context switching.
Algorithm:The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that
tests for safety by simulating the allocation for predetermined maximum possible amounts of all
resources, then makes an “s-state” check to test for possible activities, before deciding whether
allocation should be allowed to continue.
Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types.
Available :
● It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
● Available[ j ] = k means there are ‘k’ instances of resource type Rj
Max :
● It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a system.
● Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.
Allocation :
● It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently
allocated to each process.
● Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type Rj
Need :
● It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.
● Need [ i, j ] = k means process Pi currently need ‘k’ instances of resource type Rj
The algorithm for finding out whether or not a system is in a safe state can be described as follows:
1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
Resource-Request Algorithm
Let Requesti be the request array for process Pi. Requesti [j] = k means process Pi wants k instances of
resource type Rj. When a request for resources is made by process Pi, the following actions are taken:
3) Have the system pretend to have allocated the requested resources to process Pi by modifying the
state as
follows:
Available = Available – Requesti
Allocationi = Allocationi + Requesti
Needi = Needi– Requesti