OS Lab Manual
OS Lab Manual
For
Fourth Semester
B. Tech in Information Science and Engineering
Name
SRN
Branch
Semester
Section
Academic Year
INDEX
SL. No Contents Page. No
1 Prerequisites 3
2 Lab Objectives 3
3 Lab Outcomes 3
4. Lab Requirements 3
Course Objectives:
The objective of this lab is to:
1. Provide the knowledge and skills required to understand Basics of UNIX Operating
Environment
2. Introduce theory and implementation aspects of Banker’s Algorithm and Round Robin
scheduling algorithm; IPC, Scheduling algorithms
3. Design Parallel programming concepts.
Course Outcomes:
Lab Requirements:
Following are the required hardware and software for this lab, which is available in the
laboratory.
Software: Editor Tool included in the Linux package or Code Blocks in Widows operating
environment used to execute the programs written in C Programming language.
5. Given the list of processes and their CPU burst times, display/print the 24
Gantt chart for Round Robin Algorithm. For each of the scheduling
policies, compute and print the average waiting time and average
turnaround time.
9. The operating system replaces the page of a old process whenever a new 49
process page has to be loaded in memory. To select the page for
replacement there are many methods. Write a C program to implement
LRU page replacement algorithm.
1 Problem Statement
A child process in computing is a process created by another process (the parent process).
This technique pertains to multi tasking operating system and sometimes called a sub-
process or subtask. Now, use C language to create a child process to read commands from
the standard input and execute them.
3.2 Algorithm
3. If return value is -1 then a. Print "Process creation unsuccessfull" b. Terminate using exit
system call.
4. If return value is 0 then a. Print "Child process" b. Print process id of the child using getpid
system call
c. Print value of x d. Print process id of the parent using getppid system call
5. Otherwise a. Print "Parent process" b. Print process id of the parent using getpid system
call c. Print value of x d. Print process id of the shell using getppid system call.
6. Stop Result Thus a child process is created with copy of its parent's address space.
Child process
Enter the command: date
Thu May 07 13:17:32 IST 2009
3.5 Implementation Phase: Compile the Program and note the syntax errors/warning if
occurred during each compilation. Remove those syntax errors/warnings for
generation of executable file.
Fibonacci series is one of the optimal searching techniques. Multi-threaded program is used
to execute multiple process or threads concurrently by the central processing unit. Now,
run a Multi-threaded program in C to generate and print the Fibonacci series in such a way
that one thread has to generate the numbers up to the limit specified by the user and
another thread to print them.
3.2 Algorithm
#include<stdio.h>
#include<omp.h>
int fib(int n)
{
if(n<2) return n;
else return fib(n-1)+fib(n-2);
}
int main()
{
int fibnum[100],i,j,n;
printf(“Enter the series limit: “);
scanf(“%d”,&n);
#pragma omp barrier //To make the second thread wait till first one finishes the work
if(omp_get_thread_num()!=0)
{
printf(“Thread %d is printing number.\n”,omp_get_thread_num());
for(j=0;j<n;j++)
printf(“%d\t”,fib(j));
printf(“\n”);
}
}
return 0;
}
3.5 Implementation Phase: Compile the Program and note the syntax errors/warning if
occurred during each compilation. Remove those syntax errors/warnings for
generation of executable file.
1 Problem Statement
Producer-Consumer Problem also known as bounded-buffer problem is an example of multi-
process synchronization problem. This problem arises when the producer and consumer
share a common, fixed size buffer. The solution can be obtained by means of inter–process
communication typically using Semaphores. Now, implement a process with a produces
thread and a consumer thread which makes use of bounded buffer. Use any suitable
synchronization construct. (Implement producer-consumer problem using semaphores).
3.2 Algorithm
1. Create a shared memory segment BUFSIZE of size 1 and attach it.
2. Obtain semaphore id for variables empty, mutex and full using semget function.
3. Create semaphore for empty, mutex and full as follows: a. Declare semun, a union of
specific commands. b. The initial values are: 1 for mutex, N for empty and 0 for full c. Use
semctl function with SETVAL command
4. Create a child process using fork system call. a. Make the parent process to be the
producer b. Make the child process to the consumer
5. The producer produces 5 items as follows: a. Call wait operation on semaphores empty
and mutex using semop function. b. Gain access to buffer and produce data for
consumption c. Call signal operation on semaphores mutex and full using semop function.
6. The consumer consumes 5 items as follows: a. Call wait operation on semaphores full
and mutex using semop function. b. Gain access to buffer and consume the available
data. c. Call signal operation on semaphores mutex and empty using semop function.
7. Remove shared memory from the system using shmctl with IPC_RMID argument
8. Stop Result Thus synchronization between producer and consumer process for access to a
shared memory segment is implemented.
#include<stdio.h>
void producer();
void consumer();
int wait(int);
int signal(int);
1 Problem Statement
1. To design and develop CPU Scheduling policies of operating system. It is necessary to
develop various modules like, Process Manager, Memory Manager, Input-Output
Manager and Network Manager and many more. The process manager is one of the
important modules of OS. It deals with creation and execution of multiple processes
sharing the processor time. The process manager uses various scheduling policies.
Hence, there is a need to understand the various scheduling policies. Hence, given the
list of processes and their CPU burst times, display/print the Gantt chart for Priority.
For each of the scheduling policies, compute and print the average waiting time and
average turnaround time.
The CPU is allocated to the process based on the process having highest priority.
Priorities are generally some fixed range starting from 0 to 4095. Here it is assumed that
lower number represents higher priority.
3.2 Algorithm
1. Start the program
2. Get the number of jobs.
3. For the number of jobs get the job id, service time and the priority
4. Sort the jobs in ascending order according to the priority
5. First jobs time in and wait time is assigned to zero and its timeout is assigned to its
service time
6. From the second job to last job do the following
7. Calculate time in of each process by assigning time out of previous process
#include<stdio.h>
typedef struct priority
{
int pid,bt,tat,wt,prty;
}PRIORITY;
int main()
{
PRIORITY p[10],temp;
int i,j,n;
int sum_bt=0,sum_wt=0,sum_tat=0;
printf(“\nEnter the number of processes:\t”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter the burst time and priority of the process %d:\t”,i+1);
p[i].pid=i+1;
scanf(“%d%d”,&p[i].bt,&p[i].prty);
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(p[i].prty > p[j].prty)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
3.5 Implementation Phase: Compile the Program and note the syntax errors/warning if
occurred during each compilation. Remove those syntax errors/warnings for
generation of executable file.
Viva Questions:
School of Computing and Information Technology Page 23
1. What is the fixed range given to Priority queue scheduling?
2. List out different types of Priorities followed in CPU scheduling.
3. Define gaunt chart?
4. How to find the waiting time.
5. How to find the Turnaround time.
2. Given the list of processes and their CPU burst times, display/print the Gantt chart for
Round Robin Algorithm. For each of the scheduling policies, compute and print the
average waiting time and average turnaround time.
Round Robin algorithm is designed especially for time sharing system. In this algorithm, the
CPU switches between the processes based on the time called as quantum time or time slice.
A time quantum is generally reached between 5 to 10 msec depending on the algorithm. In
this algorithm the ready queue is circular when new process is added to the tail end. Then
CPU scheduler picks the first process from ready queue and sets a timer to interrupt later
when the quantum time expires.
3.2 Algorithm
1. Start the program
2. Get the number of jobs.
3. Get the time Quantum
4. For the each number of process do the following
i. (a)get the service time of each process
ii. (b)calculate the total service time
5. Do the following till j is less than total.
i. (a)If the burst time of first process is equal to Zero go for the next process
ii. (b)Otherwise Check whether it is greater than time quantum. If so do the
following otherwise go to
iii. (i)Calculate finish by adding start and time quantum
iv. (ii)Calculate new value of j by adding it with time quantum
#include<stdio.h>
typedef struct rr
{
int pid,bt,tat,wt,bt_bal;
}RR;
int main()
{
RR p[10];
int i,j,n,tq;
int sum_bt=0,sum_wt=0,sum_tat=0,tq_used=0;
printf(“\nEnter the number of processes:\t”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter the burst time of process %d:\t”,i+1);
p[i].pid=i+1;
scanf(“%d”,&p[i].bt);
p[i].bt_bal=p[i].bt;
}
3.5 Implementation Phase: Compile the Program and note the syntax errors/warning if
occurred during each compilation. Remove those syntax errors/warnings for
generation of executable file.
Viva questions:
1. What do you mean by Primitive CPU algorithm?
2. Justify whether Round robin algorithm is primitive or Non primitive algorithm.
3. Define Circular queue in terms of Scheduling.
4. Define Quantum time.
1 Problem Statement
3. In multiprogramming environment, several processes execute at the same time sharing the
processor time. It is required to understand the performance of the policies FCFS and SJF in
proper utilization of CPU time. Therefore write program to compare their performance
metrics in terms of average waiting time, average turnaround time.
3.2 Algorithm
FCFS:
3. Get the Burst time and starting time for each process.
4. Calculate Waiting time by taking difference between Starting time and arrival time
for
each process.
6. Calculate the turnaround time by taking difference between Finished time and arrival
time.
SJF:
3. Get the Burst time and starting time of the each process.
4. Find the process which as least burst time for scheduling first.
5. Calculate Waiting time by taking difference between Starting time and arrival time
for
each process.
7. Calculate the turnaround time by taking difference between Finished time and arrival
time.
FCFS
#include<stdio.h>
int main()
{
int b[10],t[10],w[10];
int n,i,j;
float avgw,avgt;
printf("Enter the number of processes:\t");
scanf("%d",&n);
for(i=0;i<n;i++)
{
w[0]=0;
for(i=1;i<n;i++)
{
w[i]=w[i-1]+b[i-1];
}
for(i=0;i<n;i++)
{
t[i]=w[i]+b[i];
}
for(i=0;i<n;i++)
{
avgw += w[i];
avgt += t[i];
}
printf(“Total waiting time=%f\n”,avgw);
avgw /= n;
printf(“Average waiting time=%f\n”,avgw);
printf(“\n”);
printf(“Total turnaround time=%f\n”,avgt);
avgt /= n;
printf(“Average turnaround time-%f\n”,avgt);
}
SJF
#include<stdio.h>
int main()
{
int p[10],b[10],w[10],t[10],i,n,j,temp,temp1;
float awt=0,att=0;
printf("Enter the number of processes:\t");
scanf("%d",&n);
for(i=0i<n;i++)
{
printf("Enter the burst time of process %d:\t”,i);
scanf(“%d”,&b[i]);
p[i]=i;
}
for(i=0;i<n;i++)
School of Computing and Information Technology Page 32
{
for(j=i;j<n;j++)
{
if(b[i] > b[j])
{
temp=b[i];
temp1=p[i];
b[i]=b[j];
p[i]=p[j];
b[j]=temp;
p[j]=temp1;
}
}
}
w[0]=0;
for(i=1;i<n;i++)
{
w[i]=w[i-1]+b[i-1];
}
for(i=0;i<n;i++)
{
t[i]=w[i]+b[i];
}
for(i=0;i<n;i++)
{
awt=awt+w[i];
att=att+t[i];
}
printf("\n\t Processes \t waiting time \t turnaround time\n");
for(i=0;i<n;i++)
printf("\t P%d \t\t %d \t\t\t %d\n",p[i],w[i],t[i]);
printf(“Total waiting time=%f\n”,awt);
awt /= n;
printf(“Average waiting time=%f\n”,awt);
printf(“\n”);
printf(“Total turnaround time=%f\n”,att);
att /=n;
printf(“Average turnaround time-%f\n”,att);
}
FCFS
SJF
3.5 Implementation Phase: Compile the Program and note the syntax errors/warning if
occurred during each compilation. Remove those syntax errors/warnings for
generation of executable file.
1 Problem Statement
Banker’s Algorithm is used for Deadlock Avoiding purpose. It is suitable to resource allocation
system with multiple instances of each resource type. Implement Banker’s Algorithm which
finds whether the state is safe or not.
3.1 Theory
Banker’s algorithm is used for deadlock avoiding purpose. It is suitable to resource
allocation system with multiple instances of each resources type. Algorithm makes decision
or granting the resources based on whether or not if I grant the resources which will put the
system to unsafe state I.e. which can lead for deadlock situation. Banker’s algorithm are
implemented with some of the data structures like Data structure for available, Data
structure for maximum, Data structure for allocation and Data structure for need.
3.2 Algorithm
1. Let work and finish be two vectors of length ‘M’and ‘N’. Initially consider
Work= Available, then Finish[i] =false, for i=1, 2, 3,…, n.
2. Find variable i such that the both conditions i.e finish[i] =false and need[i] < work.
If no such i exist then go to step 4.
3. Work= Work + allocation.
Finish[i] = true, then go to step 2.
4. If finish[i] = true (for all i) then the system is in safe state.
#include<stdio.h>
struct process
{
int max[10],alloc[10],need[10];
}
p[6];
int avail[10],n,r,safe[10];
void getdata();
void banker();
main()
{
getdata();
banker();
}
void getdata()
{
int i,j;
printf("Enter the number of process=>\n");
scanf("%d",&n);
printf("Enter the number of resources=>\n");
scanf("%d",&r);
printf("Enter Allocated Resources=>\n");
for(i=0;i<n;i++)
{
printf("Process p%d\n",i);
for(j=0;j<r;j++)
scanf("%d",&p[i].alloc[j]);
School of Computing and Information Technology Page 38
}
printf("Enter Max of Each Process\n");
for(i=0;i<n;i++)
{
printf("Process p%d\n",i);
for(j=0;j<r;j++)
scanf("%d",&p[i].max[j]);
}
printf("Enter the Current Available Resources \n");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
printf("Need Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
p[i].need[j]=p[i].max[j]-p[i].alloc[j];
printf("%d\t",p[i].need[j]);
}
printf("\n");
}
}
void banker()
{
int flag,c=0,finish[10],i,j,f,s;
for(i=0;i<n;i++)
finish[i]=0;
do
{
f=0;
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
flag=0;
if(p[i].need[j]>avail[j])
{
flag=1;
break;
}
}
if(flag==0&&finish[i]==0)
{
f=1;
Need Matrix
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1
----SAFE STATE----
Safe Sequence:
p1 p3 p4 p0 p2
School of Computing and Information Technology Page 41
For Unsafe State
Need Matrix
6 2 0
1 2 2
8 1 1
6 0 7
----UNSAFE STATE----
Viva questions:
1. Define deadlock.
2. Distinguish between safe and unsafe state.
3. List out different conditions to avoid deadlock.
4. Define Climb edge.
5. Explain different Data structure used in Banker’s algorithm.
The operating system manages the computer’s memory. The OS allocates required memory for the
task and deallocates memory after execution of task. There are many allocation strategies. Write a C
program to simulate the Multi Programming with Variable number of Task (MVT) memory strategy.
2: Declare blocks,processes,fragments
7: Repeat Steps 8 to 13
9: If processes[i]<=blocks[j] Then
12: Print “Fragment #”,i+1,”Size is : “,fragments[i] //Each fragment and its size
13: Exit loop // inner loop only [End If] [End For j] [End For i]
15: Stop
#include<stdio.h>
main()
{
int i,m,n,tot,s[20];
printf("Enter total memory size:");
scanf("%d",&tot);
printf("Enter number of pages:");
scanf("%d",&n);
printf("Enter memory for OS:");
scanf("%d",&m);
for(i=0;i<n;i++)
{
printf("Enter size of page%d:",i+1);
scanf("%d",&s[i]);
}
tot=tot-m;
for(i=0;i<n;i++)
{
if(tot>=s[i])
{
printf("Allocate page%d\n",i+1);
tot=tot-s[i];
Allocate page1
Allocate page2
Allocate page3
Allocate page4
3.5 Implementation Phase: Compile the Program and note the syntax errors/warning if
occurred during each compilation. Remove those syntax errors/warnings for
generation of executable file.
3. Define swapping.
1 Problem Statement
The operating system replaces the page of a old process whenever a new process page has to be
loaded in memory. To select the page for replacement there are many methods. Write a C program to
implement LRU page replacement algorithm.
The influence that these page allocation methods have had on virtual memory
The mechanics of paging and how a memory allocation scheme determines which
pages should be swapped out of memory
The concept of the working set and how it is used in memory allocation schemes
#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];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&p[i]);
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++)
School of Computing and Information Technology Page 51
{
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");
}/* main */
7 5
7 5 9
4 5 9
4 3 9
9 3 7
9 6 7
9 6 2
1 6 2
3.5 Implementation Phase: Compile the Program and note the syntax errors/warning if
occurred during each compilation. Remove those syntax errors/warnings for
generation of executable file.
1 Problem Statement
The operating system manages storage of information by creating and storing information in the file.
There are many methods of creating file. Write a C program to implement any one of the file
allocation techniques (Linked, Indexed or Contiguous).
In linked allocation, each file is a linked list of disk blocks. The directory contains a pointer to the first and
(optionally the last) block of the file. For example, a file of 5 blocks which starts at block 4, might continue
at block 7, then block 16, block 10, and finally block 27. Each block contains a pointer to the next block and
the last block contains a NIL pointer. The value -1 may be used for NIL to differentiate it from block 0.
With linked allocation, each directory entry has a pointer to the first disk block of the file. This pointer is
initialized to nil (the end-of-list pointer value) to signify an empty file. A write to a file removes the first
free block and writes to that block. This new block is then linked to the end of the file. To read a file, the
pointers are just followed from block to block.
There is no external fragmentation with linked allocation. Any free block can be used to satisfy a request.
Notice also that there is no need to declare the size of a file when that file is created. A file can continue to
grow as long as there are free blocks. Linked allocation, does have disadvantages, however. The major
problem is that it is inefficient to support direct-access; it is effective only for sequential-access files. To
find the ith block of a file, it must start at the beginning of that file and follow the pointers until the ith
block is reached. Note that each access to a pointer requires a disk read.
3.2 Algorithm
Contiguous Allocation
1. Requires that each file occupy a set of contiguous blocks on the disk
2. Disk addresses define a linear ordering on the disk
3. Defined by the disk address and length of the first block
4. The directory entry for each file indicates the address of the starting block and the length of the
area allocated for this file.
3.3 Coding using C Language
#include<stdio.h>
main()
{
int f[50],p,i,j,k,a,st,len,n,c;
for(i=0;i<50;i++)
f[i]=0;
scanf("%d",&p);
for(i=0;i<p;i++)
scanf("%d",&a);
f[a]=1;
X:
scanf("%d%d",&st,&len);
k=len;
if(f[st]==0)
for(j=st;j<(k+st);j++)
if(f[j]==0)
f[j]=1;
printf("\n%d->%d",j,f[j]);
else
k++;
scanf("%d",&c);
if(c==1)
goto X;
else
10
14
10
4->1
5->1
6->1
8->1
12->1
13->1
15->1
16->1
17->1
3.5 Implementation Phase: Compile the Program and note the syntax errors/warning if
occurred during each compilation. Remove those syntax errors/warnings for
generation of executable file.
Viva Questions
1. Define seek time and latency time.
1. A child process in computing is a process created by another process (the parent process). This
technique pertains to multi-tasking operating system and sometimes called a sub-process or
subtask. Now, use C language to create a Child Process to print its Process ID (PID) and Parent
3. Write and Execute a C program by using any suitable synchronization construct to implement
Dining Philosopher's Problem
4. Develop a program where system running ten I/O-bound tasks and one CPU-bound task.
Assume that the I/O-bound tasks issue an I/O operation once for every millisecond of CPU
computing and that each I/O operation takes 10 millisecond to complete. Also assume that the
context-switching overhead is 0.1 millisecond and that all processes are long –running tasks.
What is the CPU utilization for a round –robin scheduler when:
5. Design, develop and execute a program in C / C++ to simulate the working of Shortest
Remaining Time first Scheduling Algorithms. Determine the average turn-around time. The input
can be read from key board or from a file.
6. Design, develop and run a program to implement the Resource allocation algorithm.
Demonstrate its working with different data values.
7. Write program to create two threads. Protect shared resources (use two arrays of integer
elements) in both the threads using semaphores. Illustrate how deadlock happens while
protecting shared resources using semaphores.
8. Simulate the memory management technique Multi Programming with Fixed Task (MFT).