0% found this document useful (0 votes)
52 views15 pages

Operating Systems Lab Manual (CSC211) B

The document provides details about the operating systems lab manual covering topics like CPU scheduling algorithms, processes and threads, producer-consumer problem using semaphores, memory management algorithms and more. Key topics covered include FCFS, SJF, SRTF, priority and round robin scheduling algorithms, shell scripting and file manipulation, first fit and best fit memory allocation algorithms.

Uploaded by

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

Operating Systems Lab Manual (CSC211) B

The document provides details about the operating systems lab manual covering topics like CPU scheduling algorithms, processes and threads, producer-consumer problem using semaphores, memory management algorithms and more. Key topics covered include FCFS, SJF, SRTF, priority and round robin scheduling algorithms, shell scripting and file manipulation, first fit and best fit memory allocation algorithms.

Uploaded by

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

OPERATING SYSTEMS LAB

(CSC211)

LAB MANUAL

IV Semester B.Tech(CSE)

Winter Semester (2022-23)

Department of Computer Science and Engineering


IIT(ISM)
INDEX
S. No. Lab Contents No. of Labs Page No.
1. CPU Scheduling Algorithms. 2 3
i) Process and Thread
2. 2 5
ii) Shell Programming and File manipulation
3. Producer-Consumer problem using semaphores 1 7
4. Memory management: First fit and Best fit 1 8
5. Page Replacement Algorithms 3 9
6. Deadlock Prevention & Avoidance Algorithm 1 12
7. Disk Scheduling Algorithms 1 14
8. Project 1 15
1: CPU Scheduling Algorithms: a) FCFS b) SJF and c) SRTF d)
Priority e) RR, and f) Multilevel Queue

a) First Come First Server (FCFS)


1. Start the program.
2. Get the number of processes and their burst time.
3. Initialize the waiting time for process 1 and 0.
4. Process for(i=2;i<=n;i++), wt.p[i]=p[i-1]+bt.p[i-1].
5. The waiting time of all the processes is summed then average value time is calculated.
6. The waiting time of each process and average times are displayed
7. Stop the program

b) Shortest Job First (SJF)


1. Start the program. Get the number of processes and their burst time.
2. Initialize the waiting time for process 1 as 0.
3. The processes are stored according to their burst time.
4. The waiting time of all the processes summed and then the average time is calculated
5. The waiting time of each process and average time are displayed.
6. Stop the program.

c) Shortest Remaining Time First (SRTF)


1. Traverse until all process gets completely executed.
a. Find process with minimum remaining time at every single time lap.
b. Reduce its time by 1.
c. Check if its remaining time becomes 0
d. Increment the counter of process completion.
e. Completion time of current process = current_time + 1;
f. Calculate waiting time for each completed process as wt[i]= Completion time –
arrival_time-burst_time
g. Increment time lap by one.
2. Find turnaround time (waiting_time + burst_time).

d) Priority
1. Start the program.
2. Read burst time, waiting time, turn the around time and priority.
3. Initialize the waiting time for process 1 and 0.
4. Based up on the priority process are arranged
5. The waiting time of all the processes is summed and then the average waiting time
6. The waiting time of each process and average waiting time are displayed based on the priority.
7. Stop the program.

e) Round Robin (RR)


1. Get the number of process and their burst time.
2. Initialize the array for Round Robin circular queue as ‘0’.
3. The burst time of each process is divided and the quotients are stored on the round Robin array.
4. According to the array value the waiting time for each process and the average time are
calculated as line the other scheduling.
5. The waiting time for each process and average times are displayed.
6. Stop the program.

f) Multilevel Queue
1. Enter process along with their arrival time, burst time and queue number

3
2. Enter time quantum
3. Take process with lowest queue number available at that point of time.
4. If its burst time is less than time quantum then complete the process else decrease it by time
quantum
5. Execution of low priority process is interrupted if a high priority process arrives.
6. Repeat step 3, 4 and 5 until all the processes have finished execution.
7. Calculate turnaround time, waiting time and average time.

4
2: i) Process and Thread

Algorithm:
1. Start the process and the thread
2. Declare process and thread as per their id’s.
3. Read the state of process and thread.
4. Put the process and thread to sleep and wait
5. Display the completed process and thread.
6. Stop the process and thread

ii) Shell Programming and File manipulation


Basic UNIX Commands
Unix Command Description
pwd Print Working Directory.
Cd Change Directory
Cd directory Change into the specified directory name.
~ “~” is an alias for your home directory. It can be used as a
shortcut to your “home”, or other directories relative to
your home.
cd .. Move up one directory. For example, if you are in
/home/vic and you type “cd ..”, you will end up in
/home.
- Return to previous directory. An easy way to get back to
your previous location!
ls List all files in the current directory, in column format.
ls directory List the files in the specified directory.
ls -l List files in “long” format, one file per line. This also
shows you additional info about the file, such as
ownership, permissions, date, and size.
ls -a List all files, including “hidden” files. Hidden files
are those files that begin with a “.”, e.g. The
.bash_history file in your home directory.
/usr/bin/d* List all files whose names begin with the letter “d” in the
/usr/bin directory.

