KEMBAR78
OS Lab Manual | PDF | Process (Computing) | Thread (Computing)
0% found this document useful (0 votes)
18 views64 pages

OS Lab Manual

This document outlines the Operating Systems Lab curriculum for B. Tech students in Information Science and Engineering, detailing prerequisites, objectives, outcomes, and requirements. It includes a comprehensive list of lab exercises and assignments focused on UNIX programming, scheduling algorithms, and inter-process communication. The document also provides detailed instructions for implementing various programming tasks in C, including creating child processes, generating Fibonacci series, and solving the Producer-Consumer problem.

Uploaded by

Akram Afzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views64 pages

OS Lab Manual

This document outlines the Operating Systems Lab curriculum for B. Tech students in Information Science and Engineering, detailing prerequisites, objectives, outcomes, and requirements. It includes a comprehensive list of lab exercises and assignments focused on UNIX programming, scheduling algorithms, and inter-process communication. The document also provides detailed instructions for implementing various programming tasks in C, including creating child processes, generating Fibonacci series, and solving the Producer-Consumer problem.

Uploaded by

Akram Afzal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 64

BENGALURU, INDIA

SCHOOL OF COMPUTING AND INFORMATION TECHNOLOGY

Operating Systems Lab


B20CI0405

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

4 List of Lab Exercises 4

5 Solutions for Lab Exercises 6

6 List of Lab Assignments 63

School of Computing and Information Technology Page 2


Prerequisites:
UNIX fundamental and C Programming Language, Concepts of Operating Systems

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:

At the end of this lab, the student shall be able to:

1. Appreciate the scope of UNIX operating environment;


2. Implement Banker’s Algorithm and Round Robin scheduling algorithm using C.
Apply concepts to develop Parallel programming.

Lab Requirements:

Following are the required hardware and software for this lab, which is available in the
laboratory.

 Hardware: Desktop system or Virtual machine in a cloud with OS installed. Presently in


the Lab, Pentium IV Processor having 1 GB RAM and 250 GB Hard Disk is available.
Desktop systems are dual boot having Windows as well as Linux OS installed on them.

 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.

School of Computing and Information Technology Page 3


LIST OF LAB EXERCISES
SL.NO Lab Programs PAGE NO.

1. A child process in computing is a process created by another process (the 6


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.

2. Fibonacci series is one of the optimal searching techniques. Multi- 10


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. Producer-Consumer Problem also known as bounded-buffer problem is an 14


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).

4. To design and develop operating system, it is necessary to develop various 19


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.

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.

6 In multiprogramming environment, several processes execute at the same


29

School of Computing and Information Technology Page 4


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 and average turnaround time.

7 Banker’s Algorithm is used for Deadlock Avoiding purpose. It is suitable to 36


resource allocation system with multiple instances of each resource type.
Implement Banker’s Algorithm which finds whether the state is safe or
not.

8. The operating system manages the computer’s memory. The OS allocates 44


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.

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.

10. The operating system manages storage of information by creating and 56


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).

School of Computing and Information Technology Page 5


Program 1: Child Process Creation

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.

2 Student Learning Outcomes


After successful completion of this lab, the student will able to
 Analyze how to create a child process
 Implement a code which creates a child process and execute the code.

3 Design of the Program


3.1 Theory
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.

3.2 Algorithm

School of Computing and Information Technology Page 6


To create a new child process using fork system call.

1. Declare a variable x to be shared by both child and parent.

2. Create a child process using fork system call.

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.

3.3 Coding using C Language


#include<stdio.h>
#include<unistd.h>
int main()
{
int x,ch;
char cmd[20];
int pid=fork();
if(pid==0) /* child process execution */
{
printf("\n Child process");
do
{
printf("\n Enter the command:");
scanf("%s",&cmd);
system(cmd);
printf("\n Enter 1 to continue or 0 to exit:");
scanf("%d",&ch);

School of Computing and Information Technology Page 7


}
while(ch!=0);
}
else /* parent execution*/
wait(); /* Block the parent until the completion of the child*/
}
3.4 Expected Results

