Operating System Lab Manual
Operating System Lab Manual
Operating System Lab Manual
PREPARED BY
Ms Naveen Ahmed
DEPARTMENT OF COMPUTING
CS 313 – Operating System Lab Manual
LIST OF EXPERIMENTS/LABS
Lab No. 01: Introduction
LAB NO. 01
INTRODUCTION TO LINUX (UBUNTU) AND
COMMAND-LINE
Lab Instructor
CS 313 – Operating System Lab Manual
Lab CLOs
Objectives a b c
1
2
3
Instructions
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at LMS.
CS 313 – Operating System Lab Manual
The development of Linux is one of the most prominent examples of free and open source
software collaboration; typically all the underlying source code can be used, freely modified,
and redistributed by anyone.
Linux was developed by "Linus Torvalds" at the University of Helsinki, with the help of UNIX
programmers from across the Internet.
Linux Distributions
A typical Linux distribution comprises a Linux kernel, GNU tools and libraries, additional
software, documentation, a window system (the most common being the X Window System),
a window manager, and a desktop environment.
Most of the included software is free and open-source software made available both as compiled
binaries and in source code form, allowing modifications to the original software. Usually, Linux
distributions optionally include some proprietary software that may not be available in source
code form, such as binary blobs required for some device drivers.
Usually distributions are put on CD that contains the kernel and programming tools and utilities.
These distributions usually come with a setup program on CD to install a Linux system, they
have the same kernel but with different interfaces.
▪ Ubuntu
▪ RedHat
▪ Fedora
▪ Debian
▪ Slackware
▪ SUSE
CS 313 – Operating System Lab Manual
Directory Structure
File system: The way the files of an operating system are organized on the disk.
▪ All the files are grouped together in the directory structure. The file-system is arranged in
a hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally
called root (written as a slash /).
▪ Linux sorts directories descending from the root directory according to their importance
to the boot process.
▪ File systems from other hard drive partitions mount to directories beneath the root
directory, providing access to a single directory structure.
▪ The File system hierarchy standard (FHS) governs the unified file system for Linux by
defining a standard set of directories, sub-directories and files.
▪ Linux is a case sensitive operating system.
Directory Description
The root directory, all directories are below the / (root directory)
/
of the system.
/srv Contains the files for services like FTP and Web servers.
LINUX COMMANDS
1. pwd Command
The pwd command prints out the path of the current working directory (folder) you’re in. The
command will return an absolute (full) path, which is basically a path of all the directories that
starts with a forward slash (/).
2. cd Command
To navigate through the Linux files and directories, use the cd command. It requires either the
full path or the name of the directory, depending on the current working directory that you’re in.
Another scenario is if you want to switch to a completely new directory, for example,
/home/username/Movies. In this case, you have to type cd followed by the directory’s absolute
path: cd /home/username/Movies.
3. ls Command
The ls command is used to view the contents of a directory. By default, this command will
display the contents of the current working directory.
If we want to see the content of other directories, type ls and then the directory’s path. For
example, enter ls /home/username/Documents to view the content of Documents.
CS 313 – Operating System Lab Manual
4. mkdir Command
‘mkdir’ command creates a new directory in the current working directory.
Use mkdir command to make a new directory — if we type mkdir cs303 it will create a
directory called cs303.
● To generate a new directory inside another directory, use this Linux basic
command mkdir Music/Newfile
● use the p (parents) option to create a directory in between two existing directories. For
example, mkdir -p Music/2020/Newfile will create the new “2020” file.
5. rmdir Command
If we need to delete a directory, then we use the rmdir command. However, rmdir only allows
us to delete empty directories.
Sample output of the command is shown.
6. clear Command
‘clear’ command performs clear screen operation on the terminal.
Sample output of the command is shown.
CS 313 – Operating System Lab Manual
7. cat Command
cat (short for concatenate) is one of the most frequently used commands in Linux. It is used to
list the contents of a file on the standard output (sdout). To run this command, type cat followed
by the file’s name and its extension. For instance: cat file.txt.
cat >info.txt
Press Ctrl + D
cat info.txt
We can also concatenate the contents of multiple files into a single file
cat info.txt second.txt >third.txt
8. cp Command
The cp command is used to copy files from the current directory to a different directory.
9. mv Command
The primary use of the mv command is to move files, although it can also be used to rename
files.
The arguments in mv are similar to the cp command. We need to type mv, the file’s name, and
the destination’s directory. For example: mv file.txt /home/username/Documents.
10. rm Command
The rm command is used to delete directories and the contents within them. If we only want to
delete the directory — as an alternative to rmdir — we use rm -r.
Note: Be very careful with this command and double-check which directory you are in. This will
delete everything and there is no undo.
Syntax :
$man [OPTION]... [COMMAND NAME]...
Options and Examples
In this example, manual pages of the command ‘printf‘ are simply returned.
CS 313 – Operating System Lab Manual
2. Section-num: Since a manual is divided into multiple sections so this option is used to display
only a specific section of a manual.
Syntax :
$ man [SECTION-NUM] [COMMAND NAME]
Example:
$ man 2 intro
Output:
In this example, the manual pages of command ‘intro‘ are returned which lies in the section 2.
3. -f option: One may not be able to remember the sections in which a command is present. So
this option gives the section in which the given command is present.
Syntax:
$ man -f [COMMAND NAME]
Example:
$ man -f ls
Output:
CS 313 – Operating System Lab Manual
In this example, the command ‘ls‘ is returned with its section number.
4. -a option: This option helps us to display all the available intro manual pages in succession.
Syntax:
$ man -a [COMMAND NAME]
Example:
$ man -a intro
Output:
CS 313 – Operating System Lab Manual
In this example you can move through the manual pages (sections) i.e. either reading (by
pressing Enter) or skipping (by pressing ctrl+D) or exiting (by pressing ctrl+C).
5. -k option: This option searches the given command as a regular expression in all the manuals
and it returns the manual pages with the section number in which it is found.
Syntax:
$ man -k [COMMAND NAME]
Example:
$ man -k cd
Output:
The command ‘cd‘ is searched in all the manual pages by considering it as a regular expression.
6. -w option: This option returns the location in which the manual page of a given command is
present.
Syntax:
$ man -w [COMMAND NAME]
Example:
$ man -w ls
Output:
CS 313 – Operating System Lab Manual
Syntax:
$ man -I [COMMAND NAME]
Example:
$ man -I printf
Output:
CS 313 – Operating System Lab Manual
The command ‘printf‘ is taken as case-sensitive i.e ‘printf‘ returns the manual pages but ‘Printf‘
gives error.
▪ The sort command can also sort by items not at the beginning of the line, ignore case
sensitivity and return whether a file is sorted or not. Sorting is done based on one or more
sort keys extracted from each line of input.
▪ By default, the entire input is taken as a sort key. Blank space is the default field
separator.
1. Lines starting with a number will appear before lines starting with a letter.
2. Lines starting with a letter that appears earlier in the alphabet will appear before lines
starting with a letter that appears later in the alphabet.
3. Lines starting with a lowercase letter will appear before lines starting with the same letter
in uppercase.
Examples
Command:
$ sort file.txt
Output :
asad
chaudary
danyal
haris
CS 313 – Operating System Lab Manual
naveen
raja asim
saad
LAB TASKS
Task 1
Practice all the above Linux commands on your Ubuntu environment.
Task 2
What is the username which is logged-in?
What is the current working directory on your terminal?
Task 3
Perform following tasks.
Task 4
Perform following tasks.
▪ Create another text file with the name of teachers.txt, using cat command, having names
of all your teachers.
▪ Print the contents of both files, i.e. friends.txt and teachers.txt, in sorting form.
Task 5
Perform following tasks.
LAB NO. 02
EXECUTING C++ PROGRAM VIA SHELL
COMMANDS
Following are the lab objectives:
Lab
1. Installing C++ compiler in Ubuntu
Objectives 2. Executing C++ program using shell commands
CS 313 – Operating System Lab Manual
Student
Roll No. Name
Obtained
Marks Comments
Marks
Task 1 10
Task 2 10
Task 3 20
Total
40
Marks
Lab Instructor
Lab CLOs
Objectives a b c
1
2
3
CS 313 – Operating System Lab Manual
Instructions
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at LMS.
CS 313 – Operating System Lab Manual
Although we can install the C++ compiler separately by installation of the gcc package, the
recommended way to install the C++ compiler on Ubuntu 20.04 is by installation of the entire
development package build-essential.
$ g++ --version
Step 1: Open a cpp file using gedit by running the following command at Terminal.
$ gedit helloworld.cpp
int main()
{
cout << "Hello, World!";
return 0;
}
CS 313 – Operating System Lab Manual
Step 3: Compile and execute the C++ code by running the following command at Terminal.
Note: Before running the following commands, make sure your working directory is the same
where the cpp code file is stored.
$ g++ -c helloworld.cpp
$ ./helloworld
▪ factorial.cpp
▪ functions.h
Write the given C++ code in respective files using gedit editor.
main.cpp
The following is the code for main.cpp source file
#include <iostream>
#include "functions.h"
int main(){
print_hello();
cout << endl;
cout << "The factorial of 5 is " << factorial(5) << endl;
return 0;
}
hello.cpp
The following is the code for hello.cpp source file
#include <iostream>
#include "functions.h"
void print_hello(){
cout << "Hello World!";
}
CS 313 – Operating System Lab Manual
factorial.cpp
The following is the code for factorial.cpp source file.
#include "functions.h"
if(n!=1){
return(n * factorial(n-1));
} else return 1;
}
functions.h
The following is the code for functions.h source file.
void print_hello();
int factorial(int n);
The trivial way to compile the files and obtain an executable, is by running the command
Warning: But we’ll NOT use this process of executable file generation.
Makefile
The make command allows us to manage large programs (with multiple files) or groups of
programs. As we begin to write large programs, we notice that re-compiling large programs takes
longer time than re-compiling short programs.
Create a Makefile listing the rules for building the executable, the file should be named
‘Makefile’ or ‘makefile’. This only has to be done once, except when new modules are added to
the program, the Makefile must be updated to add new module dependencies to existing rules
and to add new rules to build the new modules.
If we run :
$ make
This program will look for a file named Makefile in your directory, and then execute it.
CS 313 – Operating System Lab Manual
Makefile Composition
The basic Makefile is composed of:
target: dependencies
[tab] system command
First Makefile
Create a file with the name Makefile, and write following commands in the file.
all:
g++ main.cpp hello.cpp factorial.cpp –o output
In this first example we see that our target is called all. This is the default target for Makefiles.
The make utility will execute this target if no other one is specified.
We also see that there are no dependencies for target all, so make safely executes the system
commands specified.
Finally, make compiles the program according to the command line we gave it. Run following
commands at Terminal.
$ make
g++ main.cpp hello.cpp factorial.cpp –o output
$ ./output
Second Makefile
Create a second file with the name Makefile-2, and write following commands in the file.
all: make-2
make-2: main.o factorial.o hello.o
g++ main.o factorial.o hello.o –o output-2
main.o: main.cpp
g++ -c main.cpp
factorial.o: factorial.cpp
g++ -c factorial.cpp
hello.o: hello.cpp
CS 313 – Operating System Lab Manual
g++ -c hello.cpp
clean:
rm –rf *o output-2
Sometimes it is useful to use different targets. This is because if you modify a single file in your
project, you don't have to recompile everything, only what you modified.
In this example we see a target called clean. It is useful to have such target if you want to have
a fast way to get rid of all the object files and executables. Run the following commands at
Terminal.
$ make –f Makefile-2
g++ -c main.cpp
g++ -c factorial.cpp
g++ -c hello.cpp
g++ main.o factorial.o hello.o –o output-2
$ ./output-2
LAB TASKS
Task 1 (10 marks)
Write a C++ program which takes the width & length of Rectangle from the user. Display area
and perimeter of Rectangle. Use Makefile to execute your code via Terminal.
Quantity Discount
1-50 No discount
Otherwise 20%
CS 313 – Operating System Lab Manual
Write a program that asks for the number of units purchased and computes the total cost of the
purchase. Use Makefile to execute your code via Terminal.
Create a class called Date that includes three pieces of information as data members; a month
(type int), a day (type int), and a year (type int). Your class should have three constructors that
initialize the three data members. For the purpose of this exercise, assume that the values
provided for the year and day are correct, but ensure that the month value is in the range 1-12; if
it isn’t, set the month to 1.
Provide setter and getter methods for each data member. Provide a member function
displayDate() that displays the month, day and year in the provided format, i.e. day – month –
year.
You need to create a header file, i.e. Date.h, which contains specification of class, creates
another file Data.cpp which contains all the implementations/definitions of methods available in
the class. Write a test program that demonstrates class Date’s capabilities. Use Makefile to
execute your code via Terminal.
CS 313 – Operating System Lab Manual
LAB NO. 03
SYSTEM CALLS
Following are the lab objectives:
Lab Instructor
CS 313 – Operating System Lab Manual
Lab CLOs
Objectives a b c
1
2
3
Instructions
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at LMS.
CS 313 – Operating System Lab Manual
SYSTEM CALL
When a computer is turned on, the program that gets executed first is called the “operating
system”. It controls pretty much all the activities in the computer. This includes who logs-in,
how disks are used, how memory is used, how the CPU is used, and how you talk with other
computers. The operating system we use is called “Linux”.
The communication between the user program and the kernel is done by system calls. System
calls can be seen as special functions, called from the user program and performed by the kernel.
In simple words, “a request for the operating system to do something on behalf of the user’s
program”.
The system calls are the fundamental interface between an application and the Linux kernel. These calls
are generally available as functions written in C and C++, although certain low-level tasks (for
example, tasks where hardware must be accessed directly) may have to be written using
assembly-language instructions.
System calls are expensive. While a procedure call can usually be performed in a few machine
instructions. Whereas, a system call requires the computer to save its state, let the operating
system take control of the CPU, have the operating system perform some function, have the
operating system save its state, and then have the operating system give control of the CPU back
to you.
1. File Manipulation/Management
2. Process Control
3. Device Management
4. Information Maintenance
5. Communications
6. Protection
The file structure related system calls available in the Linux system let us create, open, close,
read and write files, randomly access files, alias and remove files, get information about files,
check the accessibility of files, change protections, owner, and group of files, and control
devices. These operations either use a character string that defines the absolute or relative path
name of a file, or a small integer called a file descriptor that identifies the I/O channel.
Following file related system calls are being experimented in the lab:
▪ open()
▪ read()
▪ write()
▪ close()
File Descriptor
To the kernel, all open files are referred to by file descriptor. A file descriptor is a non-negative
integer. When we want to read or write a file, we identify the file with the file descriptor that is
returned by open as an argument to either read or write.
By convention, UNIX/Linux System shells associate file descriptor 0 with the standard input
of a process, file descriptor 1 with the standard output and file descriptor 2 with the standard
error.
Here,
Example:
Here,
Return value: If successful read returns the number of bytes actually read.
Example:
Here,
CS 313 – Operating System Lab Manual
Return value: If successful write() returns the number of bytes actually written.
Example:
Here,
n = close(fd);
Complete Example
Following is the complete working example of the file manipulation system calls.
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<iostream>
int main()
{
int n, fd;
char buff[50]; // declaring buffer
return 0;
}
CS 313 – Operating System Lab Manual
Process Identifier
Every process has a unique process ID, a positive integer. The process ID is the only well-known
identifier of a process that is always unique. As processes terminate, their IDs become candidate
for reuse.
Example 1:
#include <iostream>
#include <unistd.h>
using namespace std;
int main () {
fork();
cout << “hello world” << endl;
return 0;
}
See how many times the hello world will be printed.
Example 2:
#include <iostream>
#include <unistd.h>
using namespace std;
int main () {
fork();
fork();
cout << “hello world” << endl;
return 0;
}
See how many times the hello world will be printed.
Example 3:
#include <iostream>
#include <unistd.h>
using namespace std;
int main () {
int pid;
CS 313 – Operating System Lab Manual
pid = fork();
if( pid == -1 ){
cout << “Error in process generation” << endl;
}
else if( pid != 0 ){
cout << “This message is printed by child process” <<
endl;
}
else {
cout << “This message is printed by parent process” <<
endl;
}
return 0;
}
Observe the output of the program.
Here, status is the argument which we want to send to the OS for logging purposes.
Return value: Function doesn’t return any value.
Example:
#include <stdlib.h>
int main () {
exit(1); // process will exit here.
CS 313 – Operating System Lab Manual
return 0;
}
See how many times the hello world will be printed.
Here,
Return value: function will return the process ID of the child process.
Example:
#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
using namespace std;
int main () {
int pid, pid2;
int status;
pid = fork();
if( pid == -1 ){
CS 313 – Operating System Lab Manual
pid2 = wait(&status);
LAB TASKS
Task 1 (10 marks)
Write a C++ program which takes file name from user and creates the file using file
management system call. Later on, ask the user to enter the data which he/she wants to store in
the file and save the file with provided contents. Demonstrate your program using Makefile
utility.
Write a C++ program which calls fork function 2 times in main function. Print process ID of
each child process using getpid function. Explore the manual of getpid function and use it.
LAB NO. 04
CPU SCHEDULING
Following are the lab objectives:
Algorithm
Lab Instructor
CS 313 – Operating System Lab Manual
CS 313 – Operating System Lab Manual
Lab CLOs
Objectives a b c
1
2
3
Instructions
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at LMS.
CS 313 – Operating System Lab Manual
CPU SCHEDULING
CPU scheduling is the basis of multiprogrammed operating systems. By switching the CPU
among processes, the operating system can make the computer more productive. CPU scheduling
deals with the problem of deciding which of the processes in the ready queue is to be allocated to
the CPU. There are many different CPU-scheduling algorithms, like;
For better simulation of above mentioned algorithms we need to have two prerequisites;
struct process
{
int pid;
int arrivalTime;
int burstTime;
int waitTime;
int turnaroundTime;
};
CS 313 – Operating System Lab Manual
LAB TASKS
Task 1 (20 marks)
Write C++ program to simulate the First Come First Serve (FCFS) algorithm to manage
processes. For process representation struct process must be used. All processes should
be stored to the ready queue with proper information (burst time, arrival time, etc.). Later on
display the process ID, burst time, wait time, turnaround time, average wait time and
average turnaround time.
4- Find waiting time for all other processes i.e. for process i
->
total_waiting_time / no_of_processes.
CS 313 – Operating System Lab Manual
total_turn_around_time / no_of_processes.
EXTRA TASKS
Task
Write C++ program to simulate Shortest Remaining Time First (SRTF) algorithm to manage
processes. For process representation struct process must be used. All processes should
be stored to the ready queue with proper information (burst time, arrival time, etc.). Later on
display the process ID, burst time, wait time, turnaround time, average wait time and
average turnaround time.
CS 313 – Operating System Lab Manual
LAB NO. 05
CPU SCHEDULING (PREEMPTIVE
ALGORITHMS)
Following are the lab objectives:
Lab Instructor
CS 313 – Operating System Lab Manual
Lab CLOs
Objectives a b c
1
2
3
Instructions
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at LMS.
CS 313 – Operating System Lab Manual
CPU SCHEDULING
CPU scheduling is the basis of multiprogrammed operating systems. By switching the CPU
among processes, the operating system can make the computer more productive. CPU scheduling
deals with the problem of deciding which of the processes in the ready queue is to be allocated to
the CPU. There are many different CPU-scheduling algorithms, like;
For better simulation of above mentioned algorithms we need to have two prerequisites;
struct process_control_block
{
int pid;
int status;
int arrivalTime;
int burstTime;
int waitTime;
int turnaroundTime;
};
CS 313 – Operating System Lab Manual
LAB TASKS
Task 1 (20 marks)
Write C++ program to simulate Priority Scheduling algorithm to manage processes. For process
representation struct process must be used. All processes should be stored to the ready
queue with proper information (burst time, arrival time, etc.). Later on display the process ID,
burst time, wait time, turnaround time, average wait time and average turnaround time.
LAB NO. 06
REAL-TIME SCHEDULING
Following are the lab objectives:
Lab Instructor
CS 313 – Operating System Lab Manual
Lab CLOs
Objectives 1 2 3 4 5
1
2
Instructions
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at LMS
.
CS 313 – Operating System Lab Manual
REAL-TIME SCHEDULING
Real-time systems are systems that carry real-time tasks. These tasks need to be performed
immediately with a certain degree of urgency. A hard real-time task must be performed at a
specified time which could otherwise lead to huge losses. In soft real-time tasks, a specified
deadline can be missed. This is because the task can be rescheduled (or) can be completed after
the specified time,
In real-time systems, the scheduler is considered as the most important component which is
typically a short-term task scheduler. The main focus of this scheduler is to reduce the response
time associated with each of the associated processes instead of handling the deadline.
If a preemptive scheduler is used, the real-time task needs to wait until its corresponding tasks
time slice completes. In the case of a non-preemptive scheduler, even if the highest priority is
allocated to the task, it needs to wait until the completion of the current task. This task can be
slow (or) of the lower priority and can lead to a longer wait.
struct real_time_process
{
int pid;
int status;
int readyTime;
int startingTime;
int completionTime;
int processingTime;
int priority;
};
CS 313 – Operating System Lab Manual
LAB TASKS
Task (30 marks)
Write C++ program to simulate Earliest Deadline First Scheduling algorithm to manage real-
time periodic processes. For process representation struct real_time_process must be
used. All processes should be stored to the ready queue with proper information (burst time,
arrival time, etc.). Later on display the process ID, ready time, starting time, completion time,
processing time and priority.
EXTRA TASK
Task
Write C++ program to simulate Monotonic algorithms to manage real-time processes. For
process representation struct real_time_process must be used. All processes should
be stored to the ready queue with proper information (burst time, arrival time, etc.). Later on
display the process ID, ready time, starting time, completion time, processing time and
priority.
LAB NO. 07
DEADLOCK PREVENTION OR AVOIDANCE
TECHNIQUE
Following are the lab objectives:
Lab Instructor
CS 313 – Operating System Lab Manual
Lab CLOs
Objectives 1 2 3 4 5
1
2
Instructions
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at LMS.
CS 313 – Operating System Lab Manual
1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
a) Finish[i] = false
Finish[i] = true
CS 313 – Operating System Lab Manual
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:
Goto step (2) ; otherwise, raise an error condition, since the process has exceeded its
maximum claim.
Goto step (3); otherwise, Pi must wait, since the resources are not available.
3) Have the system pretend to have allocated the requested resources to process Pi by
modifying the state as follows:
LAB TASKS
Task 01 (15 marks)
Write C++ program to simulate Banker’s Algorithms for Deadlock avoidance.
Alloc Request
X Y Z X Y Z
P0 1 2 1 1 0 3
P1 2 0 1 0 1 2
P2 2 2 1 1 2 0
LAB NO. 08
PROCESS SYNCHRONIZATION
CRITICAL SECTION PROBLEM
Following are the lab objectives:
Lab Instructor
CS 313 – Operating System Lab Manual
Lab CLOs
Objectives a b c
1
2
3
Instructions
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at LMS.
CS 313 – Operating System Lab Manual
In the entry section, the process requests for entry in the Critical Section.
A simple solution to the critical section can be thought as shown below,
acquireLock();
Process Critical Section
releaseLock();
Peterson’s Solution
Peterson’s Solution is a classical software based solution to the critical section problem.
In Peterson’s solution, we have two shared variables:
● boolean flag[i] :Initialized to FALSE, initially no one is interested in entering
the critical section
● int turn : The process whose turn is to enter the critical section.
CS 313 – Operating System Lab Manual
PROCESS SYNCHRONIZATION
We use semaphores for synchronization, since that is the traditional way to present
such solutions.
wait(S){
S--;
CS 313 – Operating System Lab Manual
signal(S){
S++;
LAB TASKS
Task 01: (15 marks)
Write a C++ program to simulate Peterson’s solution for the critical section problem.
LAB NO. 09
PROCESS SYNCHRONIZATION
MUTUAL EXCLUSION ALGORITHM
SEMAPHORE
Following are the lab objectives:
Lab Instructor
CS 313 – Operating System Lab Manual
Lab CLOs
Objectives a b c
1
2
3
Instructions
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at LMS.
CS 313 – Operating System Lab Manual
1. Dining-Philosophers Problem:
The Dining Philosopher Problem states that K philosophers are seated around a
circular table with one chopstick between each pair of philosophers. There is one
chopstick between each philosopher.
process P[i]
while true do
THINK;
EAT;
do {
wait(mutex);
readcnt++;
if (readcnt==1)
wait(wrt);
signal(mutex);
readcnt--;
if (readcnt == 0)
} while(true);
CS 313 – Operating System Lab Manual
LAB TASKS
LAB NO. 10
MEMORY MANAGEMENT TECHNIQUES
Following are the lab objectives:
1. To simulate the MFT memory management
techniques.
Lab 2. To simulate the MVT memory management
Objectives techniques.
Lab Instructor
CS 313 – Operating System Lab Manual
Lab CLOs
Objectives a b c
1
2
3
Instructions
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at LMS.
Memory management keeps track of the status of each memory location, whether it is
allocated or free.
MFT (Multiprogramming with a Fixed number of Tasks):
CS 313 – Operating System Lab Manual
MFT (Multiprogramming with a Fixed number of Tasks) is one of the old memory
management techniques in which the memory is partitioned into fixed size partitions and
each job is assigned to a partition.
if(pn[i]==pn[j])
f=1;
if(f==0){
if(ps[i]<=siz)
extft=extft+size-ps[i];
avail[i]=1;
count++;
if(pn[i]==pn[j])
f=1;
if(f==0)
if(ps[i]<=size )
extft=extft+size-ps[i];
avail[i]=1;
count++;
LAB TASKS
CS 313 – Operating System Lab Manual
LAB NO. 11
MEMORY MANAGEMENT TECHNIQUES
Following are the lab objectives:
1. To simulate the First Fit contiguous memory allocation
techniques.
2. To simulate the Best Fit contiguous memory allocation
Lab
techniques.
Objectives
3. To simulate the Worst Fit contiguous memory allocation
techniques
Lab Instructor
CS 313 – Operating System Lab Manual
Lab CLOs
Objectives a b c
1
2
3
Instructions
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at LMS.
CS 313 – Operating System Lab Manual
Implementation
1.Input memory blocks with size and processes with size.
2.Initialize all memory blocks as free.
3.Start by picking each process and check if it can
be assigned to the current block.
4.If size-of-process <= size-of-block if yes then
assign and check for the next process.
5.If not then keep checking the further blocks.
2. Best Fit Allocates the process to the partition which is the first smallest sufficient
partition among the free available partitions.
CS 313 – Operating System Lab Manual
Implementation:
1.Input memory blocks and processes with sizes.
2. Initialize all memory blocks as free.
3.Start by picking each process and find the
minimum block size that can be assigned to
current process i.e., find min(bockSize[1],
blockSize[2],.....blockSize[n]) >
processSize[current], if found then assign
to the current process.
4. If not then leave that process and keep checking
the further processes.
3.Worst Fit: Allocates the process to the partition which is the largest sufficient
among the freely available partitions available in the main memory..
CS 313 – Operating System Lab Manual
Implementation:
1- Input memory blocks and processes with sizes.
2- Initialize all memory blocks as free.
3- Start by picking each process and find the
maximum block size that can be assigned to
current process i.e., find max(bockSize[1],
blockSize[2],.....blockSize[n]) >
processSize[current], if found then assign
to the current process.
5- If not then leave that process and keep checking
the further processes.
4. Next Fit: Next fit is similar to the first fit but it will search for the first sufficient
partition from the last allocation point.
CS 313 – Operating System Lab Manual
LAB TASKS
Task 1 (15 marks)
Write C++ program to simulate First Fit contiguous memory allocation techniques
LAB NO. 12
Segmentation MEMORY MANAGEMENT
TECHNIQUES
Following are the lab objectives:
1. To simulate two-level paging technique of memory
management.
Lab 2. To simulate segmentation memory management technique.
Objectives
Lab Instructor
CS 313 – Operating System Lab Manual
Lab CLOs
Objectives a b c
1
2
3
Instructions
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at LMS.
CS 313 – Operating System Lab Manual
Example:
Physical Address Space = 2(44) B
Page Entry = 4B
Segmentation:
A process is divided into Segments. The chunks that a program is divided into which are
not necessarily all of the same sizes are called segments.
Example:
A solution to the problem is to use segmentation along with paging to reduce the
size of the page table. Traditionally, a program is divided into four segments:
code,data stack,heap.
CS 313 – Operating System Lab Manual
LAB TASKS
Task (15 marks)
Write C++ program to simulate two-level paging technique using the above example.
LAB NO. 13
PAGE FAULTS
Following are the lab objectives:
1. To simulate the First In First Out (FIFO) Page
replacement algorithm.
Lab 2. To simulate the Optimal Page replacement algorithm.
Objectives 3. To simulate the Least Recently Used replacement
algorithm.
Lab Instructor
CS 313 – Operating System Lab Manual
Lab CLOs
Objectives a b c
1
2
3
Instructions
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at LMS.
CS 313 – Operating System Lab Manual
Page Fault – A page fault happens when a running program accesses a memory page that
is mapped into the virtual address space, but not loaded in physical memory.
Page Replacement Algorithms :
1. First In First Out (FIFO) :
This is the simplest page replacement algorithm. In this algorithm, the operating system
keeps track of all pages in the memory in a queue, the oldest page is in the front of the
queue.
Example: Consider page reference string 1, 3, 0, 3, 5, 6 with 3 page frames.Find number
of page faults.
Implementation – Let capacity be the number of pages that memory can hold. Let set be
the current set of pages in memory.
1- Start traversing the pages.
i) If the set holds less pages than capacity.
a) Insert page into the set one by one until
the size of set reaches capacity or all
page requests are processed.
b) Simultaneously maintain the pages in the
queue to perform FIFO.
c) Increment page fault
ii) Else
If the current page is present in set, do nothing.
Else
a) Remove the first page from the queue
as it was the first to be entered in
the memory
b) Replace the first page in the queue with
the current page in the string.
CS 313 – Operating System Lab Manual
LAB TASKS
Task 01: (15 marks)
Write C++ program to simulate First in First Out (FIFO) Page Replacement Algorithm using the
above example.
LAB NO. 14
STORAGE MANAGEMENT AND FILE SYSTEM
Following are the lab objectives:
1. To simulate file organization techniques in Single level
directory.
Lab 2. To simulate file organization techniques in Two level
Objectives directory.
Lab Instructor
CS 313 – Operating System Lab Manual
Lab CLOs
Objectives a b c
1
2
3
Instructions
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at the Learning Management System (LMS).
CS 313 – Operating System Lab Manual
Implementation:
1:Start
2: Initialize values gd=DETECT,gm,count,i,j,mid,cir_x;
Initialize character array fname[10][20];
3: Initialize graph function as
Initgraph(& gd, &gm," c:/tc/bgi");
Clear device();
4:set background color with setbkcolor();
5:read the number of files in variable count.
6:if check i<count
7: for i=0 & i<count
i increment;
Cleardevice();
setbkcolor(GREEN);
read file name;
setfillstyle(1,MAGENTA);
8: mid=640/count;
cir_x=mid/3;
bar3d(270,100,370,150,0,0);
settextstyle(2,0,4);
settextstyle(1,1);
outtextxy(320,125,"rootdirectory");
setcolor(BLUE);
i++;
9:for j=0&&j<=i&&cir_x+=mid
j increment;
line(320,150,cir_x,250);
CS 313 – Operating System Lab Manual
fillellipse(cir_x,250,30,30);
outtextxy(cir_x,250,fname[i]);
10: End
Two-Level Directory:
In the two-level directory structure, each user has their own user files directory (UFD).
The UFDs have similar structures, but each lists only the files of a single user. System's
master file directory (MFD) is searched whenever a new user id=s logged in.
Lab Task 01:. Write C++ program to simulate Single level directory algorithm using the above
example. (10)
Lab Task 02:Write C++ program to simulate Two- level directory algorithm using the above
example. (10)
Extra Task: Write a C++ program to simulate a two-level directory using the example given
below.
CS 313 – Operating System Lab Manual