Q-1 Write a shell program to add two numbers.


Algorithm:
1. Read first number
2. Read second number
3. Add both numbers in third variable.
4. Print the third variable

Q- 2 Write a shell program to find a number is even or odd


Algorithm:
1. Read the number
2. Use the condition of checking if the number is even by using second variable
3. Check if the variable equals 0
4. Print the result if the number is even or not

Q -3 Write a shell program for fibbonacci series.


Algorithm:
1. Start
2. Declare variable i, a, b, show.
5
3. Initialize a=0, b=1 and show=0
4. Enter the number of terms of series for printing.
5. Print first two terms of series.
6. Use loop for: show=a+b. a=b. b=show. Increment i.
7. End.

File manipulation

Algorithm 1:
1. Start the program.
2. Use the pre-defined function list out the files in directory.
3. Main function is used to check the file present in the directory or not.
4. Using the file pointer in file to that argument is less than a times means print.
5. By using if loop check in file, open two means print error
6. Stop the program.

Algorithm 2:
1. Start the program.
2. Declare the arguments for file open and file create.
3. Print the file in directory otherwise display the error message error in creation.
4. If check the files in directory
5. Close the files and directory
6. Stop the program.

6
3: PRODUCER-CONSUMER PROBLEM USING SEMAPHORES
Synchronization tool that provides more sophisticated ways (than Mutex locks) for process
to synchronize their activities.
Semaphore S – integer variable
Can only be accessed via two indivisible (atomic) operations wait() and signal() Originally
called P() and V()
Definition of the wait() operation
wait(S) {
while (S <= 0)
; // busy wait
S--;
}
Definition of the signal() operation
signal(S) {
S++;
}
Algorithm:
1. Declare variable for producer & consumer as pthread-t-tid produce tid consume.
2. Declare a structure to add items, semaphore variable set as struct.
3. Read number the items to be produced and consumed.
4. Declare and define semaphore function for creation and destroy.
5. Define producer function.
6. Define consumer function.
7. Call producer and consumer.
8. Stop the execution.

7
4: MEMORY MANAGEMENT: FIRST FIT AND BEST FIT

Algorithm: FIRST FIT


1. Start the program.
2. Get the segment size, number of processes to be allocated and their corresponding size.
3. Get the options. If the option is ‘2’ call first fit function.
4. If the option is ‘1’ call best fit function. Otherwise exit.
5. For first fit, allocate the process to first possible segment which is free and set the
personnel slap as ‘1’. So that none of process to be allocated to segment which is already
allocated and vice versa.
6. For best fit, do the following steps,
7. Sorts the segments according to their sizes.
8. Allocate the process to the segment which is equal to or slightly greater than the process
Size and set the flag as the ‘1’. So that none of the process to be allocated to the segment
which is already allocated and vice versa. Stop the program.
9. Stop the program

Algorithm: BEST FIT


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 it to the current process.
4. If not then leave that process and keep checking the further processes.

8
5: PAGE REPLACEMENT ALGORITHMS
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. When a page needs to be replaced page in the front of the queue is
selected for removal.
Example-1Consider page reference string 1, 3, 0, 3, 5, 6 with 3 page frames. Find
numberof page faults.

Initially all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —>
3 Page Faults.
When 3 comes, it is already in memory so —> 0 Page Faults.
Then 5 comes, it is not available in memory so it replaces the oldest page slot i.e 1.—>
1 Page Fault.
6 comes, it is also not available in memory so it replaces the oldest page slot i.e 3 —>1
Page Fault.
Finally, when 3 come it is not avilable so it replaces 0 1 page fault
Belady’s anomaly – Belady’s anomaly proves that it is possible to have more page
faults when increasing the number of page frames while using the First in First out
(FIFO) page replacement algorithm. For example, if we consider reference string 3, 2, 1,
0, 3, 2, 4, 3, 2,
1, 0, 4 and 3 slots, we get 9 total page faults, but if we increase slots to 4, we get 10
page faults.

Algorithm:
1. Start the program
2. Read the number of frames
3. Read the number of pages
4. Read the page numbers
5. Initialize the values in frames to -1
6. Allocate the pages in to frames in First in first out order.
7. Display the number of page faults.
8. Stop the program