[root@localhost~]# gedit child.c


[root@localhost~]# cc child.c
[root@localhost~]# ./a.out

Child process
Enter the command: date
Thu May 07 13:17:32 IST 2009

Enter 1 to continue or 0 to exit:1

Enter the command: ls


1a.sh 2a.sh 3a.sh 4a.sh 5a.sh 6a.sh 7a.sh 1b.c 2b.c 3b.c 4b.c 5b.c 6b.c 7b.c
child.c a.out cc

Enter 1 to continue or 0 to exit: 1

Enter the command: who


root :0 Sep 10 12:54
root pts/1 Sep 10 12:55 (:0.0)

Enter 1 to continue or 0 to exit: 1

Enter the command: ps


PID TTY TIME CMD
3620 pts/1 00:00:00 bash
3705 pts/1 00:00:00 a.out
3706 pts/1 00:00:00 a.out
3711 pts/1 00:00:00 ps

Enter 1 to continue or 0 to exit: 1

Enter the command: pwd


/root/hema/unix

Enter 1 to continue or 0 to exit: 0


[root@localhost~]#

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.

School of Computing and Information Technology Page 8


3.6 Simulation of Errors/Warnings:
3.6.1 Syntax Errors/Warnings:
S. No. Errors/Warnings Reflection of the Errors/Warnings in

3.6.2 Logical Errors:


S. No. Error Simulated Reflection of the error in compilation/output

4 Actual Output Obtained

School of Computing and Information Technology Page 9


Viva Questions:
1. What does the return value of fork() imply?
2. What are the different ways of creating a child process?
3. What is system() function?
4. What is a typical syntax being followed when issuing commands in shell?
5. What is Kernel and shell?

Program 2: Fibonacci Series


1 Problem Statement

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.

2 Student Learning Outcomes

After successful completion of this lab, the student will able to


 Create multi-thread.
 Implement a code which generates Fibonacci series till the specified limit.

3 Design of the Program


3.1 Theory
Multi-threaded program is used to execute multiple process or threads concurrently by the
central processing unit

3.2 Algorithm

School of Computing and Information Technology Page 10


1. Set the number of threads as 2 by calling omp_set_num_threads(2)
2. Read from standard input to generate fibonacci series of a specified number
3. Specify variables within the parallel region as private i.e., private(tid,tmp,FibNum) so
that each thread will have it’s own copy
4. Rand() generates random number, mod%24 of that number is considered which gives
the value within the range of 24
5. The omp critical directive identifies the section of code that must be executed by a
single thread at a time.
6. One thread is used to calculate the fibonacci series and the other thread to print that.
7. omp_get_num_threads() displays number of threads used.

3.3 Coding using C Language

#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 parallel num_threads(2) shared(fibnum,n) private(i,j) //openmp directive


{
if(omp_get_thread-num()==0)
{
printf(“There are %d threads\n”,omp_get_num_threads());

School of Computing and Information Technology Page 11


printf(“Thread %d is generating number.\n”,omp_get_thread_num());
for(i=0;i<n;i++)
fibnum[i]=fib(i);
}

#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.4 Expected Results

root@localhost~]# gedit fibonacci.c


root@localhost ~]# cc -fopenmp fibonacci.c
root@localhost ~]# ./a.out

Enter the series limit: 7


There are 2 threads
Thread 1 is generating number.
Thread 2 is printing number.
1 1 2 3 5 8 13

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.

School of Computing and Information Technology Page 12


3.6 Simulation of Errors/Warnings:
3.6.1 Syntax Errors/Warnings:
S. No. Errors/Warnings Reflection of the Errors/Warnings in

3.6.2 Logical Errors:


S. No. Error Simulated Reflection of the error in compilation/output

