Lab Manual-2
Lab Manual-2
LABORATORY MANUAL
OPERATING SYSTEMS LAB
BE V Semester (AICTE Model Curriculum): 2020-21
NAME: ___________________________________________________________________
ROLL NO:__________________________________________________________________
BRANCH:__________________ SEM:______________________________________
VISION
To produce ethical, socially conscious and innovative professionals who
would contribute to sustainable technological development of the society.
MISSION
To impart quality engineering education with latest technological
developments and interdisciplinary skills to make students succeed in
professional practice.
DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING
LABORATORY MANUAL
Prepared
By
Dr. P. Lavanya,
Professor & Head of Department.
METHODIST
Estd:2008 COLLEGE OF ENGINEERING AND TECHNOLOGY
VISION
MISSION
PEO1: Apply technical concepts, Analyze, Synthesize data to Design and create
novel products and solutions for the real life problems.
PROGRAM OUTCOMES
Engineering graduates will be able to:
PO2: Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
PO3: Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
PO4: Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of
the information to provide valid conclusions.
PO5: Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modelling to complex engineering activities
with an understanding of the limitations.
PO6: The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to
the professional engineering practice.
PO7: Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need
for sustainable development.
PO8: Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
PO9: Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
PO10: Communication: Communicate effectively on complex engineering activities with the
Engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
PO11: Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
PO12: Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
At the end of 4 years, Computer Science and Engineering graduates at MCET will be able to:
PSO1: Apply the knowledge of Computer Science and Engineering in various domains like networking
and data mining to manage projects in multidisciplinary environments.
PSO3: Design and develop solutions by following standard software engineering principles and
implement by using suitable programming languages and platforms
Course
Course Title Core / Elective
Code
– – – – 2 25 50 2
Course Objectives:
To learn shell programming and the use of filters in the LINUX environment.
To practice multithreaded programming.
To implement CPU Scheduling Algorithms and memory management algorithms.
Course Outcomes:
Evaluate the performance of different types of CPU scheduling algorithms.
Implement producer-consumer problem, reader-writers problem, Dining philosopher’s
problem.
Simulate Banker’s algorithm for deadlock avoidance.
Implement paging replacement and disk scheduling techniques.
Use different system calls for writing application programs.
I. CASE STUDY
Perform a case study by installing and exploring various types of operating systems on a
physical or logical (virtual) machine.
II. List of Experiments (preferred programming language is C)
1. Write a C programs to implement UNIX system calls and file management.
2. Write C programs to demonstrate various process related concepts.
3. Write C programs to demonstrate various thread related concepts.
4. Write C programs to simulate CPU scheduling algorithms: FCFS, SJF, and Round Robin.
5. Write C programs to simulate Intra & Inter – Process Communication (IPC) techniques:
Pipes, Messages Queues, and Shared Memory.
6. Write C programs to simulate solutions to Classical Process Synchronization Problems:
Dining Philosophers, Producer – Consumer, Readers – Writers.
7. Write a C program to simulate Bankers Algorithm for Deadlock Avoidance.
8. Write C programs to simulate Page Replacement Algorithms: FIFO, LRU.
9. Write C programs to simulate implementation of Disk Scheduling Algorithms: FCFS, SSTF.
METHODIST
Estd:2008 COLLEGE OF ENGINEERING AND TECHNOLOGY
SEMESTER: V
Taxonomy
CO No. Course Outcome
Level
Evaluate the performance of different types of CPU scheduling Evaluating
PC532CS.1
algorithms.
Implement producer – consumer problem, reader – writers Applying
PC532CS.2
problem, Dining philosopher’s problem.
PC532CS.5 Use different system calls for writing application programs. Applying
a. Laboratory observation notes with all the details (Problem statement, Aim, Algorithm,
Procedure, Program, Expected Output, etc.,) filled in for the lab session.
b. Laboratory Record updated up to the last session experiments.
c. Formal dress code and Identity card.
4. Sign in the laboratory login register, write the TIME-IN, and occupy the computer system
allotted to you by the faculty.
5. Execute your task in the laboratory, and record the results / output in the lab observation note
book, and get certified by the concerned faculty.
6. All the students should be polite and cooperative with the laboratory staff, must maintain the
discipline and decency in the laboratory.
7. Computer labs are established with sophisticated and high end branded systems, which should
be utilized properly.
8. Students / Faculty must keep their mobile phones in SWITCHED OFF mode during the lab
sessions. Misuse of the equipment, misbehaviours with the staff and systems etc., will attract
severe punishment.
9. Students must take the permission of the faculty in case of any urgency to go out. If anybody
found loitering outside the lab / class without permission during working hours will be treated
seriously and punished appropriately.
10. Students should SHUT DOWN the computer system before he/she leaves the lab after
completing the task (experiment) in all aspects. He/she must ensure the system / seat is kept
properly.
All students must observe the dress code while in the laboratory
Footwear is NOT allowed
Foods, drinks and smoking are NOT allowed
All bags must be left at the indicated place
The lab timetable must be strictly followed
Be PUNCTUAL for your laboratory session
All programs must be completed within the given time
Noise must be kept to a minimum
Workspace must be kept clean and tidy at all time
All students are liable for any damage to system due to their own negligence
Students are strictly PROHIBITED from taking out any items from the laboratory
Report immediately to the lab programmer if any damages to equipment
Lab In – charge
METHODIST
Estd:2008 COLLEGE OF ENGINEERING AND TECHNOLOGY
LIST OF EXPERIMENTS
7. Writers.
Write a C program to simulate 62
Bankers Algorithm for Deadlock
Avoidance.
ADDITIONAL EXPERIMENTS
PROGRAM I(CASE STUDY): Perform a case study by installing and exploring various types
of operating systems on a physical or logical (virtual) machine. (Linux Installation).
Instructions to Install Ubuntu Linux 12.04 (LTS) along with Windows
Back Up Your Existing Data!
This is highly recommended that you should take backup of your entire data before start
with the installation process.
Obtaining System Installation Media
Download latest Desktop version of Ubuntu from this link:
http://www.ubuntu.com/download/desktop
Booting the Installation System
There are several ways to boot the installation system. Some of the very popular ways are ,
Booting from a CD ROM, Booting from a USB memory stick, and Booting from TFTP.
Here we will learn how to boot installation system using a CD ROM.
Before booting the installation system, one need to change the boot order and set CD-ROM
as first boot device.
Changing the Boot Order of a Computers
As your computer starts, press the DEL, ESC, F1, F2, F8 or F10 during the initial startup screen.
Depending on the BIOS manufacturer, a menu may appear. However, consult the hardware
documentation for the exact key strokes. In my machine, its DEL key as shown in following screen-
shot.
2. Find the Boot option in the setup utility. Its location depends on your BIOS.
Select the Boot option from the menu, you can now see the options Hard Drive, CD-ROM
Drive, Removable Devices Disk etc.
Page 1
MCET OPERATING SYSTEMS LAB MANUAL
3. Change the boot sequence setting so that the CD-ROM is first. See the list of “Item Specific
Help” in right side of the window and find keys which is used to toggle to change the boot
sequence.
In a few minutes installation wizard will be started. Select your language and click the
"Install Ubuntu" button to continue...
Page 2
MCET OPERATING SYSTEMS LAB MANUAL
Optionally, you can choose to download updates while installing and/or install third party
software, such as MP3 support. Be aware, though, that if you select those options, the entire
installation process will be longer!
Since we are going to create partitions manually, select Something else, then click
Continue. Keep in mind that even if you do not want to create partitions manually, it is better to
select the same option as indicated here. This would insure that the installer will not overwrite your
Windows , which will destroy your data. The assumption here is that sdb will be used just for
Ubuntu 12.04, and that there are no valuable data on it.
Page 3
MCET OPERATING SYSTEMS LAB MANUAL
Where are you? Select your location and Click the "Continue" button.
Keyboard layout
Select your keyboard layout and UK (English) and Click on “Continue” button.
Page 4
MCET OPERATING SYSTEMS LAB MANUAL
Now Ubuntu 12.04 LTS (Precise Pangolin) operating system will be installed.
Page 5
MCET OPERATING SYSTEMS LAB MANUAL
Please remove the CD and press the "Enter" key to reboot. The computer will be restarted.
In a few seconds, you should see Windows 7′s boot menu with two entires listed – Windows 7 and
Ubuntu 12.04 (LTS).
Then you may choose to boot into Windows 7 or Ubuntu 12.04 using the UP/Down arrow key.
Please select Ubuntu 12.04 (LTS) and press Enter to boot the machine in Ubuntu 12.04
Linux.
Page 6
MCET OPERATING SYSTEMS LAB MANUAL
Here you can see the users on the machine, Click on the user name and enter the password
and press Enter key to login.
Page 7
MCET OPERATING SYSTEMS LAB MANUAL
The interface between a process and an operating system is provided by system calls. In
general, system calls are available as assembly language instructions. They are also included in the
manuals used by the assembly level programmers. System calls are usually made when a process in
user mode requires access to a resource. Then it requests the kernel to provide the resource via a
system call.
As can be seen from this diagram, the processes execute normally in the user mode until a
system call interrupts this. Then the system call is executed on a priority basis in the kernel mode.
After the execution of the system call, the control returns to the user mode and execution of user
processes can be resumed.
If a file system requires the creation or deletion of files. Reading and writing from files also
require a system call.
Network connections also require system calls. This includes sending and receiving packets.
Access to a hardware devices such as a printer, scanner etc. requires a system call.
Page 8
MCET OPERATING SYSTEMS LAB MANUAL
There are mainly five types of system calls. These are explained in detail as follows −
Process Control
These system calls deal with processes such as process creation, process termination etc.
File Management
These system calls are responsible for file manipulation such as creating a file, reading a
file, writing into a file etc.
Device Management
These system calls are responsible for device manipulation such as reading from device
buffers, writing into device buffers etc.
Information Maintenance
These system calls handle information and its transfer between the operating system and the
user program.
Communication
These system calls are useful for interprocess communication. They also deal with creating
and deleting a communication connection.
Some of the examples of all the above types of system calls in Windows and Unix are given
as follows −
CreateProcess() fork()
Process Control ExitProcess() exit()
WaitForSingleObject() wait()
CreateFile() open()
ReadFile() read()
File Management
WriteFile() write()
CloseHandle() close()
Page 9
MCET OPERATING SYSTEMS LAB MANUAL
WriteConsole() write()
GetCurrentProcessID() getpid()
Information Maintenance SetTimer() alarm()
Sleep() sleep()
CreatePipe() pipe()
Communication CreateFileMapping() shmget()
MapViewOfFile() mmap()
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables pid and child id.
Step 3: Get the child id value using system call fork().
Step 4: If child id value is greater than zero then print as “i am in the parent process”.
Step 5: If child id! = 0 then using getpid() system call get the process id.
Step 6: Print “i am in the parent process” and print the process id.
Step 7: If child id! = 0 then using getppid() system call get the parent process id.
Step 8: Print “i am in the parent process” and print the parent process id.
Step 9: Else If child id value is less than zero then print as “i am in the child process”.
Step 10: If child id! = 0 then using getpid() system call get the process id.
Step 11: Print “i am in the child process” and print the process id.
Step 12: If child id! = 0 then using getppid() system call get the parent process id.
Step 13: Print “i am in the child process” and print the parent process id.
Step 14: Stop the program.
Page 10
MCET OPERATING SYSTEMS LAB MANUAL
PROGRAM :
SOURCE CODE:
Page 11
MCET OPERATING SYSTEMS LAB MANUAL
AIM:To write the program to implement the system calls wait ( ) and exit ( ).
DESCRIPTION:
i. fork ( )
Used to create new process. The new process consists of a copy of the address space of the
original process. The value of process id for the child process is zero, whereas the value of process
id for the parent is an integer value greater than zero.
Syntax: fork ( );
ii. wait ( )
The parent waits for the child process to complete using the wait system call. The wait system
call returns the process identifier of a terminated child, so that the parent can tell which of its
possibly many children has terminated.
Syntax: wait (NULL);
iii. exit ( )
A process terminates when it finishes executing its final statement and asks the operating
system to delete it by using the exit system call. At that point, the process may return data (output)
to its parent process (via the wait system call).
Syntax: exit (0);
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables pid and i as integers.
Step 3: Get the child id value using the system call fork ().
Step 4: If child id value is less than zero then print “fork failed”.
Step 5: Else if child id value is equal to zero, it is the id value of the child and then start the child
process to execute and perform Steps 7 & 8.
Step 6: Else perform Step 9.
Step 7: Use a for loop for almost five child processes to be called.
Step 8: After execution of the for loop then print “child process ends”.
Step 9: Execute the system call wait ( ) to make the parent to wait for the child process to get over.
Step 10: Once the child processes are terminated, the parent terminates and hence prints “Parent
process ends”.
Step 11: After both the parent and the child processes get terminated it execute the wait ( ) system
call to permanently get deleted from the OS.
Step 12: Stop the program.
Page 12
MCET OPERATING SYSTEMS LAB MANUAL
PROGRAM:
Page 13
MCET OPERATING SYSTEMS LAB MANUAL
OUTPUT:
$ vi waitexit.c
$ cc waitexit.c
$ ./a.out
Child process starts
Child process 0 is called
Child process 1 is called
Child process 2 is called
Child process 3 is called
Child process 4 is called
Child process ends
Parent process ends
RESULT:
Thus the program was executed and verified successfully
Page 14
MCET OPERATING SYSTEMS LAB MANUAL
Page 15
MCET OPERATING SYSTEMS LAB MANUAL
$ vi wait.c
$ cc wait.c
$ ./a.out
CHILD: This is the child process!
CHILD: My PID is 3821
CHILD: My parent's PID is 3820
CHILD: Enter my exit status (make it small):
CHILD: I'm outta here!
PARENT: This is the parent process!
PARENT: My PID is 3820
10
PARENT: My child's PID is 3821
PARENT: I'm now waiting for my child to exit()...
PARENT: My child's exit status is: 10
PARENT: I'm outta here!
RESULT:
Thus the program was executed and verified successfully
Page 16
MCET OPERATING SYSTEMS LAB MANUAL
Page 17
MCET OPERATING SYSTEMS LAB MANUAL
OUTPUT:
$ vi execl.c
$ cc execl.c
$ ./a.out
Before execl
a1 aaa aaa.txt abc a.out b1 b2 comm.c db db1 demo2 dir1 direc.c execl.c f1.txt fflag.c
file1 file2 fork.c m1 m2 wait.c xyz
RESULT:
Thus the program was executed and verified successfully
Page 18
MCET OPERATING SYSTEMS LAB MANUAL
Page 19
MCET OPERATING SYSTEMS LAB MANUAL
Page 20
MCET OPERATING SYSTEMS LAB MANUAL
ALGORITHM:
}
if((dirp=opendir(argv[1]))==NULL)
{
perror("Failed to open directory \n");
return 1;
}
while((direntp=readdir(dirp))!=NULL)
printf("%s\n",direntp->d_name);
while((closedir(dirp)==-1)&&(errno==EINTR));
return 0;
}
Page 21
MCET OPERATING SYSTEMS LAB MANUAL
OUTPUT:
$ vi direc.c
$ cc direc.c
$ ./a.out ./
f1.txt
fflag.c
.
..
b2
comm.c
dir1
demo2
a1
m1
db
a.out
direc.c
db1
file1
b1
xyz
abc
aaa
file2
aaa.txt
m2
RESULT:
Thus the program was executed and verified successfully.
Page 22
MCET OPERATING SYSTEMS LAB MANUAL
AIM:To write the program to implement the system calls open ( ), read ( ), write ( ) & close ().
DESCRIPTION:
The file structure related system calls available in the UNIX system let you create, open, and
close files, 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. A channel is
a connection between a process and a file that appears to the process as an unformatted stream of
bytes. The kernel presents and accepts data from the channel as a process reads and writes that
channel. To a process then, all input and output operations are synchronous and unbuffered.
SYSTEM CALLS USED:
System calls are functions that a programmer can call to perform the services of the operating
system.
Open ():
Open () system call to open a file.
open () returns a file descriptor, an integer specifying the position of this open n file in the
table of open files for the current process.
Close ():
Close () system call to close a file.
Read ():
Read () data from a file opened for reading.
Write ():
Write () data to a file opened for writing.
The open () system call:
#include<fcntl.h>
int open (const char *path, int oflag);
The return value is the descriptor of the file. Returns -1 if the file could not be opened. The
first parameter is path name of the file to be opened and the second parameter is the opening mode
specified by bitwise oring one or more of the following values
Page 23
MCET OPERATING SYSTEMS LAB MANUAL
Value Meaning
O_RDONLY Open for reading only
O_WRONLY Open for writing only
O_RDWR Open for reading and writing
O_APPEND Open at end of file for writing
O_CREAT Create the file if it doesn't already exist
O_EXCL If set and O_CREAT set will cause open() to fail if the file already exists
O_TRUNC Truncate file size to zero if it already exists
Page 24
MCET OPERATING SYSTEMS LAB MANUAL
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the structure elements.
Step 3: Create a temporary file named temp1.
Step 4: Open the file named “test” in a write mode.
Step 5: Enter the strings for the file.
Step 6: Write those strings in the file named “test”.
Step 7: Create a temporary file named temp2.
Step 8: Open the file named “test” in a read mode.
Step 9: Read those strings present in the file “test” and save it in temp2.
Step 10: Print the strings which are read.
Step 11: Stop the program.
PROGRAM :
SOURCE CODE:
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h>
int main( )
{
int fd[2];
char buf1[25]= ”just a test\n”; char
buf2[50];
fd[0]=open(“file1”, O_RDWR);
fd[1]=open(“file2”, O_RDWR);
write(fd[0], buf1, strlen(buf1));
printf(“\n Enter the text now….”);
gets(buf1);
write(fd[0], buf1, strlen(buf1));
lseek(fd[0], SEEK_SET, 0);
read(fd[0], buf2, sizeof(buf1));
write(fd[1], buf2, sizeof(buf2));
close(fd[0]);
close(fd[1]);
printf(“\n”);
Page 25
MCET OPERATING SYSTEMS LAB MANUAL
return0;
}
OUTPUT:
Enter the text now….progress
Cat file1 Just a
test progress
Cat file2 Just a test progress
RESULT:
Thus the program was executed successfully.
Page 26
MCET OPERATING SYSTEMS LAB MANUAL
The concept of multi-threading needs proper understanding of these two terms – a process
and a thread. A process is a program being executed. A process can be further divided into
independent units known as threads.
A thread is like a small light-weight process within a process. Or we can say a collection of
threads is what is known as a process.
Applications –
Threading is used widely in almost every field.
Most widely it is seen over the internet now days where we are using transaction processing
of every type like recharges, online transfer, banking etc.
Threading is a segment which divide the code into small parts that are of very light weight
and has less burden on CPU memory so that it can be easily worked out and can achieve
goal in desired field.
The concept of threading is designed due to the problem of fast and regular changes in
technology and less the work in different areas due to less application
Page 27
MCET OPERATING SYSTEMS LAB MANUAL
PROGRAM :
SOURCE CODE:
/* Implementing a program using thread */
#include<pthread.h>
#include<stdio.h>
#define NUM_THREADS 3
int je,jo,evensum=0,sumn=0,oddsum=0,evenarr[50],oddarr[50];
void *Even(void *threadid)
{
int i,n;
je=0;
n=(int)threadid;
for(i=1;i<=n;i++)
{
if(i%2==0)
{
evenarr[je]=i;
evensum=evensum+i;
je++;
}
}
}
void *Odd(void *threadid)
{
int i,n;
jo=0;
n=(int)threadid;
for(i=0;i<=n;i++)
{
if(i%2!=0)
{
oddarr[jo]=i;
oddsum=oddsum+i;
jo++;
}
Page 28
MCET OPERATING SYSTEMS LAB MANUAL
}
}
void *SumN(void *threadid)
{
int i,n;
n=(int)threadid;
for(i=1;i<=n;i++)
{
sumn=sumn+i;
}
}
int main()
{
pthread_t threads[NUM_THREADS];
int i,t;
printf("Enter a number\n");
scanf("%d",&t);
pthread_create(&threads[0],NULL,Even,(void *)t);
pthread_create(&threads[1],NULL,Odd,(void *)t);
pthread_create(&threads[2],NULL,SumN,(void *)t);
for(i=0;i<NUM_THREADS;i++)
{
pthread_join(threads[i],NULL);
}
printf("The sum of first N natural nos is %d\n",sumn);
printf("The sum of first N even natural nos is %d\n",evensum);
printf("The sum of first N odd natural nos is %d\n",oddsum);
printf("The first N even natural nos is----\n");
for(i=0;i<je;i++)
printf("%d\n",evenarr[i]);
printf("The first N odd natural nos is----\n");
for(i=0;i<jo;i++)
printf("%d\n",oddarr[i]);
pthread_exit(NULL);
}
Page 29
MCET OPERATING SYSTEMS LAB MANUAL
OUTPUT:
$ vi threadf.c
$ cc threadf.c -pthread
$ ./a.out
Enter a number
12
The sum of first N natural nos is 78
The sum of first N even natural nos is 42
The sum of first N odd natural nos is 36
The first N even natural nos is----
2
4
6
8
10
12
The first N odd natural nos is----
1
3
5
7
9
11
RESULT:
Thus the program was executed and verified successfully.
Page 30
MCET OPERATING SYSTEMS LAB MANUAL
3.AIM :Write C programs to simulate CPU scheduling algorithms: FCFS, SJF, and Round
Robin.
CPU Scheduling Algorithms:
Scheduling of processes/work is done to finish the work on time.
Below are different times with respect to a process.
Arrival Time : Time at which the process arrives in the ready queue.
Completion Time : Time at which process completes its execution.
Burst Time : Time required by a process for CPU execution.
Turn Around Time : Time Difference between completion time and arrival time.
Turn Around Time = Completion Time - Arrival Time
Waiting Time(W.T) : Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time - Burst Time
Why do we need scheduling?
A typical process involves both I/O time and CPU time. In a uniprogramming system like MS-
DOS, time spent waiting for I/O is wasted and CPU is free during this time. In multiprogramming
systems, one process can use CPU while another is waiting for I/O. This is possible only with
process scheduling.
Objectives of Process Scheduling Algorithm
Max CPU utilization [Keep CPU as busy as possible]
Fair allocation of CPU.
Max throughput [Number of processes that complete their execution per time unit]
Min turnaround time [Time taken by a process to finish execution]
Min waiting time [Time a process waits in ready queue]
Min response time [Time when a process produces first response]
Different Scheduling Algorithms:
First Come First Serve (FCFS): Simplest scheduling algorithm that schedules according to arrival
times of processes. First come first serve scheduling algorithm process that requests the CPU first is
allocated the CPU first. It is implemented by using the FIFO queue. When a process enters the
ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated to the
process at the head of the queue. The running process is then removed from the queue. FCFS is a
non – preemptive scheduling algorithm.
Note: First come first serve suffers from convoy effect.
Shortest Job First (SJF): Process which has the shortest burst time is scheduled first. If two
processes have the same bust time then FCFS is used to break the tie. It is a non-preemptive
scheduling algorithm.
Page 31
MCET OPERATING SYSTEMS LAB MANUAL
Longest Job First (LJF): It is similar to SJF scheduling algorithm. But, in this scheduling
algorithm, we give priority to the process having the longest burst time. This is non – preemptive in
nature i.e., when any process starts executing, can’t be interrupted before complete execution.
Shortest Remaining Time First (SRTF): It is preemptive mode of SJF algorithm in which jobs are
schedule according to shortest remaining time.
Longest Remaining Time First (LRTF): It is preemptive mode of LJF algorithm in which we give
priority to the process having largest burst time remaining.
Round Robin Scheduling: Each process is assigned a fixed time (Time Quantum/Time Slice) in
cyclic way. It is designed especially for the time-sharing system. The ready queue is treated as a
circular queue. The CPU scheduler goes around the ready queue, allocating the CPU to each
process for a time interval of up to 1-time quantum. To implement Round Robin scheduling, we
keep the ready queue as a FIFO queue o£ processes. New processes are added to the tail of the
ready queue. The CPU scheduler picks the first process from the ready queue, sets a timer to
interrupt after 1-time quantum, and dispatches the process. One of two things will then happen. The
process may have a CPU burst of less than 1-time quantum. In this case, the process itself will
release the CPU voluntarily. The scheduler will then proceed to the next process in the ready queue.
Otherwise, if the CPU burst of the currently running process is longer than 1-time quantum, the
timer will go off and will cause an interrupt to the operating system. A context switch will be
executed, and the process will be put at the tail o£ the ready queue. The CPU scheduler will then
select the next process in the ready queue.
Priority Based scheduling (Non - Preemptive): In this scheduling, processes are scheduled
according to their priorities, i.e., highest priority process is scheduled first. If priorities of two
processes match, then schedule according to arrival time. Here starvation of process is possible.
Highest Response Ratio Next (HRRN) In this scheduling, processes with highest response ratio is
scheduled. This algorithm avoids starvation.
Response Ratio = (Waiting Time + Burst time) / Burst time
Multilevel Queue Scheduling: According to the priority of process, processes are placed in the
different queues. Generally high priority process is placed in the top level queue. Only after
completion of processes from top level queue, lower level queued processes are scheduled. It can
suffer from starvation.
Multi level Feedback Queue Scheduling: It allows the process to move in between queues. The
idea is to separate processes according to the characteristics of their CPU bursts. If a process uses
too much CPU time, it is moved to a lower-priority queue.
Page 32
MCET OPERATING SYSTEMS LAB MANUAL
P0 0-0=0
P1 5-1=4
P2 8-2=6
P3 16 - 3 = 13
Page 33
MCET OPERATING SYSTEMS LAB MANUAL
ALGORITHM:
Step 1: Start the program.
Step 2: Create the number of process.
Step 3: Get the ID and Service time for each process.
Step 4: Initially, Waiting time of first process is zero and Total time for the first process is the
starting time of that process.
Step 5: Calculate the Total time and Processing time for the remaining processes.
Step 6: Waiting time of one process is the Total time of the previous process.
Step 7: Total time of process is calculated by adding Waiting time and Service time.
Step 8: Total waiting time is calculated by adding the waiting time for lack process.
Step 9: Total turn around time is calculated by adding all total time of each process.
Step 10: Calculate Average waiting time by dividing the total waiting time by total number of
process.
Step 11: Calculate Average turn around time by dividing the total time by the number of process.
Step 12: Display the result.
Step 13: Stop the program.
PROGRAM:
SOURCE CODE:
/* A program to simulate the FCFS CPU scheduling algorithm */
#include<stdio.h>
int main()
{
char pn[10][10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,n;
int totwt=0,tottat=0;
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Process Name, Arrival Time & Burst Time:");
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
}
for(i=0;i<n;i++)
{
Page 34
MCET OPERATING SYSTEMS LAB MANUAL
if(i==0)
{
star[i]=arr[i];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
else
{
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
}
printf("\nPName Arrtime Burtime Start TAT Finish");
for(i=0;i<n;i++)
{
printf("\n%s\t%6d\t\t%6d\t%6d\t%6d\t%6d",pn[i],arr[i],bur[i],star[i],tat[i],finish[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("\nAverage Waiting time:%f", (float)totwt);
printf("\nAverage Turn Around Time:%f", (float)tottat);
}
Page 35
MCET OPERATING SYSTEMS LAB MANUAL
OUTPUT:
$ vi fcfs.c
$ cc fcfs.c
$ ./a.out
Enter the number of processes: 3
Enter the Process Name, Arrival Time & Burst Time: 1 2 3
Enter the Process Name, Arrival Time & Burst Time: 2 5 6
Enter the Process Name, Arrival Time & Burst Time: 3 6 7
PName Arrtime Burtime Start TAT Finish
1 2 3 2 3 5
2 5 6 5 6 11
3 6 7 11 12 18
Average Waiting time: 1.666667
Average Turn Around Time: 7.000000
RESULT:
Thus the program was executed and verified successfully.
Page 36
MCET OPERATING SYSTEMS LAB MANUAL
P0 0 5 0
P1 1 3 5
P2 2 8 14
P3 3 6 8
P0 0-0=0
P1 5-1=4
P2 14 - 2 = 12
P3 8-3=5
Page 37
MCET OPERATING SYSTEMS LAB MANUAL
ALGORITHM:
PROGRAM:
SOURCE CODE:
/* A program to simulate the SJF CPU scheduling algorithm */
#include<stdio.h>
#include<string.h>
main()
{
int i=0,pno[10],bt[10],n,wt[10],temp=0,j,tt[10];
float sum,at;
printf("\n Enter the no of process ");
scanf("\n %d",&n);
printf("\n Enter the burst time of each process");
for(i=0;i<n;i++)
{
printf("\n p%d",i);
scanf("%d",&bt[i]);
Page 38
MCET OPERATING SYSTEMS LAB MANUAL
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[i]>bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
temp=pno[i];
pno[i]=pno[j];
pno[j]=temp;
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=bt[i-1]+wt[i-1];
sum=sum+wt[i];
}
printf("\n process no \t burst time\t waiting time \t turn around time\n");
for(i=0;i<n;i++)
{
tt[i]=bt[i]+wt[i];
at+=tt[i];
printf("\n p%d\t\t%d\t\t%d\t\t%d",i,bt[i],wt[i],tt[i]);
}
printf("\n\n\t Average waiting time%f\n\t Average turn around time%f", sum, at);
}
Page 39
MCET OPERATING SYSTEMS LAB MANUAL
OUTPUT:
$ vi sjf.c
$ cc sjf.c
$ ./a.out
Enter the no of process 5
Enter the burst time of each process
p0 1
p1 5
p2 2
p3 3
p4 4
Page 40
MCET OPERATING SYSTEMS LAB MANUAL
P0 (0 - 0) + (12 - 3) = 9
P1 (3 - 1) = 2
P3 (9 - 3) + (17 - 12) = 11
Page 41
MCET OPERATING SYSTEMS LAB MANUAL
ALGORITHM:
Step 1: Start the program.
Step 2: Initialize all the structure elements.
Step 3: Receive inputs from the user to fill process id, burst time and arrival time.
Step 4: Calculate the waiting time for all the process id.
i. The waiting time for first instance of a process is calculated as: a[i].waittime=count +
a[i].arrivt.
ii. The waiting time for the rest of the instances of the process is calculated as:
a) If the time quantum is greater than the remaining burst time then waiting time is
calculated as: a[i].waittime=count + tq.
b) Else if the time quantum is greater than the remaining burst time then waiting time is
calculated as: a[i].waittime=count - remaining burst time
Step 5: Calculate the average waiting time and average turnaround time
Step 6: Print the results of the step 4.
Step 7: Stop the program.
PROGRAM :
SOURCE CODE:
/* A program to simulate the Round Robin CPU scheduling algorithm */
#include<stdio.h>
struct process
{
int burst,wait,comp,f;
}p[20]={0,0};
int main()
{
int n,i,j,totalwait=0,totalturn=0,quantum,flag=1,time=0;
printf("\nEnter The No Of Process :");
scanf("%d",&n);
printf("\nEnter The Quantum time (in ms) :");
scanf("%d",&quantum);
for(i=0;i<n;i++)
{
printf("Enter The Burst Time (in ms) For Process #%2d :",i+1);
scanf("%d",&p[i].burst);
Page 42
MCET OPERATING SYSTEMS LAB MANUAL
p[i].f=1;
}
printf("\nOrder Of Execution \n");
printf("\nProcess Starting Ending Remaining");
printf("\n\t\tTime \tTime \t Time");
while(flag==1)
{
flag=0;
for(i=0;i<n;i++)
{
if(p[i].f==1)
{
flag=1;
j=quantum;
if((p[i].burst-p[i].comp)>quantum)
{
p[i].comp+=quantum;
}
else
{
p[i].wait=time-p[i].comp;
j=p[i].burst-p[i].comp;
p[i].comp=p[i].burst;
p[i].f=0;
}
printf("\nprocess # %-3d %-10d %-10d %-10d", i+1, time, time+j,
p[i].burst-p[i].comp);
time+=j;
}
}
}
printf("\n\n------------------");
printf("\nProcess \t Waiting Time TurnAround Time ");
for(i=0;i<n;i++)
{
Page 43
MCET OPERATING SYSTEMS LAB MANUAL
printf("\nProcess # %-12d%-15d%-15d",i+1,p[i].wait,p[i].wait+p[i].burst);
totalwait=totalwait+p[i].wait;
totalturn=totalturn+p[i].wait+p[i].burst;
}
printf("\n\nAverage\n------------------ ");
printf("\nWaiting Time: %fms",totalwait/(float)n);
printf("\nTurnAround Time : %fms\n\n",totalturn/(float)n);
return 0;
}
OUTPUT:
$ vi rr.c
$ cc rr.c
$ ./a.out
Enter The No Of Process: 3
Enter The Quantum time (in ms): 5
Enter The Burst Time (in ms) For Process # 1: 25
Enter The Burst Time (in ms) For Process # 2: 30
Enter The Burst Time (in ms) For Process # 3: 54
Order Of Execution
Process Starting Ending Remaining
Time Time Time
process # 1 0 5 20
process # 2 5 10 25
process # 3 10 15 49
process # 1 15 20 15
process # 2 20 25 20
process # 3 25 30 44
process # 1 30 35 10
process # 2 35 40 15
process # 3 40 45 39
process # 1 45 50 5
process # 2 50 55 10
process # 3 55 60 34
process # 1 60 65 0
Page 44
MCET OPERATING SYSTEMS LAB MANUAL
process # 2 65 70 5
process # 3 70 75 29
process # 2 75 80 0
process # 3 80 85 24
process # 3 85 90 19
process # 3 90 95 14
process # 3 95 100 9
process # 3 100 105 4
process # 3 105 109 0
------------------
Process Waiting Time TurnAround Time
Process # 1 40 65
Process # 2 50 80
Process # 3 55 109
Average
------------------
Waiting Time: 48.333333ms
TurnAround Time: 84.666667ms
RESULT:
Thus the program was executed and verified successfully.
Page 45
MCET OPERATING SYSTEMS LAB MANUAL
4. AIM: Write C programs to simulate Intra & Inter – Process Communication (IPC)
techniques: Pipes, Messages Queues, and Shared Memory.
DESCRIPTION:
Inter Process Communication (IPC) is a mechanism that involves communication of one process
with another process. This usually occurs only in one system.
Communication can be of two types:
Between related processes initiating from only one process, such as parent and child processes.
Between unrelated processes, or two or more different processes.
Following are some important terms that we need to know before proceeding further on this topic.
Pipes − Communication between two related processes. The mechanism is half duplex meaning the
first process communicates with the second process. To achieve a full duplex i.e., for the second
process to communicate with the first process another pipe is required.
FIFO − Communication between two unrelated processes. FIFO is a full duplex, meaning the first
process can communicate with the second process and vice versa at the same time.
Message Queues − Communication between two or more processes with full duplex capacity. The
processes will communicate with each other by posting a message and retrieving it out of the queue.
Once retrieved, the message is no longer available in the queue.
Shared Memory − Communication between two or more processes is achieved through a shared
piece of memory among all processes. The shared memory needs to be protected from each other by
synchronizing access to all the processes.
Semaphores − Semaphores are meant for synchronizing access to multiple processes. When one
process wants to access the memory (for reading or writing), it needs to be locked (or protected) and
released when the access is removed. This needs to be repeated by all the processes to secure data.
Signals − Signal is a mechanism to communication between multiple processes by way of signaling.
This means a source process will send a signal (recognized by number) and the destination process
will handle it accordingly.
Note − Almost all the programs in this tutorial are based on system calls under Linux Operating
System (executed in Ubuntu).
Page 46
MCET OPERATING SYSTEMS LAB MANUAL
Page 47
MCET OPERATING SYSTEMS LAB MANUAL
write(ser[1],inbuff,msgsize);
printf("\n parent ended!");
}
}
OUTPUT:
$ vi echopipe.c
$ cc echopipe.c
$ ./a.out
RESULT:
Thus the program was executed and verified successfully.
Page 48
MCET OPERATING SYSTEMS LAB MANUAL
Page 49
MCET OPERATING SYSTEMS LAB MANUAL
if(msgrcv(qid,&recv,100,2,0)==-1)
{
perror("\n msgrcv error:");
exit(1);
}
printf("\n message received from child - %s\n",recv.mtext);
}
else
{
send.mtype=2;
strcpy(send.mtext,"\n hi i am child");
len=strlen(send.mtext);
if(msgrcv(qid,&recv,100,1,0)==-1)
{
perror("\n child message received failed");
exit(1);
}
if(msgsnd(qid,&send,len,0)==-1)
{
perror("\n child message send failed");
}
printf("\n received from parent - %s",recv.mtext);
}
}
OUTPUT:
$ vi echomsg.c
$ cc echomsg.c
$ ./a.out
received from parent -
hello i am parent message has been posted
message received from child -
hi i am child
RESULT:
Thus the program was executed and verified successfully.
Page 50
MCET OPERATING SYSTEMS LAB MANUAL
Page 51
MCET OPERATING SYSTEMS LAB MANUAL
Page 52
MCET OPERATING SYSTEMS LAB MANUAL
Page 53
MCET OPERATING SYSTEMS LAB MANUAL
int i, j, s=0, t, r, x;
printf("\n Allow two philosophers to eat at same time\n");
for(i=0;i<howhung;i++)
{
for(j=i+1;j<howhung;j++)
{
if(abs(hu[i]-hu[j])>=1&& abs(hu[i]-hu[j])!=4)
{
printf("\n\ncombination %d \n", (s+1));
t=hu[i];
r=hu[j];
s++;
printf("\nP %d and P %d are granted to eat", philname[hu[i]],
philname[hu[j]]);
for(x=0;x<howhung;x++)
{
if((hu[x]!=t)&&(hu[x]!=r))
printf("\nP %d is waiting", philname[hu[x]]);
}
}
}
}
}
Page 54
MCET OPERATING SYSTEMS LAB MANUAL
OUTPUT:
DINING PHILOSOPHER PROBLEM
Enter the total no. of philosophers: 5
How many are hungry : 3
Enter philosopher 1 position: 2
Enter philosopher 2 position: 4
Enter philosopher 3 position: 5
1.One can eat at a time 2.Two can eat at a time 3.Exit
Enter your choice: 1
Allow one philosopher to eat at any time
P 3 is granted to eat
P 3 is waiting
P 5 is waiting
P 0 is waiting
P 5 is granted to eat
P 5 is waiting
P 0 is waiting
P 0 is granted to eat
P 0 is waiting
1.One can eat at a time 2.Two can eat at a time 3.Exit
Enter your choice: 2
Allow two philosophers to eat at same time
combination 1
P 3 and P 5 are granted to eat
P 0 is waiting
combination 2
P 3 and P 0 are granted to eat
P 5 is waiting
combination 3
P 5 and P 0 are granted to eat
P 3 is waiting
1.One can eat at a time 2.Two can eat at a time 3.Exit
Enter your choice: 3
RESULT:
Thus the program was executed and verified successfully.
Page 55
MCET OPERATING SYSTEMS LAB MANUAL
Page 56
MCET OPERATING SYSTEMS LAB MANUAL
scanf(“%d”, &choice);
switch(choice)
{
case 1: if((in+1)%bufsize==out)
printf(“\nBuffer is Full”);
else
{
printf(“\nEnter the value: “);
scanf(“%d”, &produce);
buffer[in] = produce;
in = (in+1)%bufsize;
}
break;
case 2: if(in == out)
printf(“\nBuffer is Empty”);
else
{
consume = buffer[out];
printf(“\nThe consumed value is %d”, consume);
out = (out+1)%bufsize;
}
break;
}
}
}
OUTPUT:
Produce 2. Consume 3. Exit
Enter your choice: 2
Buffer is Empty
Produce 2. Consume 3. Exit
Enter your choice: 2
Enter the value: 100
Produce 2. Consume 3. Exit
Enter your choice: 1
Page 57
MCET OPERATING SYSTEMS LAB MANUAL
RESULT:
Thus the program was executed and verified successfully.
Page 58
MCET OPERATING SYSTEMS LAB MANUAL
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<stdio.h>
#include<stdlib.h>
#define MKEY1 5543L
#define MKEY2 4354L
#define PERMS 0666
typedef struct
{
long mtype;
char mdata[50];
Page 59
MCET OPERATING SYSTEMS LAB MANUAL
}mesg;
//File: sender1.c
#include "mesg.h"
mesg msg;
int main()
{
int mq_id;
int n;
if((mq_id=msgget(MKEY1,PERMS|IPC_CREAT))<0)
{
printf("Sender: Error creating message");
exit(1);
}
msg.mtype=1111L;
n=read(0,msg.mdata,50);
msg.mdata[n]='\0';
msgsnd(mq_id,&msg,50,0);
}
//File: receiver1.c
#include "mesg.h"
mesg msg;
main()
{
int mq_id;
int n;
if( ( mq_id=msgget(MKEY1, PERMS|IPC_CREAT ) ) < 0)
{
printf("receiver: Error opening message");
exit(1);
}
msgrcv(mq_id,&msg,50,1111L,0);
write(1,msg.mdata,50);
msgctl(mq_id,IPC_RMID,NULL);
}
Page 60
MCET OPERATING SYSTEMS LAB MANUAL
OUTPUT:
$ vi mesg.h
$ vi sender1.c
$ vi receiver1.c
$ cc sender1.c
$ ./a.out
mcet
$ cc receiver1.c
$ ./a.out
mcet
RESULT:
Thus the program was executed and verified successfully.
Page 61
MCET OPERATING SYSTEMS LAB MANUAL
Page 62
MCET OPERATING SYSTEMS LAB MANUAL
{
int n,i,I,j,avail[3],work[3],flag,count=0,sequence[10],k=0;
printf("\nEnter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the %dth process allocated resources:",i);
scanf("%d%d%d",&p[i].allocation[0],&p[i].allocation[1],&p[i].allocation[2]);
printf("\nEnter the %dth process maximum resources:",i);
scanf("%d%d%d",&p[i].max[0],&p[i].max[1],&p[i].max[2]);
p[i].finish=0;
p[i].need[0]=p[i].max[0]-p[i].allocation[0];
p[i].need[1]=p[i].max[1]-p[i].allocation[1];
p[i].need[2]=p[i].max[2]-p[i].allocation[2];
}
printf("\nEnter the available vector:");
scanf("%d%d%d",&avail[0],&avail[1],&avail[2]);
for(i=0;i<3;i++)
work[i]=avail[i];
while(count!=n)
{
count=0;
for(i=0;i<n;i++)
{
flag=1;
if(p[i].finish==0)
if(p[i].need[0]<=work[0])
if(p[i].need[1]<=work[1])
if(p[i].need[2]<=work[2])
{
for(j=0;j<3;j++)
work[j]+=p[i].allocation[j];
p[i].finish=1;
sequence[k++]=i;
flag=0;
Page 63
MCET OPERATING SYSTEMS LAB MANUAL
}
if(flag==1)
count++;
}
}
count=0;
for(i=0;i<n;i++)
if(p[i].finish==1)
count++;
printf("\n The safe sequence is:\t");
if(count++==n)
for(i=0;i<k;i++)
printf("%d\n",sequence[i]);
else
printf("SYSTEM IS NOT IN A SAFE STATE \n\n");
return 0;
}
Page 64
MCET OPERATING SYSTEMS LAB MANUAL
OUTPUT
$ vi bankersavoidance.c
$ cc bankersavoidance.c
$ ./a.out
Enter the number of process: 3
Enter the 0th process allocated resources: 1 2 3
Enter the 0th process maximum resources: 4 5 6
Enter the 1th process allocated resources: 3 4 5
Enter the 1th process maximum resources: 6 7 8
Enter the 2th process allocated resources: 1 2 3
Enter the 2th process maximum resources: 3 4 5
Enter the available vector: 10 12 11
The safe sequence is: 0 1 2
RESULT:
Thus the program was executed and verified successfully.
Page 65
MCET OPERATING SYSTEMS LAB MANUAL
DESCRIPTION:
Deadlock Detection is an important task of OS. As the OS doesn’t take many precautionary
means to avoid it. The OS periodically checks if there is any existing deadlock in the system and
take measures to remove the deadlocks.
Detection
There are 2 different cases in case of Deadlock detection –
If resource has single Instance
o We make a Wait
If resources have multiple instances
o We make a different algorithm.
Deadlock Recovery
Deadlock can be recovered by
1) Kill the Process – One way is to kill all the process in deadlock or the second way kill the
process one by one, and check after each if still deadlock exists and do the same till the
deadlock is removed.
2) Pre – emption – The resources that are allocated to the processes involved in deadlock are
taken away (pre – empted) and are transferred to other processes. In this way, system may
recover from deadlock as we may change system state.
3) Rollback – The OS maintains a database of all different states of system, a state when the
system is not in deadlock is called safe state. A rollback to previous ‘n’ number of safe
states in iterations can help in the recover.
ALGORITHM:
Step 1: Start the Program.
Step 2: Obtain the required data through char and in data types.
Step 3: Enter the filename, index block.
Step 4: Print the file name index loop.
Step 5: File is allocated to the unused index blocks.
Step 6: This is allocated to the unused linked allocation.
Step 7: Stop the execution.
Page 66
MCET OPERATING SYSTEMS LAB MANUAL
PROGRAM:
SOURCE CODE:
Page 67
MCET OPERATING SYSTEMS LAB MANUAL
temp[i]=a[i];
}
for(i=0;i<4;i++)
{
sum=0;
for(j=0;j<5;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=0;i<4;i++)
{
for(l=1;l<k;l++)
{
if(i!=m[l])
{
flag=1;
for(j=0;j<5;j++)
{
if(c[i][j]>temp[j])
{
flag=0;
break;
}
}
}
}
if(flag==1)
{
m[k]=i;
Page 68
MCET OPERATING SYSTEMS LAB MANUAL
k++;
for(j=0;j<5;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing processes are: \n");
for(j=0;j<tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
}
Page 69
MCET OPERATING SYSTEMS LAB MANUAL
OUTPUT:
$ vi bankersdetection.c
$ cc bankersdetection.c
$ ./a.out
enter total no of processes:
4
enter clain matrix:
0 1 0 0 1
0 0 1 0 1
0 0 0 0 1
1 0 1 0 1
enter allocation matrix:
1 0 1 1 0
1 1 0 0 0
0 0 0 1 0
0 0 0 0 0
enter resource vector:
2 1 1 2 1
enter availability vector:
0 0 0 0 1
deadlock causing processes are:
0 1
RESULT:
Thus the program was executed and verified successfully.
Page 70
MCET OPERATING SYSTEMS LAB MANUAL
ALGORITHM:
Step 1: Start the program
Step 2: Read the number of frames
Step 3: Read the number of pages
Step 4: Read the page numbers
Step 5: Initialize the values in frames to -1
Step 6: Allocate the pages in to frames in First in first out order.
Step 7: Display the number of page faults.
Step 8: Stop the program
PROGRAM:
SOURCE CODE:
Page 71
MCET OPERATING SYSTEMS LAB MANUAL
Page 72
MCET OPERATING SYSTEMS LAB MANUAL
OUTPUT:
$ vi fifo.c
$ cc fifo.c
$ ./a.out
2 2-->F
3 23-->F
2 23
1 231-->F
5 531-->F
2 521-->F
4 524-->F
5 524
3 324-->F
2 324
5 354-->F
2 352-->F
No of faults: 9
RESULT:
Thus the program was executed and verified successfully.
Page 73
MCET OPERATING SYSTEMS LAB MANUAL
8.B) LRU
AIM:To Simulate LRU page replacement algorithms.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the number of frames.
Step 3: Read the number of pages.
Step 4: Read the page numbers.
Step 5: Initialize the values in frames to -1.
Step 6: Allocate the pages in to frames by selecting the page that has not been used for the longest
period of time.
Step 7: Display the number of page faults.
Step 8: Stop the program.
PROGRAM :
SOURCE CODE:
/* A program to simulate LRU Page Replacement Algorithm */
#include<stdio.h>
main()
{
int a[5],b[20],p=0,q=0,m=0,h,k,i,q1=1,j,u,n;
char f='F';
printf("Enter the number of pages:");
scanf("%d",&n);
printf("Enter %d Page Numbers:",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
if(p==0)
{
if(q>=3)
q=0;
a[q]=b[i];
q++;
Page 74
MCET OPERATING SYSTEMS LAB MANUAL
if(q1<3)
{
q1=q;
}
}
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3))
{
printf("-->%c",f);
m++;
}
p=0;
if(q1==3)
{
for(k=0;k<q1;k++)
{
if(b[i+1]==a[k])
p=1;
}
for(j=0;j<q1;j++)
{
u=0;
k=i;
while(k>=(i-1)&&(k>=0))
{
if(b[k]==a[j])
u++;
k--;
}
if(u==0)
q=j;
}
Page 75
MCET OPERATING SYSTEMS LAB MANUAL
}
else
{
for(k=0;k<q;k++)
{
if(b[i+1]==a[k])
p=1;
}
}
}
printf("\nNo of faults:%d",m);
}
OUTPUT:
$ vi lru.c
$ cc lru.c
$ ./a.out
Enter the number of pages: 12
Enter 12 Page Numbers:
2 3 2 1 5 2 4 5 3 2 5 2
2 2-->F
3 23-->F
2 23
1 231-->F
5 251-->F
2 251
4 254-->F
5 254
3 354-->F
2 352-->F
5 352
2 352
No of faults: 7
RESULT:
Thus the program was executed and verified successfully.
Page 76
MCET OPERATING SYSTEMS LAB MANUAL
#include<stdio.h>
int main()
{
int queue[20],n,head,i,j,k,seek=0,max,diff;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the size of queue request\n");
scanf("%d",&n);
printf("Enter the queue of disk positions to be read\n");
for(i=1;i<=n;i++)
scanf("%d",&queue[i]);
printf("Enter the initial head position\n");
scanf("%d",&head);
queue[0]=head;
for(j=0;j<=n-1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("Disk head moves from %d to %d with
seek %d\n",queue[j],queue[j+1],diff);
}
printf("Total seek time is %d\n",seek);
avg=seek/(float)n;
printf("Average seek time is %f\n",avg);
return 0;
}
Page 77
MCET OPERATING SYSTEMS LAB MANUAL
OUTPUT
Enter the max range of disk
200
Enter the size of queue request
8
Enter the queue of disk positions to be read
90 120 35 122 38 128 65 68
Enter the initial head position
50
Disk head moves from 50 to 90 with seek
40
Disk head moves from 90 to 120 with seek
30
Disk head moves from 120 to 35 with seek
85
Disk head moves from 35 to 122 with seek
87
Disk head moves from 122 to 38 with seek
84
Disk head moves from 38 to 128 with seek
90
Disk head moves from 128 to 65 with seek
63
Disk head moves from 65 to 68 with seek
3
Total seek time is 482
Average seek time is 60.250000
RESULT:
Thus the program was executed and verified successfully.
Page 78
MCET OPERATING SYSTEMS LAB MANUAL
SSTF stands for Shortest Time First which very uses full of learning about how the disk drive
manages the data having the shortest seek time.
Algorithm :
1. Let Request array represents an array storing indexes of tracks that have been requested.
‘head’ is the position of disk head.
2. Find the positive distance of all tracks in the request array from head.
3. Find a track from requested array which has not been accessed/serviced yet and has
minimum distance from head.
4. Increment the total seek count with this distance.
5. Currently serviced track position now becomes the new head position.
6. Go to step 2 until all tracks in request array have not been serviced.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int queue[100],t[100],head,seek=0,n,i,j,temp;
float avg;
// clrscr();
printf("*** SSTF Disk Scheduling Algorithm ***\n");
printf("Enter the size of Queue\t");
scanf("%d",&n);
printf("Enter the Queue\t");
for(i=0;i<n;i++)
{
scanf("%d",&queue[i]);
}
printf("Enter the initial head position\t");
scanf("%d",&head);
for(i=1;i<n;i++)
t[i]=abs(head-queue[i]);
for(i=0;i<n;i++)
Page 79
MCET OPERATING SYSTEMS LAB MANUAL
{
for(j=i+1;j<n;j++)
{
if(t[i]>t[j])
{
temp=t[i];
t[i]=t[j];
t[j]=temp;
temp=queue[i];
queue[i]=queue[j];
queue[j]=temp;
}
}
}
for(i=1;i<n-1;i++)
{
seek=seek+abs(head-queue[i]);
head=queue[i];
}
printf("\nTotal Seek Time is%d\t",seek);
avg=seek/(float)n;
printf("\nAverage Seek Time is %f\t",avg);
return 0;
}
OUTPUT:
RESULT:
Thus the program was executed and verified successfully.
Page 80
MCET OPERATING SYSTEMS LAB MANUAL
ADDITIONAL PROGRAMS
11. SHELL PROGRAMMING
1. Shell or the Command interpreter is the mediator which interprets the commands and then
conveys them to the kernel which ultimately executes them.
2. Kernel is usually stored in a file called ‘UNIX’ where as the shell program in a file called
‘sh’.
3. Types of shells:-.
i. Bourne shell (sh) or Bourne again shell (bash)
ii. C shell (csh)
iii. Korn shell (ksh)
4. A shell program is nothing but a series of unix commands.
5. Instead of specifying one job at a time, the shell is given a to-do-list – a program – that
carries out an entire procedure.
6. Such programs are known as shell scripts.
Shell programming language incorporates most of the features that most modern day
programming languages offer.
Shell variables –
Rules for building shell variables are as follows:
1) A variable name is any combination of alphabets, digits and an underscore (‘_’).
2) No commas or blanks are allowed within a variable name.
3) The first character of a variable name must either be an alphabet or an underscore.
4) Variable names should be of any reasonable length.
5) Variable names are case sensitive.
Keywords for accepting input – read
Displaying output - echo
Assigning value to variables –
Values can be assigned to variables through read statement or also by using a simple
assignment operator. For ex: age=30
Note : While assigning values to variables using assignment operator, no spaces to be given on
either side of it. If the variable doesn’t exist it will be created and value assigned
1. Unix-defined variables or System variables or Environment variables
2. User- defined variables
Note : To print or access value of a variable use ‘$’ .
For ex: To print value of variable ‘flag‘ write - echo $flag
Arithmetic in Shell script -
1. All shell variables are string variables, hence to carry out arithmetic operations use expr
Page 81
MCET OPERATING SYSTEMS LAB MANUAL
Page 82
MCET OPERATING SYSTEMS LAB MANUAL
Page 83
MCET OPERATING SYSTEMS LAB MANUAL
SOURCE CODE
echo “enter the first string”
read str1
echo “enter the second string”
read str2
echo “the concatenated string is” $str1$str2
OUTPUT:
$ vi concat.sh
$ sh concat.sh
Enter first string: Hello
Enter first string: World
The concatenated string is HelloWorld
RESULT:
Thus the shell program to concatenate two strings is executed and output is verified
successfully.
Page 84
MCET OPERATING SYSTEMS LAB MANUAL
ALGORITHM:
Step 1: Start the program.
Step 2: Enter into the vi editor and go to the insert mode for entering the code.
Step 3: Read the first string.
Step 4: Read the second string.
Step 5: Compare the two strings using the if loop.
Step 6: If the condition satisfies then print that two strings are equal else print two strings are not
equal.
Step 7: Enter into the escape mode for the execution of the result and verify the output.
Step 8: Stop the program.
SOURCE CODE:
echo "enter a string 1"
read first
echo "enter a string 2"
read second
if [ $first = $second ] # this “ =” for other than digits equals
then
echo “strings are equal”
else
echo “strings are unequal”
fi
OUTPUT:
$ vi compare1.sh
$ sh compare1.sh
enter a string 1
abcxyz
enter a string 2
abcxyz
Page 85
MCET OPERATING SYSTEMS LAB MANUAL
RESULT:
Thus the shell program to compare the two strings is executed and output is verified
successfully.
Page 86
MCET OPERATING SYSTEMS LAB MANUAL
AIM:To write a shell program to compare of two strings from command lines.
ALGORITHM:
Step 1: Start the program.
Step 2: Enter into the vi editor and go to the insert mode for entering the code.
Step 3: Read the first string.
Step 4: Read the second string.
Step 5: Compare the two strings using the if loop.
Step 6: If the condition satisfies then print that two strings are equal else print two strings are not
equal.
Step 7: Enter into the escape mode for the execution of the result and verify the output.
Step 8: Stop the program.
SOURCE CODE:
if [ $1 = $2 ]
then
echo "Strings are equal….."
else
echo "Strings are not equal….."
fi
OUTPUT:
$ vi compare2.sh
$ sh compare2.sh methodist methodist
Strings are equal…..
$ sh compare2.sh methodist tsidohtem
Strings are not equal…..
RESULT:
Thus the shell program to compare the two strings from command lines is executed and
output is verified successfully.
Page 87
MCET OPERATING SYSTEMS LAB MANUAL
ALGORITHM:
Step 1: Start the program.
Step 2: Initialise a to 0 and b to 1.
Step 3: Print the values of ‘a’ and ‘b’.
Step 4: Add the values of ‘a'’ and ‘b’. Store the added value in variable 'c'.
Step 5: Print the value of ‘c’.
Step 6: Initialise ‘a’ to ‘b’ and ‘b’ to ‘c’.
Step 7: Repeat the steps 3, 4, 5 till the value of ‘a’ is less than 10.
Step 8: Stop the program.
SOURCE CODE:
echo “enter the no. of numbers in the series”
read n
a=0
b=1
d=2
echo “$a”
echo “$b”
while [ $d -lt $n ]
do
c=`expr $a + $b`
echo “$c”
a=$b
b=$c
d=`expr $d + 1`
done
Page 88
MCET OPERATING SYSTEMS LAB MANUAL
OUTPUT:
$ vi fibonacci.sh
$ sh fibonacci.sh
enter the no. of numbers in the series
10
0
1
1
2
3
5
8
13
21
34
RESULT:
Thus the shell program to find the fibonacci series is executed and output is verified
successfully.
Page 89
MCET OPERATING SYSTEMS LAB MANUAL
ALGORITHM:
Step 1: Start the program.
Step 2: Enter into the vi editor and go to the insert mode for entering the code.
Step 3: Read the number.
Step 4: Evaluate whether the given number Even or Odd.
Step 5: Print the Result and verify the output.
Step 6: Stop the program.
SOURCE CODE:
echo “enter the number”
read n
r=1
r=`expr $n % 2`
if [ $r -eq 0 ] # “-eq” for digits or numbers
then
echo “even”
else
echo “odd”
fi
OUTPUT:
$ vi evenodd.sh
$ sh evenodd.sh
enter the number
5
odd
RESULT:
Thus the shell program to find whether the given number is even or odd number is executed
and output is verified successfully.
Page 90
MCET OPERATING SYSTEMS LAB MANUAL
ALGORITHM:
Step 1: Start the program.
Step 2: Enter into the vi editor and go to the insert mode for entering the code.
Step 3: Read the limit.
Step 4: Read the second string.
Step 5: Evaluate all Even in the given limit.
Step 6: Print the Result and verify the output.
Step 7: Stop the program.
SOURCE CODE:
echo -n "enter the limit:"
x=2
read num
while [ $x -lt $num ]
do
echo -n "$x \t"
x=`expr $x + 2`
done
OUTPUT:
$ vi evennums.sh
$ sh evennums.sh
enter the limit:20
2 4 6 8 10 12 14 16 18
RESULT:
Thus the shell program to find list of even numbers in a given limit is executed and output is
verified successfully.
Page 91
MCET OPERATING SYSTEMS LAB MANUAL
Page 92
MCET OPERATING SYSTEMS LAB MANUAL
Page 93