CS 330 Operating Systems - Lab Manual
CS 330 Operating Systems - Lab Manual
Operating System
(CS 330)
Fall Semester
PREFACE
This lab manual has been prepared to facilitate the students of software engineering in studying and analysing
various components of an operating system. Operating system is a software component that allows the users to
interact with the hardware and also hides the underlying complexity. This lab manual is designed to give
practice to the students in using and manipulating different OS. The OS constructs are simulated as well. The
platforms are Windows and Linux. Tools and language required are highlighted in each session.
PREPARED BY
Lab manual is prepared by Dr. Hammad Afzal and Lab Engr Umar Mahmud under the supervision of Head of
Department Dr. Naveed Iqbal Rao in year 2012.
GENERAL INSTRUCTIONS
a. Students are required to maintain the lab manual with them till the end of the semester.
b. All readings, answers to questions and illustrations must be solved on the place provided. If more space is
required then additional sheets may be attached.
c. It is the responsibility of the student to have the manual graded before deadlines as given by the instructor
d. Loss of manual will result in re submission of the complete manual.
e. Students are required to go through the experiment before coming to the lab session. Lab session details will
be given in training schedule.
f. Students must bring the manual in each lab.
g. Keep the manual neat clean and presentable.
h. Plagiarism is strictly forbidden. No credit will be given if a lab session is plagiarised and no re submission
will be entertained.
i. Marks will be deducted for late submission.
Date
Version
September 2012
August 2014
August 2015
1.0
1.1
1.2
VERSION HISTORY
Updated By
LE Umar Mahmud
Lec Mobeena Shahzad
Lec Mobeena Shahzad
Details
1
2
3
4
5
6
7
8
9
10
11
12
13
14
List of Experiments
Introduction to Operating Systems
Ubuntu
Creating Process using C++
Creating process in Ubuntu and Windows
Creating Process using Java
Process State Model
Threads
Process Scheduling
Petersons Algorithm
Semaphores
Linux Modules
IPC
MLFQ
Memory Management
CLO
1
1
2
2
2
2
2
2
2
3
2
3
3
3
MARKS
Ex
p#
Date
Conducted
Experiment Title
Max.
Marks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Grand Total
Marks
Obtaine
d
Instructor
Sign
LIST OF EXPERIMENTS
EXPERIMENT 1 INTRODUCTION TO OPERATING SYSTEMS.................................................................6
EXPERIMENT 2 UBUNTU................................................................................................................. 9
EXPERIMENT 3 CREATING PROCESS USING C++..............................................................................13
EXPERIMENT 4 CREATING PROCESS IN UBUNTU AND WINDOWS.....................................................16
EXPERIMENT 5 CREATING PROCESS USING JAVA............................................................................ 20
EXPERIMENT 6 PROCESS STATE MODEL......................................................................................... 23
EXPERIMENT 7 THREADS.............................................................................................................. 25
EXPERIMENT 8 PROCESS SCHEDULING.......................................................................................... 27
EXPERIMENT 9 PETERSONS ALGORITHM...................................................................................... 29
EXPERIMENT 10 SEMAPHORES...................................................................................................... 31
EXPERIMENT 11 LINUX MODULES................................................................................................. 33
EXPERIMENT 12 IPC...................................................................................................................... 36
EXPERIMENT 13 MLFQ.................................................................................................................. 38
EXPERIMENT 14 MEMORY MANAGEMENT..................................................................................... 41
5.
Objectives:
(a). Practical usage of different OS.
(b). Working with common system tools in Windows and Ubuntu
Time Required: 3 hrs
Software Required:
(a). MS DOS
(b). Windows OS
(c). Ubuntu OS
(d). Android/Symbian/etc
Operating System is an intermediary between the user of a compute machine and the underlying
hardware. The major need of an OS is to provide a convenient means to the user for machine
operation. Moreover, the OS also solves the user problems as well as hides the complexities of
the underlying hardware.
Components of the OS are shown as below
6.
(0.5)
7.
(0.25)
8.
(0.25)
9.
10.
Disk Operating System (DOS): DOS is a single task batch OS. It has a Command-Line
Interface (CLI) that allows the user to perform different tasks. DOS was developed for IBM
compatible machines in 1981.
Press Windows key + r and type cmd. What was the outcome?
(0.25)
11.
2.
3.
4.
12.
13.
14.
15.
(0.25)
(0.5)
16.
(0.25)
17.
What happens when we type IPCONFIG? What happens when IPCONFIG/ALL is typed?
(0.25)
18.
19.
20.
22.
(0.5)
23.
In the Performance Tab, What are the current CPU usage and the PF Usage?
(0.25)
24.
In the users tab? Who all are the current users? Can users be switched in windows? If so how?
(0.5)
25.
What is the total number of Handles, Threads and Processes currently on your machine? Where
did you find it?
(0.5)
26.
Disk Manager: Right click on My Computer and select Manage. Go to storage and then Disk
Management. What is displayed?
(0.25)
27.
(0.25)
28.
Device Manager: Select device manager. Disable and then Enable the sound driver. What
facility is provided in Device Manager?
(0.5)
21.
(1)
29.
What are the specifications of your mobile phone? Write the Make, Model, and the OS.
(0.25)
30.
Describe how can you get the MAC address of your mobile phone? Write the
command/procedure and the MAC address
(0.5)
31.
32.
33.
34.
35.
Use the Device Manager in Ubuntu. Disable and Enable the Sound Driver. What facility is
provided in Device manager?
35.
36.
Web Resources:
A list of DOS commands can be found here. http://www.computerhope.com/msdos.htm
More information about linux at http://www.liberiangeek.net/2010/03/linux-beginner-anintroduction-to-ubuntu-linux/
More info on using Ubuntu Terminal https://help.ubuntu.com/community/UsingTheTerminal and
http://linuxpoison.blogspot.com/2008/10/useful-commands-in-ubuntu.html Further Guide
can be found here http://ubuntuguide.org/wiki/Ubuntu_Precise
Video Resources
http://www.youtube.com/watch?v=VuV8aR2D5Gs
http://www.youtube.com/watch?v=1FJHYqE0RDg
http://www.youtube.com/watch?v=MaA0vFKt-ew
37.
Summary:
(0.5)
(1)
(0.5)
(1)
In this experiment we have learned how to use different operating systems (Windows, Ubuntu) and how to use
their tools.
EXPERIMENT 2 UBUNTU
1.
2.
3.
4.
5.
Objectives:
(a). Using Ubuntu and beginners tasks in Ubuntu
(b). Using Terminal Commands in Ubuntu. Setting up Proxy and Network in Ubuntu.
(c). Creating Java codes in Ubuntu and analysing Java codes over multiple platforms.
Time Required: 3 hrs
Software Required:
(a). Ubuntu OS/Windows OS
(b). NetBeans 7.2
Change Themes: Change the theme.
(a). Right-click on the desktop and select change desktop background
(b). Select a Wall paper that best describe you. What is the name of the image?
______________________________________
(c). Change the Theme.
Updating Software: Go to System Settings and then Software Sources
(a). On the Ubuntu Software tab is where you specify Ubuntu Software Sources. All software
from Ubuntu is stored in this location. Select the check boxes to enable. If you need to
add software from other sources, select other.
(b). On the Updates tab, check the types of updates to install. The first two are recommended.
You can also choose to alert you when theres a new upgrade available by choosing
Normal releases in the Release upgrade option.
More
information
at
introduction-to-ubuntu-linux/
10
http://www.liberiangeek.net/2010/03/linux-beginner-an-
(1)
6.
7.
Establish Network and Proxy: You need to know the IP of the system you are using from
Windows.
(a). Go to Windows, Select Run, Type cmd. Type IPCONFIG/all. Write the IP of your
machine here; ___________________
(b). In Ubuntu Go to System Settings and then Network. In Wired tab select Configure. Type
Connection Name as CSDept.
(c). Under IPv4 Settings, Add under address your IP, Netmask is 255.255.252.0, Gateway is
172.23.19.250
(d). DNS Servers are 172.23.16.1 and 172.23.5.12
(e). Select Save.
(f). Which is the browser provided with Ubuntu? ____________________
(g). Open FireFox, Go to Edit and Preferences.
(h). In Network Tab , Select Settings
(i). Type Manual Proxy as 192.168.50.10 and port 8080
(j). Check the Internet and write the top news at www.bbc.com here
__________________________________
(k). Downloads this experiment document from LMS.
(l). Note: This configuration only works in MCS.
(m). Help can be found here http://www.howtogeek.com/howto/17012/how-to-find-your-ipaddress-in-ubuntu/
Using Terminal: Terminal is Command Line Interface of Ubuntu.
(a). Methods to load terminal are : (1)
Dash -> Search for Terminal
(2)
Dash -> More Apps -> 'See More Results' -> Terminal
(3)
Dash -> More Apps -> Accessories -> Terminal
(4)
Keyboard Shortcut: Ctrl + Alt + T
(5)
Type help to view commands
(6)
What is the CLI interface provided in Windows? _________________
(b). File and Directory Commands
(7)
The tilde (~) symbol stands for your home directory. If you are user, then the tilde
(~) stands for /home/user
(8)
pwd: The pwd command will allow you to know in which directory you're
located (pwd stands for "print working directory").
(9)
ls: The ls command will show you ('list') the files in your current directory. Used
with certain options, you can see sizes of files, when files were made, and
permissions of files.
(10)
cd: The cd command will allow you to change directories.
(11)
cp: The cp command will make a copy of a file for you. Example: "cp file foo"
will make an exact copy of "file" and name it "foo", but the file "file" will still be
there. If you are copying a directory, you must use "cp -r directory foo".
(12)
mv: The mv command will move a file to a different location or will rename a
file.
(13)
rm: Use this command to remove or delete a file in your directory.
(14)
rmdir: The rmdir command will delete an empty directory.
(15)
mkdir: The mkdir command will allow you to create directories.
(16)
sudo: The sudo command is used to perform file operations on files that the Root
User would only be allowed to change.
11
8.
9.
10.
11.
12.
(1)
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
Double click the icon in explorer and install NetBeans. Installation help can be found here
http://netbeans.org/community/releases/72/install.html
Creating Java Application: Select File and then New Project and Select Java Application.
Create a Java Code that Displays your name. Show the output here.
12
(2)
(0.5)
(0.5)
(1)
24.
(2)
25.
(2)
26.
Video Resources
http://www.youtube.com/watch?v=VuV8aR2D5Gs
Resources
https://help.ubuntu.com/
http://showmedo.com/videotutorials/ubuntu
27.
Summary:
In this experiment we have learned how to perform basic tasks in Ubuntu, change network
settings and how to create java codes in Ubuntu.
13
4.
5.
6.
Objectives:
(a). Creating processes in Ubuntu.
(b). Learning fork commands in Ubuntu
Time Required: 3 hrs
Software Required:
(a). Ubuntu/Linux and gcc compiler (already available in Ubuntu)
(b). Borland/Turbo C/Visual Studio for Windows Platform
(c). VMWare if required
(d). C language
How to compile C Program in Ubuntu/Linux
(a). Save your program with extension .c in any directory (working directory).
(b). Open Terminal.
(c). Navigate to the working directory
(d). Suppose you saved the program with name hello.c. Now write following lines:
gcc -c hello.c
gcc -o hello hello.c
./hello
(e). The first line compiles your program. Object file hello.o is created
(f). Second line makes an exe file with the name hello.
(g). Third line runs the exe file and shows output of the program.
Using getpid(): This function returns the pid of the current program. Use the following code and
write the output.
int main(){
int pid;
pid = getpid();
printf(Process ID is %d\n, pid);
return 0;
}
14
(1)
7.
Using getppid(): This function returns the pid of the parent process.
int main(){
int ppid;
ppid = getppid();
printf(Parent Process ID is %d\n, ppid);
return 0;
}
What is the outcome?
(1)
8.
Using fork(): fork command in Linux creates a new process by duplicating the calling process.
The new process, referred to as the child, is an exact duplicate of the calling process, referred to
as the parent.
(1)
http://manpages.ubuntu.com/manpages/lucid/man2/fork.2.html
What is the outcome of the following program?
int main(){
fork();
printf(The PID is %d\n, getpid());
return 0;
}
9.
15
(1)
10.
To see if the pid is same as shown in the system, Open System Monitor. Check to see if the pid is
same. Use the following code
int main(){
int pid,i;
pid = fork();
if(pid==0){
printf(I am the child, my process ID is %d\n, getpid());
printf(The childs parent process ID is %d\n, getppid());
}
else{
printf(I am the parent, my process ID is %d\n, getpid());
printf(The parent process ID is %d\n, getppid());
}
scanf(%d,&i); //so that program halts for user input
return 0;
}
11.
(3)
12.
Explain how was the execution carried out in program in point 11? You may use a flow model to
describe it.
(3)
13.
16
4.
Objectives:
(a). Learning fork/exec commands in Ubuntu
(b). Creating process in Windows
Time Required: 3 hrs
Software Required:
(e). Ubuntu/Linux and gcc compiler (already available in Ubuntu)
(f). Borland/Turbo C/Visual Studio for Windows Platform
(g). VMWare if required
(h). C language
What is the outcome of this program?
/**
* This program forks a separate process using the fork()/exec() system calls.
* * Figure 3.10*
* @author Gagne, Galvin, Silberschatz Operating System Concepts - Seventh Edition
* Copyright John Wiley & Sons - 2005. */
#include <stdio.h>
#include <unistd.h>
3include<stdlib.h>
#include <sys/types.h>
int main(){
pid_t pid;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
perror(fork failed);//prints error message
exit(-1);
}
else if (pid == 0) { /* child process */
printf("I am the child %d\n",pid);
execlp("/bin/ls","ls",NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
printf("I am the parent %d\n",pid);
wait(NULL);
printf("Child Complete\n");
exit(0);
}
}
17
(1)
5.
Using Exec: The fork system call creates a new process but that process contains, and is
executing, exactly the same code that the parent process has. More often than not, we'd like to
run a new program.
6.
Example of Execve():The execve system call replaces the current process with a new
program.Type the following command in Terminal and show the output.
ls -aF /
7.
(1)
Using Execlp
execlp, which allows you to specify all the arguments as parameters to the function. Note that the
first parameter is the command. The second parameter is the first argument in the argument list
that is passed to the program (argv[0]). These are often the same but don't have to be. The last
parameter must be a null pointer.
/* execlp: run a program using execlp */
#include <stdlib.h>
/* needed to define exit() */
#include <unistd.h>
/* needed to define getpid() */
#include <stdio.h> /* needed for printf() */
int main(int argc, char **argv) {
printf("About to run ls\n");
execlp("ls", "ls", "-aF", "/", (char*)0);
perror("execlp"); /* if we get here, execlp failed */
exit(1);
}
What is the output?
(1)
18
8.
9.
Using fork() and exec(): The fork system call creates a new process. The execve system call
overwrites a process with a new program. A process forks itself and the child process execs a
new program, which overlays the one in the current process.
/* forkexec: create a new process. */
/* The child runs "ls -aF /". The parent wakes up after 5 seconds */
#include <stdlib.h>
/* needed to define exit() */
#include <unistd.h>
/* needed for fork() and getpid() */
#include <stdio.h> /* needed for printf() */
Int main(int argc, char **argv) {
void runit(void);
int pid; /* process ID */
switch (pid = fork()) {
case 0:
/* a fork returns 0 to the child */
runit();
break;
default: /* a fork returns a pid to the parent */
sleep(5); /* sleep for 5 seconds */
printf("I'm still here!\n");
break;
case -1: /* something went wrong */
perror("fork");
exit(1);
}
exit(0);
}
void runit(void) {
printf("About to run ls\n");
execlp("ls", "ls", "-aF", "/", (char*)0);
perror("execlp"); /* if we get here, execlp failed */
exit(1);
}
What is the outcome of the program?
(1)
10.
19
(3)
11.
12.
Which OS gives you a better interface for creating and executing child programs and why?
13.
(3)
8.
Objectives:
(a). Creating process in Java.
(b). Destroying processes in Java
Time Required: 3 hrs
Software Required:
(a). Windows OS
(b). NetBeans 7.2/JCreator
Process is a program in execution; process execution must progress in sequential fashion. A
process includes a program counter , stack and data section
Check the PID of WINWORD.EXE on your machine and write it here ___________
Close the WINWORD application, restart WINWORD after 1 minute and write the PID here
__________
What is your observation in points 5 and 6?
(2)
Getting PID of JVM through Java: For the given code show what is the output
import java.util.*;
import java.io.*;
import java.lang.management.*;
public class JavaPID {
public static void main(String args[]){
try{
System.out.println(ManagementFactory.getRuntimeMXBean().getName());
}
catch (Exception t){
t.printStackTrace();
}
}
}
9.
(2)
10.
(1)
11.
Creating a Process in Java: A number of methods exist in Java to create a process. We will
follow the method through Java Process class. For the given code
import java.util.*;
import java.io.*;
import java.lang.management.*;
21
12.
13.
14.
Now open Calculator and Freecell application using the same method
To get the exit value modify the code as follows
(2)
import java.util.*;
import java.io.*;
import java.lang.management.*;
public class JavaExec {
public static void main(String args[]){
try {
System.out.println(ManagementFactory.getRuntimeMXBean().getName());
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("mspaint"); // execute mspaint
Thread.sleep(5000); // system sleeps for 5 seconds
proc.destroy(); // close mspaint
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
}
catch (Exception t){
t.printStackTrace();
}
}
}
15.
16.
Re-execute point 14 but close the application in windows before the time expires. Write the
output here.
17.
(2)
18.
(1)
22
19.
Challenging Task: Make a program of CLI (Command Line Interface) which can understand
the command Open <App_Name> and Close_App_Name and open and close the mentioned
application.
20.
References
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Process.html
21. Video Resource:
http://www.youtube.com/watch?v=mwecn6UIzqc
www.cosmolearning.com/video-lectures/concurrency-processes-threads-and-address-spaces7406/
Summary:
In this experiment we have learned how to create and destroy processes in Java.
23
Objectives:
(a). Learning process 5 State Process Model.
(b). Simulating 5 State Process Model.
2.
3.
Software Required:
(a). Java/C/C++/C#
(b). Windows/Ubuntu
4.
Process States: As a process executes, it changes state. The states of a process are: (a). New: The process is being created
(b). Running: Instructions are being executed
(c). Waiting: The process is waiting for some event to occur
(d). Ready: The process is waiting to be assigned to a process
(e). Terminated: The process has finished execution
5.
Process State Model: Process state models describe the life cycle of a process. The Process State
Model is given in Figure 1.
24
6.
Simulate the 5-State Process Model that shows the execution of different processes. Assume the
following global parameters: (a). Ready Queue Size is 5
(b). Waiting Queue Size is 5
(c). The Switch from Ready Queue to Waiting Queue or vice versa takes a total of 2 Seconds.
(d). All Processes will be given a numerical PID for simulation.
(e). Simulate a Process Control Block that holds the total time required to execute and the time of
each I/O request.
(f). A timer counts the total execution seconds of a process and updates it in the PCB.
(g). You may use milliseconds or seconds as unit of time for your program simulation
(h). A Process automatically terminates after it is completed.
7.
The Program must show the following: (a). The Total Time Taken
(b). The current state of the Queues.
(c). The Creation and Termination times of Each Process.
25
(6)
8.
For the information in following set create the trace and show the output.
(a). Example 1
PID
Creation
Time
Total CPU
Time
I/O Operations
P1
T1
10 Seconds
P2
T9
19 Seconds
P3
T20
25 Seconds
(b). Example 2
9.
PID
Creation
Time
Total CPU
Time
I/O Operations
P1
T1
20 Seconds
P2
T9
40 Seconds
P3
T20
25 Seconds
P4
T25
50 seconds
P5
T100
50 seconds
P6
T125
50 seconds
Applet at
26
(4)
http://courses.cs.vt.edu/csonline/OS/Lessons/Processes/index.html
10.
More Resources
http://www.cs.kent.edu/~farrell/osf03/oldnotes/L06.pdf
http://www.slideserve.com/chas/process-description-and-control
http://www.youtube.com/watch?v=ZDQk6e3glfI
Summary:
In this experiment we have learned and simulated 5 state process model in multiple languages in different
operating systems.
27
28
EXPERIMENT 7 THREADS
1.
Objectives:
(a). Using Java Threads
(b). Thread Synchronization
2.
3.
Software Required:
(a). Java
(b). Windows/Ubuntu
4.
5.
Context Switch Using Threads: Switching among threads is much cheaper than to switch
processes as threads share the same process area.
6.
Java Threads; Every thread in Java is created and controlled by the java.lang.Thread class. A
Java program can have many threads, and these threads can run concurrently, either
asynchronously or synchronously. Following shows the methods in Object and Thread Classes of
Java.
29
7.
30
(2)
8.
Thread Creation by Extending the Thread Class: The procedure for creating threads based on
extending the Thread is as follows:
(a). A class extending the Thread class overrides the run() method from the Thread class to
define the code executed by the thread.
(b). This subclass may call a Thread constructor explicitly in its constructors to initialize the
thread, using the super() call.
(c). The start() method inherited from the Thread class is invoked on the object of the class to
make the thread eligible for running.
Download and execute the code from here.
http://www.javabeginner.com/java-thread-example.zip
Post the output acquired on Ubuntu here: -
31
(2)
9.
10.
Create two threads one to read from a file and the other to write into another file. Show the
output trace for a given text file. Code can be found in this online book at page 136. Java
Programming by Example
(2)
11.
What is your learning in this experiment? Are threads better than processes? Why?
(4)
32
12.
References
1. http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html [MUST VISIT]
2. http://www.javabeginner.com/learn-java/java-threads-tutorial
13.
Video Link
http://www.youtube.com/watch?v=KxTRsvgqoVQ
http://www.youtube.com/watch?v=D0u2USIonds
http://www.youtube.com/watch?v=CKFq-WTZZc8
Summary:
In this experiment we have learned how to use and synchronize threads in Java.
Objectives:
(a). Learning process scheduling
(b). Comparing among FCFS and SJF scheduling algorithms through a simulation
2.
3.
Software Required:
(a). Java/C/C++/C#
(b). Windows/Ubuntu
4.
Process Schedulers: Process schedulers are responsible for selecting which process to execute
so that overall performance is increased.
5.
Performance Metrics: Metrics include throughput, CPU overhead, response time, average wait
time etc.
6.
First Come First Served (FCFS) Scheduler: This scheduler implements a FIFO Queue and
schedules all process on their order of arrival.
http://cs.uttyler.edu/Faculty/Rainwater/COSC3355/Animations/fcfs.htm
7.
Shortest Job First (SJF) Scheduler: This scheduler priorities processes on the basis of their
CPU usage time.
http://sharingmythreeyears.blogspot.com/
8.
Comparison of Schedulers: Implement a queue of length 5 to hold processes for FCFS and SJF
both and for different algorithms observe the throughput at each second. To observe throughput
you have to mark at each instant how many processes have been executed for each scheduler.
9.
Assume that all processes arrive at initial time and burst time is available for each process.
10.
34
Process ID
P1
P2
P3
11
11.
12.
13.
P4
P5
Process ID
P1
P2
10
P3
15
P4
20
P5
25
Process ID
P1
25
P2
20
P3
15
P4
10
P5
Given the states in Points 10, 11 and 12 schedule using both techniques and plot throughput on a
graph. The horizontal specifies the time and the vertical marks the total number of processes
completed so far.
35
(5)
14.
Calculate average wait time for each algorithm and for all three states on Points 10, 11 and 12.
Average wait time is the sum of waiting times by each process divided by the total number of
processes
(2)
15.
What is your conclusion based on the throughput and average wait time as well as the type of
input.
(3)
36
16.
Resources
http://www.cosmolearning.com/video-lectures/concurrency-processes-threads-and-addressspaces-7406/
17.
Video Links
http://www.youtube.com/watch?v=FiGKndlvO8I
http://www.youtube.com/watch?v=u9VOokHuXIA
http://www.youtube.com/watch?v=bta48Ix3t_Q
37
Summary:
In this experiment we have learned process scheduling and simulated scheduling algorithms and then compared
them based on their throughput and wait time.
Objectives:
(a). Critical Section
(b). Petersons Algorithm
2.
3.
Software Required:
(a). Java/C/C++/C#
(b). Windows/Ubuntu
4.
Create two processes (producer and consumer) or functions that access the same shared variable.
38
One process increments the variable while the other decrements it. The maximum value of the
variable is 5 and the producer cannot produce anymore items if the variable is 5. The consumer
cannot consume if the variable is zero.
5.
In the main function write a sequence of calls that simulate the producer consumer problem.
Output the normal case, the case where the consumer should be blocked and the case where the
producer should be blocked. Show the trace only.
(2)
6.
How does race condition occur in the cases of point 5? Explain and trace the case.
(2)
39
7.
Critical Section: Access to a shared item is considered critical and is carried out in critical
section only.
8.
40
//The flag array is used to indicate if a process is ready to enter the critical section. //flag[i] =
true implies that process Pi is ready!
9.
10.
Implement and show trace of Petersons Algorithm for two processes. Show output here.
41
(3)
11.
Implement and show trace of Petersons Algorithm for three processes. Show output here.
12.
References
(3)
http://en.wikipedia.org/wiki/Peterson%27s_algorithm
http://www.techopedia.com/definition/27325/petersons-algorithm
Summary:
In this experiment we have implemented Petersons algorithm for two and three processes and evaluated their
output through trace graphs
42
EXPERIMENT 10 SEMAPHORES
1.
Objectives:
(a). Solving critical section problem using semaphores.
(b). Semaphore implementation.
2.
3.
Software Required:
(a). Java/C/C++/C#
(b). Windows/Ubuntu
4.
Semaphores: An integer value used for signalling among processes. Only three operations may be
performed on a semaphore, all of which are atomic:
Initialize,
Decrement (Wait)
Increment. (Signal)
signal (S) {
S++;
}
5.
When one process modifies semaphore value, no other process can simultaneously modify that
semaphore. When a process releases resource, it performs signal. When count goes to 0, all
resources are being used. After that, process that wish to use a resource will block until count
becomes greater than 0.
6.
Counting Semaphore: Integer value can range over an unrestricted domain. Counting semaphores
can be used to control access to a given resource with finite number of instances.
43
7.
Binary Semaphore: Integer value can range only between 0 and 1. They are simpler to implement.
Binary semaphores can be used to deal with critical section problem.
Semaphore mutex;
// initialized to 1
do {
wait (mutex);
// Critical Section
signal (mutex);
// remainder section
} while (TRUE);
8.
9.
Create a semaphore class that implements a semaphore for mutual exclusion. Also implement a
producer class and a consumer class that acquire the use of a shared resource through the shared
semaphore. Both the producer and consumer should implement the wait and signal method.
10.
(8)
11.
45
(2)
12.
References
http://en.wikipedia.org/wiki/Semaphore_(programming)
http://elvis.rowan.edu/~hartley/ConcProgJava/Semaphores/bbou.java
http://www.cs.loyola.edu/~jglenn/702/F2007/Examples/Producer_Consumer_Semaphores/pc_sema
phore.html
13.
Video Link
46
http://freevideolectures.com/Course/2260/Computer-Science-III-Programming-Paradigms/16
Summary:
In this experiment we have implemented semaphores for scheduling processes in operating system.
47
5.
Objectives:
(a). Module programming
(b). Developing Modules In Linux
Time Required: 3 hrs
Software Required: C on Ubuntu
Modules: In computing, a loadable kernel module (or LKM) is an object file that contains code
to extend the running kernel, or so-called base kernel, of an operating system. LKMs are
typically used to add support for new hardware, file-systems, or for adding system calls. When
the functionality provided by an LKM is no longer required, it can be unloaded in order to free
memory and other resources.
Creating Hello World Module: In this experiment the simplest hello world module will be
created. The steps are as follows: (a). Make a folder on the desktop for this lab. Make sure that the name of the folder does not
have a space character in it. Write the name of the folder here ______________.
(b). Create a text file using a text editor with the name hello-1.c. You may create any file
name but the extension should be .c.
(c). Write the following code in the text file
(1)
#include <linux/module.h>
/* Needed by all modules */
#include <linux/kernel.h>
/* Needed for KERN_INFO */
int init_module(void){
printk(KERN_INFO "Sky Fall\n");
// A non 0 return means init_module failed
// module can't be loaded.
return 0;
}
void cleanup_module(void){
printk(KERN_INFO "Breaking Dawn.\n");
}
(d).
(e).
6.
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
48
(3)
(b).
(c).
(d).
(e).
The first line should show the name of your file with .o extension after the += characters.
Do not forget to put a tab character before make in both instances of the keyword make.
In the terminal type sudo make. If asked for the password type in the password.
What is the outcome of this step (if correctly executed)? Show the output as well as the
names of the files created in the working directory.
(f). Use modinfo hello-1.ko to see what kind of information it is. Show the output here.
7.
8.
Loading Module: To load the module in kernel type sudo insmod ./hello-1.ko and remember to
rename hello-1 if you have different file name.
Type lsmod and see if the module is loaded. Show the screen capture here.
(1)
9.
Viewing Log: To view log type dmesg. Show the output here.
(1)
10.
11.
12.
Removing Modules: To remove module type sudo rmmod hello-1 and remember to rename
hello-1 if you have different file name.
Type lsmod and see if the module is unloaded.
Type dmesg again and show the output here.
(1)
13.
14.
49
15.
Remember that there are two underscores before init and exit keywords
Modify the make file as follows:
obj-m += hello-1.o
obj-m += hello-2.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules
clean:
16.
17.
18.
19.
(1)
10.
11.
Remove the modules by typing sudo rmmod hello-1 and then sudo rmmod hello-2
Show the output of dmesg
(1)
12.
(1)
13.
Resources
https://en.wikipedia.org/wiki/Loadable_kernel_module
Videos
http://www.youtube.com/watch?v=8JBZc5OasFg
http://www.youtube.com/watch?v=o768iZKtzBA
14.
Summary:
In this experiment we have learned how to develop and removes modules in Linux .
50
EXPERIMENT 12 IPC
1.
Objectives:
1. To write a program for inter-process communication using shared memory.
2. To write a program for inter-process communication using pipe processing.
2.
3.
Software Required:
(c). Java/C/C++/C#
(d). Windows/Ubuntu
4.
Shared Memory: A shared memory is an extra piece of memory that is attached to some address
spaces for their owners to use. As a result, all of these processes share the same memory segment
and have access to it. Consequently, race conditions may occur if memory accesses are not
handled properly. The following figure shows two processes and their address spaces. The yellow
rectangle is a shared memory attached to both address spaces and both process 1 and process 2
can have access to this shared memory as if the shared memory is part of its own address space.
In some sense, the original address space is "extended" by attaching this shared memory.
5.
(g). The data in the shared memory is read by the child process using the shnot pointer
(h). Now, detach and rebase the shared memory
(i). Stop the program
6.
Pipe Processing: Using pipes for establishing a communication between two processes. As a
first step we need to identify two executables that need to communicate. As an example, consider
a case where one process gets a character string input and communicates it to the other process
which reverses strings. Then we have two processes which need to communicate. Next we define
a pipe and connect it between the processes to facilitate communication. One process gets input
strings and writes into the pipe. The other process, which reverses strings, gets its input (i.e.
reads) from the pipe.
Figure explains how the pipes are used. As shown in the upper part of the figure, a pipe has an
input end and an output end. One can write into a pipe from the input end and read from the
output end. A pipe descriptor, therefore, has an array that stores two pointers. One pointer is for
its input end and the other is for its output end. When a process defines a pipe it gets both the
addresses, as shown in the middle part of Figure. Let us suppose array pp is used to store the
descriptors. pp[0] stores the write end address and pp[1] stores the read end address. Suppose
two processes, Process A and Process B, need to communicate, then it is imperative that the
process which writes closes its read end of the pipe and the process which read closes its write
end of the pipe. Essentially, for a communication from Process A to process B the following
should happen. Process A should keep its write end open and close read end of the pipe.
Similarly, Process B should keep its read end open and close its write end.
7.
(b). Next we spawn two child processes. Both of these would get the pipe definition which we
have defined in the parent. The child processes, as well as the parent, have both the write
and read ends of the pipe open at this time.
(c). Next, one child process, say Process A, closes its read end and the other child process,
Process B, closes its write end.
(d). The parent process closes both write and read ends.
(e). Next, Process A is populated with code to get a string and Process B is populated to
52
reverse a string. With the above arrangement the output from Process A is piped as input
to Process B.
The programs given below precisely achieve this.
In reading the programs, the following interpretations have to be borne in mind:
(a).
(b). The dup command replaces the standard I/O channels by pipe descriptors.
(c). The execlp command is used to populate the child process with the desired code.
(d). The close command closes the appropriate ends of the pipe.
(e). The get_str and rev_str processes are pre-compiled to yield the required executables.
8.
(b).
(c).
(d).
Now close the read end of the parent process using close().
(e).
(f).
(g).
53
(h).
(i).
9.
10.
References
http://www.sriengg.com/wp-content/uploads/2012/05/CS2257-OS-LAB-MANUAL-II-IT-byGBN.pdf
Video Links
http://en.wikipedia.org/wiki/Inter-process_communication
http://www.youtube.com/watch?v=5T4xZ9f7Bfc
http://www.youtube.com/watch?v=ukGjIgVsZjI
Summary:
In this experiment we have learned how to implement inter-process communication programs in
shared memory and pipe memory.
54
(10)
55
EXPERIMENT 13 MLFQ
1.
Objectives: To learn the advanced process scheduling algorithms with priorities and multi-level
feedback queues.
2.
3.
Software Required:
(e). Java/C/C++/C#
(f). Windows/Ubuntu
4.
Priority Scheduling
Sometimes, processes are not equally important. For instance, we may want to see long CPUintensive (non-interactive) processes get a lower priority than interactive processes. These
processes, in turn, should get a lower priority than jobs that are critical to the operating system.
In addition, different users may have different status. A system administrator's processes may
rank above those of a student's. These goals led to the introduction of priority scheduling.
The idea here is that each process is assigned a priority (just a number). Of all processes ready to
run, the one with the highest priority gets to run next. With a priority scheduler, the scheduler
simply picks the highest priority process to run. If the system uses preemptive scheduling, a
process is preempted whenever a higher priority process is available in the ready list.
Dynamic Priority: A process with a dynamic priority will have that priority changed by the
scheduler during its course of execution. For example, the scheduler may decide to decrease a
process' priority to allow a lower-priority job to run.
Advantage: Priority scheduling provides a good mechanism where the relative importance of
each process may be precisely defined.
Disadvantage and its remedy: If high priority processes use up a lot of CPU time, lower priority
processes may starve and be postponed indefinitely. The situation where a process never gets
scheduled to run is called starvation. One approach to the problem of indefinite postponement is
to use dynamic priorities. At the expiration of each quantum, the scheduler can decrease the
priority of the current running process (thereby penalizing it for taking that much CPU time).
Eventually its priority will fall below that of the next highest process and that process will be
allowed to run.
56
Multi-level Queues:
Processes can be grouped into priority classes and assigned a separate run queue for each class;
for instance, separate queues for system processes, interactive processes, low-priority interactive
processes, and background non-interactive processes. The scheduler picks the highest-priority
queue (class) that has at least one process in it. In this sense, it behaves like a priority scheduler.
It then picks a process from the queue. Each queue may use a different scheduling algorithm,
(e.g., typically round robin with a different quantum). For example, low-priority non-interactive
processes could be given a longer quantum. They won't get to run as often since they are in a low
priority queue but, when they do, the scheduler will let them run longer.
Multi-level queues are generally used as a top-level scheduling discipline to separate broad
classes of processes, such as real-time, kernel threads, interactive, and background processes.
Specific schedulers within each class determine which process gets to run within that class.
5.
57
As long as a process uses the full quantum at each level, it continues to drop to lower priority
levels (eventually reaching the lowest level, where it circulates round robin). At each lower
priority level, the quantum is increased (so the process does not get the CPU often, but gets to
use more of it when it does get it). The rationale for this is that processes at lower levels have
demonstrated that they are compute bound rather than I/O bound. By giving them a fatter chunk
of the CPU, but only when there are no higher priority processes to run, we can minimize the
context switch overhead and get more efficient use of the CPU.
A process that waits too long to be scheduled may be moved up to a higher-priority queue
(process aging). While it's common for each queue to circulate processes round-robin, as with
multilevel queues, each queue may have its own scheduling algorithm to determine the order in
which the scheduler removes a process from the queue.
Multi-level feedback queues work well in favouring I/O bound processes with frequent
scheduling by keeping them at high priorities.
Advantages: Multi-level feedback queues are good for separating processes into categories based
on their need for a CPU. They favour I/O bound processes by letting them run often. Versions of
this scheduling policy that increases the quantum at lower priority levels also favor CPU bound
processes by giving them a larger chunk of CPU time when they are allowed to run.
Disadvantages: The priority scheme here is one that is controlled by the system rather than by
the administrator or users. A process is deemed important not because it is, but because it
happens to do a lot of I/O. This scheduler also has the drawback that I/O bound processes that
become CPU bound or CPU bound processes that become I/O bound will not get scheduled well.
6.
Performance Metrics:
Different performance metrics are used to measure the efficiency of scheduling algorithms. They
are briefly described below.
58
(a). Throughput: The total number of processes that complete their execution per time unit.
(b). Turnaround time: Total time between submission of a process and its completion.
(c). Response time: Amount of time it takes from when a request was submitted until the first
response is produced.
(d). Waiting Time: Equal CPU time to each process (or more generally appropriate times
according to each process' priority). It is the time for which the process remains in the ready
queue.
7.
For the following processes with arrival time and burst time given.
Process ID
Arrival
P1
25
P2
20
P3
15
P4
10
P5
10
(10)
8.
References
1. http://www.cs.rutgers.edu/~pxk/416/notes/07-scheduling.html
2. http://sharingmythreeyears.blogspot.com/
9.
Video
60
http://www.youtube.com/watch?v=6vk2yMwa2n0
http://www.youtube.com/watch?v=XONQb3dj78g
Summary:
In this experiment we have learned advanced scheduling algorithms with priority queues and multilevel
feedback, and used performance metrics to calculate the efficiency of algorithms.
61
Objectives: To write a program for first fit and best fit algorithm for memory management.
2.
3.
Software Required:
(c). Java
(d). Windows/Ubuntu
4.
62
5.
Algorithm
Start the program.
(a). Get the number of segments and size.
(b). Get the memory requirement and select the option.
(c). If the option is 2 call first fit function.
(d). If the option is 1 call best fit function.
(e). Otherwise exit.
(f). For first fit, allocate the process to first possible segment which is free.
(g). For best fit, do the following steps.
a. Sorts the segments according to their sizes.
b. Allocate the process to the segment which is equal to or slightly greater than the process
size.
(h). Stop the program.
Implement the algorithm
6.
FIFO
The frames are empty in the beginning and initially no page fault occurs so it is set to minus one.
When a page fault occurs the page reference string is brought into memory. The operating system
keeps track of all the pages in memory, herby keeping track of the most recently arrived and the
oldest one. If the page in the page reference string is not in memory, the page fault is incremented
and the oldest page is replaced. If the page in the page reference string is in the memory, take the
next page without calculating the page fault. Take the next page in the page reference string and
check if the page is already present in the memory or not. Repeat the process until all the pages
are referred and calculate the page fault for all those pages in the page reference string for the
63
(5)
Algorithm
(a). Start the program
(b). Obtain the number of sequences, number of frames and sequence string from the user
(c). Now when a page is not in the frame comes, increment the number of page fault and
remove the page that come in the first in FIFO algorithm
(d). In LRU algorithm, when a page fault occurs, the page which most recently used is
removed
(e). Display the number of faults
(f). Stop the program
Implement the code
64
(5)
8.
Resources
https://en.wikipedia.org/wiki/Memory_management
9.
Video
http://www.youtube.com/watch?v=PSPF_ELJ0cY
Summary:
In this experiment we have written codes and implemented best fit and first fit algorithms used in
65
memory management.
66
Confucius
67
68