Actual Output Obtained


4

School of Computing and Information Technology Page 13


Viva questions:
1. What is parallel programming?
2. How do you determine status of a thread?
3. Differentiate between process and thread.
4. What happens if parent thread exits before child thread?
5. How is a thread killed?

Program 3: Producer-Consumer Problem

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).

School of Computing and Information Technology Page 14


2 Student Learning Outcomes

After successful completion of this lab, the student will able to


 Apply Multi-process synchronization concept for bounded buffer concept.
 Implement the code for inter-process communication.
 Implement the code of semaphore.

3 Design of the Program


3.1 Theory
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.

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.

3.3 Coding using C Language

#include<stdio.h>
void producer();
void consumer();
int wait(int);
int signal(int);

School of Computing and Information Technology Page 15


int mutex=1,full=0,empty=3,x=0;
main()
{
int n;
printf("\n1.PRODUCER\n2.CONSUMER\n3.EXIT\n");
while(1)
{
printf("\nENTER YOUR CHOICE\n");
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: exit(0);
break;
}
}
}
int wait(int s)
{
return(--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nproducer produces the item%d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);

School of Computing and Information Technology Page 16


full=wait(full);
empty=signal(empty);
printf("\n consumer consumes item%d",x);
x--;
mutex=signal(mutex);
}
3.4 Expected Results

root@localhost~]# gedit prodcons.c


root@localhost~]# cc prodcons.c
[root@localhost ~]# ./a.out

1.PRODUCER 2.CONSUMER 3.EXIT


ENTER YOUR CHOICE
2
BUFFER IS EMPTY
ENTER YOUR CHOICE
1
producer produces the item1
ENTER YOUR CHOICE
1
producer produces the item2
ENTER YOUR CHOICE
1
producer produces the item3
ENTER YOUR CHOICE
1
BUFFER IS FULL
ENTER YOUR CHOICE
2
consumer consumes item3
ENTER YOUR CHOICE
2
consumer consumes item2
ENTER YOUR CHOICE
2
consumer consumes item1
ENTER YOUR CHOICE
2
BUFFER IS EMPTY
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.

School of Computing and Information Technology Page 17


3.6 Simulation of Errors/Warnings:
3.6. Syntax Errors/Warnings:
1 S. No. Errors/Warnings Reflection of the Errors/Warnings in

3.6. Logical Errors:


2 S. No. Error Simulated Reflection of the error in compilation/output

4 Actual Output Obtained

School of Computing and Information Technology Page 18


Viva questions:
1. What are Semaphores?
2. Explain Producer-Consumer problem.
3. List different types of Inter-Process Communication.
4. What is the difference between Mutex and Semaphore.
5. If a binary semaphore can be used to achieve mutual exclusion then why does the
concept of a 'mutex' exist?

School of Computing and Information Technology Page 19


Program 4: CPU Scheduling policies

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.

2 Student Learning Outcomes

After successful completion of this lab, the student will able to


 Analyze different CPU scheduling policies.
 Design Gantt chart for the given processes on the basis of priority.
 Implement a code which calculates average waiting time and turnaround time for
priority CPU scheduling.

3 Design of the Program


3.1 Theory

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

School of Computing and Information Technology Page 20


8. Calculate time out of each process by adding its corresponding time in and service
time
9. Wait time of each process is equal to its time in
10. Calculate Average wait time and average turnaround time
11. Display all the jobs along with their service time waiting time and turnaround time
12. Stop the program.
3.3 Coding using C Language

#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;
}

School of Computing and Information Technology Page 21


for(i=0;i<n;i++)
{
sum_bt += p[i].bt;
p[i].tat=sum_bt;
p[i].wt=p[i].tat - p[i].bt;
sum_wt += p[i].wt;
sum_tat += p[i].tat;
}
printf(“\nTotal waiting time=%d\n”,sum_wt);
printf(“Average waiting time=%.2f\n”,(float)sum_wt/n);
printf(“\n”);
printf(“Total turnaround time=%d\n”,sum_tat);
printf(“Average turnaround time=%.2f\n”,(float)sum_tat/n);
}