9
Least Recently Used(LRU)
In this algorithm page will be replaced which is least recently used.
Example-2 Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 with 4
pageframes. Find number of page faults.

Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page
faults
0 is already there so —> 0 Page fault.
When 3 came it will take the place of 7 because it is least recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already
available in the memory.
Algorithm:
1. Start
2. Read the number of frames
3. Read the number of pages
4. Read the page numbers
5. Initialize the values in frames to -1
6. Allocate the pages in to frames by selecting the page that has not been used
for thelongest period of time.
7. Display the number of page faults.
8. Stop

10
Optimal Page(OP)
In this algorithm, pages are replaced which would not be used for the longest duration
oftime in the future.
Example-3: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, with 4 page
frame.Find number of page fault.

Initially all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page
faults
0 is already there so —> 0 Page fault.
When 3 came it will take the place of 7 because it is not used for the longest duration of
timein the future.—>1 Page fault.
0 is already there so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault.
Now for the further page reference string —> 0 Page fault because they are already
available in the memory.
Optimal page replacement is perfect, but not possible in practice as the operating system
cannot know future requests. The use of Optimal Page replacement is to set up a benchmark
so that other replacement algorithms can be analyzed against it.

Algorithm:
1. Start the program
2. Read the number of frames
3. Read the number of pages
4. Read the page numbers
5. Initialize the values in frames to -1
6. Allocate the pages in to frames by selecting the page that will not be used for the longest
period of time.
7. Display the number of page faults.
8. Stop the program

11
6: DEADLOCK PREVENTION & AVOIDANCE ALGORITHMS

Deadlock Prevention
We can prevent Deadlock by eliminating any of the above four conditions.
Eliminate Mutual Exclusion
It is not possible to dis-satisfy the mutual exclusion because some resources, such as the
tapedrive and printer, are inherently non-shareable.
Eliminate Hold and wait
1. Allocate all required resources to the process before the start of its execution, this way
hold and wait condition is eliminated but it will lead to low device utilization. For
example, if a process requires printer at a later time and we have allocated printer
before the start of its execution printer will remain blocked till it has completed its
execution.
2. The process will make a new request for resources after releasing the current
set ofresources. This solution may lead to starvation.

Eliminate No Preemption
Preempt resources from the process when resources required by other high priority
processes.
Eliminate Circular Wait
Each resource will be assigned with a numerical number. A process can
request theresources increasing/decreasing order of numbering.
For Example, if P1 process is allocated R5 resources, now next time if P1 ask for R4,
R3 lesser than R5 such request will not be granted, only request for resources more
than R5 willbe granted.
Algorithm:
1. Start the program
2. Attacking mutex condition: never grant exclusive access. But this may not be
possible forseveral resources.
3. Attacking preemption: not something you want to do.
4. Attacking hold and wait condition: make a process hold at the most 1 resource.
5. At a time. Make all the requests at the beginning. Nothing policy. If you feel, retry.
6. Attacking circular wait: Order all the resources. Make sure that the requests are
issued in the
7. Correct order so that there are no cycles present in the resource graph.
Resources numbered 1 ... n.
8. Resources can be requested only in increasing
9. Order. i.e. you cannot request a resource whose no is less than any you may be holding
10. Stop

12
Banker’s Algorithm:
1. Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively. Initialize: Work =
Available, Finish[i] = false; for i=1, 2, 3, 4….n
2. Find an i such that both Finish[i] = false and Needi <= Work if no such i exists goto
step (4)
3. Work = Work + Allocation[i], Finish[i] = true, goto step (2)
4. if Finish [i] = true for all i then the system is in a safe state

13
7: DISK SCHEDULING ALGORITHMS
First Come First Server(FCFS)
Algorithm:
1. Let Request array represents an array storing indexes of tracks that have been requested in
ascending order of their time of arrival. ‘head’ is the position of disk head.
2. Let us one by one take the tracks in default order and calculate the absolute distance of the track
from the head.
3. Increment the total seek count with this distance.
4. Currently serviced track position now becomes the new head position.
5. Go to step 2 until all tracks in request array have not been serviced.

Shortest Seek Time First(SSTF)


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.

SCAN
Algorithm:
1. Let Request array represents an array storing indexes of tracks that have been requested in
ascending order of their time of arrival. ‘head’ is the position of disk head.
2. Let direction represents whether the head is moving towards left or right.
3. In the direction in which head is moving service all tracks one by one.
4. Calculate the absolute distance of the track from the head.
5. Increment the total seek count with this distance.
6. Currently serviced track position now becomes the new head position.
7. Go to step 3 until we reach at one of the ends of the disk.
8. If we reach at the end of the disk reverse the direction and go to step 2 until all tracks in request
array have not been serviced.

14
8: PROJECT
The project would be assigned in the group of students, where each group would be
given different project to work upon. The project would be given in the initial phase of
the lab conduction and the students would be required to work upon the project from
that duration itself till the course continues. At last, the project would be evaluated.

15

You might also like