3.4 Expected Results

root@localhost~]# gedit priority.c


root@localhost~]# cc priority.c
[root@localhost ~]# ./a.out

Enter the number of processes: 5


Enter the burst time and priority of the process 1: 10 3
Enter the burst time and priority of the process 2: 1 1
Enter the burst time and priority of the process 3: 2 4
Enter the burst time and priority of the process 5: 1 5
Enter the burst time and priority of the process 1: 5 2

Total waiting time=41


Average waiting time=8.20

Total turnaround time=60


Average turnaround time=12.00

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.

School of Computing and Information Technology Page 22


3.6 Simulation of Errors/Warnings:
3.6.1 Syntax Errors/Warnings:
S. No. Errors/Warnings Reflection of the Errors/Warnings in

3.6.2 Logical Errors:


S. No. Error Simulated Reflection of the error in compilation/output

4 Actual Output Obtained

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.

Program 5: Round Robin Algorithm

School of Computing and Information Technology Page 24


1 Problem Statement

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.

2 Student Learning Outcomes

After successful completion of this lab, the student will able to


 Design Gantt chart for Round Robin Algorithm.
 Implement a code which calculates the Average waiting and turnaround time.

3 Design of the Program


3.1 Theory

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

School of Computing and Information Technology Page 25


v. (iii)start is assignees to the value of finish
vi. (iv)service time is calculated by deducting time quantum from its value
vii. (c) (i)calculate the value of finish by adding start and service time of process
viii. (ii)calculate new value of j by adding its value with service time of process
ix. (iii)service time of a corresponding process assigned to zero
x. (iv)wait time is calculated by deducting value of service time from finish
xi. (v)Turnaround time is calculated by adding ST
xii. (d)Repeat steps 5a to 5c till the last job
6. Display all the process along with their waiting time and turnaround time, also
average waiting time and average turnaround time.
7. Stop the program

3.3 Coding using C Language

#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;
}

printf(“Enter the time quantum number:\t”);

School of Computing and Information Technology Page 26


scanf(“%d”,tq);
for(i=0;i<n;i++)
sum_bt += p[i].bt;
printf(“\nSum of burst time=%d\n”,sum_bt);
do
{
for(i=0;i<n;i++)
if(p[i].bt_bal > 0 && p[i].bt_bal <= tq)
{
tq_used += p[i].bt_bal;
p[i].tat=tq_used;
p[i].wt=p[i].tat - p[i].bt;
p[i].bt_bal=0;
}
else if(p[i].bt_bal > 0)
{
tq_used += tq;
p[i].bt_bal -= tq;
}
else if(p[i].bt_bal < 0)
{
printf(“\nError: Burst time is negative\n”);
exit(1);
}
}
while(tq_used != sum_bt);
for(i=0;i<n;i++)
sum_wt += p[i].wt;
for(i=0;i<n;i++)
sum_tat += p[i].tat;
printf(“\nTotal waiting time=%d\n”,sum_wt);
printf(“Average waiting time=%.2f\n”,(float)sum_wt/n);
printf(“\n”);
printf(“Total turnaround time=%d\n”,sum_tat);
printf(“Average turnaround time=%.2f\n”,(float)sum_tat/n);
}

3.4 Expected Results

School of Computing and Information Technology Page 27


root@localhost~]# gedit rounrobin.c
root@localhost~]# cc roundrobin.c
[root@localhost ~]# ./a.out

Enter the number of processes: 3


Enter the burst time of process 1: 24
Enter the burst time of process 2: 3
Enter the burst time of process 3: 3

Sum of burst time=30

Total waiting time=17


Average waiting time=5.67

Total turnaround time=47


Average turnaround time=15.67

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.6 Simulation of Errors/Warnings:


3.6.1 Syntax Errors/Warnings:
S. No. Errors/Warnings Reflection of the Errors/Warnings in

3.6.2 Logical Errors:


S. No. Error Simulated Reflection of the error in compilation/output

School of Computing and Information Technology Page 28


4 Actual Output Obtained

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.

School of Computing and Information Technology Page 29


Program 6: FCFS and SJF CPU Scheduling algorithms

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.

2 Student Learning Outcomes

After successful completion of this lab, the student will able to


 Apply Multiprogramming concept to develop a program for FCFS and SJF
 Implement FCFS code to calculate average waiting time, average turnaround time.
 Implement SJF code to calculate average waiting time, average turnaround time.

3 Design of the Program


3.1 Theory
FCFS(First Come First Serve):
Here in this scheme the process that request the CPU first is allocated the CPU clock
cycle, the implementation is as similar to the first in first out (FIFO) procedure which is used
inside the queuing concept. FCFS is non-primitive scheduling algorithm.

SJF(Shortest Job First):


The idea of this algorithm is to schedule the process which has smallest CPU burst time.
Whenever there are two process having the same CPU burst time then we can follow first
come first serve mechanism to select the process.

3.2 Algorithm
FCFS:

1. Start the program

2. Get the number of process to be scheduled.

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.

School of Computing and Information Technology Page 30


5. Calculate average waiting time.

6. Calculate the turnaround time by taking difference between Finished time and arrival
time.

7. Calculate Average turnaround time.

SJF:

1. Start the program

2. Get the number of process to be scheduled.

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.

6. Calculate average waiting time.

7. Calculate the turnaround time by taking difference between Finished time and arrival
time.

8. Calculate Average turnaround time.

3.3 Coding using C Language

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++)
{

School of Computing and Information Technology Page 31


printf("\n Enter the burst time of process %d:\t",i);
scanf("%d",&b[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);
}

3.4 Expected Results

FCFS

root@localhost~]# gedit fcfs.c

School of Computing and Information Technology Page 33


root@localhost~]# cc fcfs.c
[root@localhost ~]# ./a.out

Enter the number of processes: 3


Enter the burst time of process 0: 24
Enter the burst time of process 1: 3
Enter the burst time of process 2: 3

Total waiting time=51.000000


Average waiting time=17.000000

Total turnaround time=81.000000


Average turnaround time=27.000000

SJF

root@localhost~]# gedit sjf.c


root@localhost~]# cc sjf.c
[root@localhost ~]# ./a.out

Enter the number of processes: 3


Enter the burst time of process 0: 24
Enter the burst time of process 1: 3
Enter the burst time of process 2: 3

Processes waiting time turnaround time


P1 0 3
P2 3 6
P0 6 30

Total waiting time=9.000000


Average waiting time=3.000000

Total turnaround time=39.000000


Average turnaround time=13.000000

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.

School of Computing and Information Technology Page 34


3.6 Simulation of Errors/Warnings:
3.6. Syntax Errors/Warnings:
1 S. No. Errors/Warnings Reflection of the Errors/Warnings in

3.6. Logical Errors:


2 S. No. Error Simulated Reflection of the error in compilation/output

4 Actual Output Obtained

School of Computing and Information Technology Page 35


Viva questions:

1. Differentiate between FCFS and SJF.


2. How do you find waiting time for FCFS and SJF.
3. How do you find turnaround time for FCFS and SJF.
4. Consider an example and draw gantt chart for FCFS and SJF.

School of Computing and Information Technology Page 36


Program 7: Banker’s algorithm

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.

2 Student Learning Outcomes

After successful completion of this lab, the student will able to


 Analyze to allocate the resources without causing deadlock.
 Develop a framework for resource request allocation to achieve safe state.
 Implement a code which finds whether the state is safe or not.

3 Design of the Program

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.

School of Computing and Information Technology Page 37


Resource request allocation algorithm:

1. If request i < need i, then go to step 2 otherwise raise an error.


2. If request i < available, then go to step 3 otherwise Pi must wait because the
resources
are not available.
3. Available = Available – Request i.
4. Allocation = Allocation + Request i.
5. Need = Need i – Request i.
6. If the resulting resource allocation state is safe then the transaction is completed and
the
Process Pi is allocated with the resources.
3.3 Coding using C Language

#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;

School of Computing and Information Technology Page 39


printf("After process p%d Available Resources=",i);
for(j=0;j<r;j++)
{
p[i].need[j]=0;
avail[j]=p[i].alloc[j]+avail[j];
printf("%d\t",avail[j]);
}
printf("\n");
safe[c++]=i;
finish[i]=1;
} //if
} //for
if(f==0)
break;
}
while(f==1);
s=1;
for(i=0;i<n;i++)
if(finish[i]==0)
{
printf("Cannot Allocate Requested Resources for Process p%d",i);
s=0;
break;
}
if(s!=0)
{
printf("----SAFE STATE----\n");
printf("Safe sequence:\n");
for(i=0;i<c;i++)
printf("p%d\t",safe[i]);
printf("\n");
}
else
printf("----UNSAFE STATE----\n");
} //banker

3.4 Expected Results

For Safe State

root@localhost~]# gedit banker.c


root@localhost~]# cc banker.c
[root@localhost ~]# ./a.out

Enter the number of process=> 5

School of Computing and Information Technology Page 40


Enter the number of resources=> 3
Enter Allocated Resources=>
Process p0
010
Process p1
200
Process p2
302
Process p3
211
Process p4
002

Enter Max of Each Process


Process p0
753
Process p1
322
Process p2
902
Process p3
222
Process p4
433

Enter the Current Available Resources


332

Need Matrix
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1

After process p1 Available Resources= 5 3 2


After process p3 Available Resources= 7 4 3
After process p4 Available Resources= 7 4 5
After process p0 Available Resources= 7 5 5
After process p2 Available Resources= 10 5 7

----SAFE STATE----

Safe Sequence:
p1 p3 p4 p0 p2
School of Computing and Information Technology Page 41
For Unsafe State

root@localhost~]# gedit banker.c


root@localhost~]# cc banker.c
[root@localhost ~]# ./a.out

Enter the number of process=> 4


Enter the number of resources=> 3
Enter Allocated Resources=>
Process p0
012
Process p1
310
Process p2
022
Process p3
211

Enter Max of Each Process


Process p0
632
Process p1
432
Process p2
833
Process p3
818

Enter the Current Available Resources


232

Need Matrix
6 2 0
1 2 2
8 1 1
6 0 7

After process p1 Available Resources= 5 4 2


Cannot Allocate Requested Resources for Process p0

----UNSAFE STATE----

School of Computing and Information Technology Page 42


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.6 Simulation of Errors/Warnings:


3.6.1 Syntax Errors/Warnings:
S. No. Errors/Warnings Reflection of the Errors/Warnings in

3.6.2 Logical Errors:


S. No. Error Simulated Reflection of the error in compilation/output

School of Computing and Information Technology Page 43


4 Actual Output Obtained

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.

School of Computing and Information Technology Page 44


Program 8: Multi Programming with Variable Task (MVT)
1 Problem Statement

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 Student Learning Outcomes

After successful completion of this lab, the student will able to


 Understand the working of the Multiprogramming concept of Operating system.
 Analyze the fragmentation concepts

3 Design of the Program


3.1 Theory
This scheme of memory management was first introduces in IBM OS/MVT. The main
characteristics of the MVT are:
1. Both the number and size of the partitions change with time.
2. Dynamic address translation is followed
3. Must perform and addition on every memory reference to add the starting address of
the partition
4. Eliminates Internal Fragmentation
5. Introduce external Fragmentation
3.2 Algorithm
Step 1: Start

2: Declare blocks,processes,fragments

3: Declare i,j,totalfragments:=0,np,nb //i,j for index and np-no.of processes,nbno.of


blocks

4: Input np,nb //no.of processes and no.of blocks

4: Set blocks:=call allocateMemoryBlocks()

5: Set processes:=call allocateProcesses()

6: Set fragments:=allocate with size of no.of processes

7: Repeat Steps 8 to 13

For I:=0 to np step 1 //no.of process times

School of Computing and Information Technology Page 45


8: Repeat Steps 9 to 13

For j:=0 to nb step 1 //no.of blocks times

9: If processes[i]<=blocks[j] Then

10: Set fragments[i]:=blocks[j]-processes[i] //allocation and subtraction for remaining


space

11: Set totalfragments+=fragments[i]

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]

14: Print “Total fragmentation : “,totalfragments

15: Stop

3.3 Coding using C Language

#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];

School of Computing and Information Technology Page 46


}
else
printf("process p%d is blocked\n",i+1);
}
printf("External Fragmentation is:%d",tot);
}
3.4 Expected Results

[root@localhost~]# gedit mvt.c


root@localhost~]# cc mvt.c
[root@localhost ~]# ./a.out

Enter total memory size: 50

Enter number of pages: 4

Enter memory for OS: 10

Enter size of page1: 10

Enter size of page2: 9

Enter size of page3: 9

Enter size of page4: 10

Allocate page1

Allocate page2

Allocate page3

Allocate page4

External Fragmentation is: 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.

School of Computing and Information Technology Page 47


3.6 Simulation of Errors/Warnings:
3.6.1 Syntax Errors/Warnings:
S. No. Errors/Warnings Reflection of the Errors/Warnings in

3.6.2 Logical Errors:


S. No. Error Simulated Reflection of the error in compilation/output

4 Actual Output Obtained

School of Computing and Information Technology Page 48


Viva Questions

1. What is logical address space and physical address space?

2. What is the main function of the memory-management unit?

3. Define swapping.

4. What do you mean by best fit?

5. What do you mean by first fit?

School of Computing and Information Technology Page 49


Program 9: LRU page replacement algorithm

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.

2 Student Learning Outcomes

After successful completion of this lab, the student will able to


 The basic functionality of the memory allocation methods covered in this chapter:
paged, demand paging, segmented, and segmented/demand paged memory
allocation

 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

 The impact that virtual memory had on multiprogramming

 Cache memory and its role in improving system response time

3 Design of the Program


3.1 Theory
A good approximation to the optimal algorithm is based on the observation that pages that
have been heavily used in the last few instructions will probably be heavily used again in the
next few. Conversely, pages that have not been used for ages will probably remain unused
for a long time. This idea suggests a realizable algorithm: when a page fault occurs, throw out
the page that has been unused for the longest time. This strategy is called LRU (Least
Recently Used) paging.
3.2 Algorithm
Step 1: Start

Step 2: Declare frame, available ,count=0, n //n for number of pages

Step 3: Enter pages

Step 4: Enter page numbers

School of Computing and Information Technology Page 50


Step 5: Enter number of frames

Step 6: Print pages

Step 7: Print page numbers

Step 8: Print number of frames

Step 9: Print reference string, page frames

Step 10: count no of page faults

Step 11: print pagefaults

Step 12: stop

3.3 Coding using C Language

#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 number of pages:");

scanf("%d",&n);

printf("Enter the reference string:");

for(i=0;i<n;i++)

scanf("%d",&p[i]);

printf("Enter number 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++)
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;

School of Computing and Information Technology Page 52


}

for(r=0;r<f;r++)

if(c2[r]==b[0])

q[r]=p[i];

printf("\t%d",q[r]);

printf("\n");

printf("\nThe number of page faults is: %d",c);

}/* main */

3.4 Expected Results

[root@localhost~]# gedit lru.c


root@localhost~]# cc lru.c
[root@localhost ~]# ./a.out

Enter number of pages: 10

Enter the reference string: 7 5 9 4 3 7 9 6 2 1

Enter number of frames: 3

7 5

7 5 9

4 5 9

4 3 9

School of Computing and Information Technology Page 53


4 3 7

9 3 7

9 6 7

9 6 2

1 6 2

The number of page faults is 10

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.6 Simulation of Errors/Warnings:


3.6.1 Syntax Errors/Warnings:
S. No. Errors/Warnings Reflection of the Errors/Warnings in

School of Computing and Information Technology Page 54


3.6.2 Logical Errors:
S. No. Error Simulated Reflection of the error in compilation/output

4 Actual Output Obtained

School of Computing and Information Technology Page 55


Viva Questions
1. What is the purpose of page replacement algorithms?
2. What is meant by paging?
3. What is meant by FIFO?
4. What is meant by second-chance algorithm?
5. What is the advantage of the LRU algorithm?

School of Computing and Information Technology Page 56


Program 10: File Allocation Technique

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).

2 Student Learning Outcomes

After successful completion of this lab, the student will able to


 Understand that each file is a linked list of blocks.
 There is no external fragmentation.
 It is effective for sequential access.
 Problematic for direct access.

3 Design of the Program


3.1 Theory

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

School of Computing and Information Technology Page 57


Linked Allocation
1. Solves external fragmentation: can use any block for any file
2. solves pre-allocation internal fragmentation
3. good for sequential access: chase the pointer
4. not effective for direct access
5. where is the nth block of the file
6. requires space for pointers
7. if a pointer is bad, file is lost

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;

printf("Enter the blocks already allocated:\n");

scanf("%d",&p);

printf("\nEnter the block numbers:\n");

for(i=0;i<p;i++)

scanf("%d",&a);

f[a]=1;

X:

School of Computing and Information Technology Page 58


printf("Enter index starting block and length:\n");

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

printf("\n %d->file is already allocated",j);

k++;

printf("\nIf you want enter one more time (yes-1/no-0)");

scanf("%d",&c);

if(c==1)

goto X;

else

School of Computing and Information Technology Page 59


exit;

3.4 Expected Results

[root@localhost~]# gedit contiguous.c


root@localhost~]# cc contiguous.c
[root@localhost ~]# ./a.out

Enter theblocks already allocated:

Enter the block numbers:

10

14

Enter index starting block and length:

10

4->1

5->1

6->1

7->file is already allocated

8->1

9->file is already allocated

10->file is already allocated

School of Computing and Information Technology Page 60


11->1

12->1

13->1

14->file is already allocated

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.

3.6 Simulation of Errors/Warnings:


3.6. Syntax Errors/Warnings:
1 S. Errors/Warnings Reflection of the Errors/Warnings in

3.6. Logical Errors:


2 S. Error Simulated Reflection of the error in compilation/output

School of Computing and Information Technology Page 61


4 Actual Output Obtained

Viva Questions
1. Define seek time and latency time.

2. What are the allocation methods of a disk space?

3. What are the advantages of Contiguous allocation?

School of Computing and Information Technology Page 62


4. What are the drawbacks of contiguous allocation of disk space?

5. What are the advantages of Linked allocation?

List of Lab Assignments

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

School of Computing and Information Technology Page 63


Process ID (PPID) and also the other process which is a parent process to print its Process ID
(PID) and Parent Process ID (PPID) by using suitable function.

2. Run a Multi-threaded program in C to implement Merge Sort using Multithreaded programming


concept.

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:

A. The time quantum is 1 millisecond

B. The time quantum is 10 millisecond.

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).

9. Implement LFU page replacement algorithm.

10. Simulate Sequential file allocation strategy.

School of Computing and Information Technology Page 64

You might also like