Sei sulla pagina 1di 38

LAB MANUAL

CSC 351 Operating Systems Lab


(IV Semester BSSE)

Department of Computer Science


Lahore Garrison University
Operating Systems Lab manual for BSSE
Department of CS, LGU

TABLE OF CONTENTS
Lab 1 Concepts Revision about Software, Hardware, Memory, User & Operating System Types, Features, Uses,
Functions ......................................................................................................................................................................................... 3
Lab 2 Installation of Virtual Machine and Ubuntu .................................................................................................................... 5
Lab 3 Exploring Linux GUI ............................................................................................................................................................. 6
Lab 4 Linux Basic Commands ...................................................................................................................................................... 7
Lab 6 Compiling and running C code in Linux ........................................................................................................................ 12
Lab 7 Process creation and termination using system call .................................................................................................... 14
Lab 9 Implementing FCFS scheduling algorithm in C ............................................................................................................. 26
Lab 10 Implementing Round Robin scheduling algorithm in C ............................................................................................. 29
Lab 11 Implementing Banker’s Algorithm for Deadlock avoidance in C .............................................................................. 31
Lab 12 Logical to Physical Address Translation in C ................................................................. Error! Bookmark not defined.
Lab 13 Demand Paging Performance Program in C ................................................................. Error! Bookmark not defined.
Lab 14 Calculating Bit Map Overhead in Disk Free-Space Management in C ..................... Error! Bookmark not defined.
Lab 15 Demand Paging and Program Structure in C ............................................................... Error! Bookmark not defined.

2|Page
Operating Systems Lab manual for BSSE
Department of CS, LGU

Operating Systems (CSC-351) Instructor: Miss. Afrozah Nadeem


Lab 1 Date: 4 Feb, 2019
Time Allowed :120 min

Lab 1 Concepts Revision about Software, Hardware, Memory,


User & Operating System Types, Features, Uses, Functions

What is an Operating System?


 A program that acts as an intermediary between a user of a
 computer and the computer hardware
 Operating system goals:
 Execute user programs and make solving user problems
 easier
 Make the computer system convenient to use
 Use the computer hardware in an efficient manner
Computer System Structure
 Computer system can be divided into four components:
 Hardware – provides basic computing resources
CPU, memory, I/O devices
 Operating system
Controls and coordinates use of hardware among various
 applications and users
 Application programs – define the ways in which the system
3 |P aresources
ge are used to solve the computing problems of the
users
3|Page
Operating Systems Lab manual for BSSE
Department of CS, LGU

Word processors, compilers, web browsers, database


 systems, video games
Users
People, machines, other computers

What Operating Systems Do


 Depends on the point of view Users want convenience, ease of use and good performance
 Don’t care about resource utilization
 But shared computer such as mainframe or minicomputer
 must keep all users happy
 Users of dedicate systems such as workstations have dedicated

4|Page
Operating Systems Lab manual for BSSE
Department of CS, LGU

 resources but frequently use shared resources from servers


 Handheld computers are resource poor, optimized for usability and battery life
 Some computers have little or no user interface, such as embedded computers in devices and
automobiles

Operating Systems Instructor: Miss. Afrozah Nadeem


Lab 2
Date: 11 Feb, 2019 Time Allowed :120 min

Lab 2 Installation of Virtual Machine and Ubuntu

Aim
To let students, know about Linux and virtual machine installation.

Lab Work:
In order to practice the C code in Linux environment, you are required to install Virtual Box and Ubuntu
operating system on your systems using the guidelines given below.

Following are the guidelines and helping material including required software and their installations:

Step No.1
To install the Linux first you have to install Virtual Box visualization software in your Computer system.

Step No.2

5|Page
Operating Systems Lab manual for BSSE
Department of CS, LGU

Download Ubuntu Linux from the following link:


http://de.releases.ubuntu.com/16.10/ubuntu-16.10-desktop-i386.iso

Follow these tutorials to install Ubuntu Linux in Virtual Machine:


1 - https://vulms.vu.edu.pk/Courses/CS604/Downloads/CS604-Virtual%20Machine.mp4
2 - https://www.lifewire.com/run-ubuntu-within-windows-virtualbox-2202098

Operating Systems Instructor: Miss. Afrozah Nadeem


Lab 3
Date: 18 Feb, 2019 Time Allowed :120 min

Lab 3 Exploring Linux GUI

GUI interaction of Linux


Create Directories,
Delete Directories and
create files on Linux

6|Page
Operating Systems Lab manual for BSSE
Department of CS, LGU

Operating Systems Instructor: Miss. Afrozah Nadeem


Lab 4

Date: 25 Feb, 2019 Time Allowed :120 min

Lab 4 Linux Basic Commands


Aim
To execute some basic commands of Linux

Practice the following Linux commands in Shell:


1. 01
2. Write a command to change your current directory to Desktop.
3. Write a command to create a directory structure as: CS604/Files/CFiles
4. Write a command to display the contents of Desktop.
5. Write a command to remove a directory named CS604.

7|Page
Operating Systems Lab manual for BSSE
Department of CS, LGU

1. Write a command to show your current working directory.

2. Write a command to change your current directory to Desktop.

3.Write a command to create a directory structure as: CS604/Files/CFiles

4. Write a command to display the contents of new created directories.

8|Page
Operating Systems Lab manual for BSSE
Department of CS, LGU

6. Write a command to remove a directory named CS604.

9|Page
Operating Systems Lab manual for BSSE
Department of CS, LGU

Operating Systems Instructor: Miss. Afrozah Nadeem


Lab 5
Date: 1 Mar, 2019 Time Allowed :120 min

Lab 5 Linux Commands

• Ctrl + Alt + t
Open Ubuntu Terminal Window, the command line interface. This is a preinstalled software comes
with every standard Ubuntu.
• pwd
print work directory: e.g. if you are at your home directory then it will print something like
/home/<username>
• ls
list directory: This command will list the items of a directory. If you don’t specify a directory then it
will list work directory, the place where you currently are.
• cd
change directory. It will change your work directly as you specify. You will have to specify a
directory.g. cd /home will change your work directory to /homeregardless where you are.
• cd ..
change directory one level up.
• cd ~
change to home directory. Suppose if your username is tom then cd ~ will change your work
directory to /home/tom.
• cp
Copy Command. It will copy a file or directory. It is similar to Copy-Paste in GUI.
• mv
10 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

Move or rename files. It is like renaming a file or cut-paste in GUI.


• rm
remove file or directory.
• rmdir
remove empty directory. This will not remove content of the directory but it will delete a directory if
it is empty.
• mkdir make directory. It will create a new directory. We will need to specify a name.
• Ctrl + c
It kills process with a signal SIGINT. Don’t confuse with Ctrl+C in Windows or Ctrl+Z. Ctrl+Z is used
to suspend a process while In Windows Ctrl+C is copy command.
• Ctrl + Shift + c
This is a copy command.
• Ctrl + Shift + v
This is paste command like Ctrl+v in Windows.
• nano <filename>
nano is command line text editor. In terminal by running the above command you could edit file
named <filename>.
• sudo nano <filename>
This is same command as we mentioned earlier except it will edit file with privilege of super user.
• gedit <filename>
gedit is GUI text editor like notepad in Windows. It will start editing file named <filename> in gedit
window.
• man
Ubuntu will help you instantly and has a built in manual. Simply apend any command with man or
type executes man. e.g. man mkdir
• sudo
sudo basically allow a standard user to execute a command with root or superuser privilege.
11 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

• adduser <username>
As the name suggest it will add another user. You may also use useradd, which is a little bit different
from the adduser.
• passwd <username>
This command will change password for an user named <username>. Root privilege will be require
while change other user’s password.
• time
time command in Ubuntu will not actually show you current date or time. It will actually run and
show the summary system resources This command will show you the current date and time of your
system (including timezone).

Operating Systems Instructor: Miss. Afrozah Nadeem


Lab 6 Max Marks: 10
Date: 7 March 2019 Time Allowed: 120 min

Lab 6 Compiling and running C code in Linux


Aim
To write a C-program and compile it.

Write a program in C which will display your student id and name as output. Compile and execute your
program in Command prompt to display the output as shown below.

12 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
printf("Student ID: BC123456789\n"); // printing student id
printf("Student Name: ABC\n"); // printing student name

exit(0);

Q2: Write a program in C++ which will display “Class” string as output. Compile and execute your program
in Command prompt to display the output as shown below.

13 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

Lab Tasks: Do it in C & C++


Q1: Write a program to show the prime numbers from the range 1-50?
Q2: Write a program to check the inputted number is ARMSTRONG or Not?
Q3: Write a program to display the following shape?
*
**
***
****

Operating Systems Instructor: Miss.


Afrozah Nadeem
Lab 7
Date: Time Allowed :120
min

Lab 7 Process creation and termination using system call


14 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

Aim: Write a program in C to create a child process using fork() system call.

System call is the programmatic way in which a computer program requests a service from the kernel of
the operating system it is executed on. A system call is a way for programs to interact with the operating
system. A computer program makes a system call when it makes a request to the operating system’s
kernel. System call provides the services of the operating system to the user programs via Application
Program Interface(API). It provides an interface between a process and operating system to allow user-level
processes to request services of the operating system. System calls are the only entry points into the kernel
system. All programs needing resources must use system calls.
Services Provided by System Calls :
1. Process creation and management
2. Main memory management
3. File Access, Directory and File system management
4. Device handling(I/O)
5. Protection
6. Networking, etc.
Types of System Calls :
There are 5 different categories of system calls –
1. Process control: end, abort, create, terminate, allocate and free memory.
2. File management: create, open, close, delete, read file etc.
3. Device management
4. Information maintenance
5. Communication
Examples of Windows and Unix System Calls –

WINDOWS UNIX

15 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

CreateProcess() fork()
ExitProcess() exit()
Process Control WaitForSingleObject() wait()

CreateFile() open()
ReadFile() read()
WriteFile() write()
File Manipulation CloseHandle() close()

SetConsoleMode() ioctl()
ReadConsole() read()
Device Manipulation WriteConsole() write()

GetCurrentProcessID() getpid()
Information SetTimer() alarm()
Maintenance Sleep() sleep()

CreatePipe() pipe()
CreateFileMapping() shmget()
Communication MapViewOfFile() mmap()

SetFileSecurity() chmod()
InitlializeSecurityDescriptor() umask()
Protection SetSecurityDescriptorGroup() chown()

Process creation and termination using fork() and exit() system calls
Write a program in C to create a child process using fork() system call. After process creation, you have to
16 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

display the ids of a process and parent process in case of both parent and child processes. You also
have to terminate processes. Compile and execute your program in Command prompt to display the
output as shown below.

17 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

#include <stdio.h>
main()
{ int pid, status;
if((pid = fork()) == -1)

18 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

{
perror("fork() failed...");
exit(1);
}
else
{
if (pid == 0){
printf("\nChild Process:\n");
printf("Process ID of Child: %i\t", getpid());
printf("Parent ID of Child: %i\t", getppid());
exit(0);
}
else{
wait(&status) ;
printf("\nParent Process:\n");
printf("Process ID of Parent: %i\t", getpid());
printf("Parent ID of Parent: %i\t", getppid());
exit(0);
}
}
} // end of main

Process termination
is a technique in which a process is terminated and release the CPU after completing the execution. Most of the
OS use exit( ) system call to terminate a process. Main causes of process termination. Normal Completion: The
process completes all task and releases the CPU

Topic:
19 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

Inter-process communication between processes using PIPES

Write a program in C for inter-process communication between child and parent processes. You have to
use pipe() system calls for creating a pipe between parent and child processes. The child will write the
student id trough PIPE and parent will read the student id from pipe and display it on screen. Parent will
wait for child process until child write data to pipe. Compile and execute your program in Command
prompt to display the output as shown below.

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>

main()
{
int pipefd[2], pid, n, rc, nr, status;
char id[20] ;

printf("Enter your ID: ");


scanf("%s", &id);

rc = pipe (pipefd);

20 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

if (rc < 0) {
perror("Pipe fail");

exit(1);
}

pid = fork ();


if (pid < 0) {
perror("Fork fail");
exit(1);
}

if (pid == 0) { /* Child’s Code */


close(pipefd[0]);
write(pipefd[1], id, strlen(id));
close(pipefd[1]);
exit(0);
}

/* Parent’s Code */

else
{
wait(&status);
close(pipefd[1]);
n = strlen(id);
21 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

nr = read(pipefd[0], id, n);

write(1, "Stuent ID: ");


write(1, id, nr);
}

} // end of main()

Operating Systems Instructor: Miss.


Afrozah Nadeem
22 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

Lab 8
Date: Time
Allowed :120 min

Lab 8Threads creation and termination

Write a program in C that will create three threads named as Thread 1, Thread 2 and Thread 3. You are
required to run these threads in parallel fashion and display Process ID and Thread IDs according to the
output as shown below. At the end, all threads will be terminated. Compile & run C program on Linux
Operating system.

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void* myThread(void* tmp)
23 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

{
printf("Thread ID: %u", (unsigned int)pthread_self());
printf(" and Parent ID: %d", getpid());
printf (" created and started \n");
sleep(1);
return NULL;
}
int main()
{

printf("\nProcess ID: %d\n", getpid());


int status;
pthread_t tid1,tid2,tid3;
pthread_create(&tid1,NULL, myThread, NULL);
sleep(1);
pthread_create(&tid2,NULL, myThread, NULL);
sleep(1);
pthread_create(&tid3,NULL, myThread, NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);

printf("\nThreads are going to be terminated one by one\n");


printf("\nAll Threads exited safely...\n");
pthread_exit(NULL);
}

24 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

Topic:
Process management commands and Key presses

Following are the sequence of actions which user has to perform for process management. You are
required to write the Linux commands against each action.

 User wants to move the background job to foreground.


 Then for certain reason, he/she wants to suspend the running process.
 And then he/she puts the suspended job in the background.
 Then he/she wants to see the status of all suspended and background jobs.
 At the end, he/she terminates that job/process.
fg
^Z
bg
jobs
kill

25 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

Operating Systems Instructor: Miss. Afrozah


Nadeem
Lab 9
Date: Time Allowed
:120 min

Lab 9 Implementing FCFS scheduling algorithm in C

Write a program in C to implement FCFS scheduling algorithm. Given the list of Processes and their burst
time, calculate waiting time for each process and average waiting time. For example, the list of processes
and their CPU burst time are as follows:

Processes Burst Time


P0 10
P1 4
P2 6
P3 8
Assume that all the processes arrive at time 0 in sequence P0, P1, P2 P3

26 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

#include <stdio.h>
int main(){ // calculating burst time for each process
int n = 4 ; // number of processes for(int i = 0; i<n; i++){
int bTime[4] ; tATime[i] = wTime[i]+bTime[i] ;
int wTime[4] ; avgtATime = avgtATime + tATime[i] ;
int tATime[4] ; }
bTime[0] = 10 ; avgWTime = avgWTime / n;
bTime[1] = 4 ; avgtATime = avgtATime / n ;
bTime[2] = 6 ; // displaying waiting time and turnaround times
bTime[3] = 8 ; for (int i =0; i<n; i++){
int avgWTime = 0 ; printf("P%i: " , i) ;
int avgtATime = 0 ; printf("Wait Time: %i\t", wTime[i]) ;
wTime[0] = 0 ; printf("Turnaround Time: %i\n", tATime[i]) ;
// calculating waiting time for each process }
// displaying average waiting time and average turnaround
for(int i = 1; i<n; i++){ time

27 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

wTime[i] = wTime[i-1]+bTime[i-1] ; printf("\nAverage Wait time: %i\n", avgWTime) ;


avgWTime = avgWTime + wTime[i] ; printf("\nAverage Turnaround time: %i\n\n", avgtATime) ;
} return 0;
}
// end of main()

28 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

Operating Systems Instructor:


Miss. Afrozah Nadeem
Lab 10

Date:
Time Allowed :120 min

Lab 10 Implementing Round Robin


scheduling algorithm in C

Write a program in C to implement Round Robin scheduling algorithm. Given the list of
Processes and their burst time, display the Gantt chart. For example, the list of processes
and their CPU burst time are as follows:

Processes Burst Time


P0 10
P1 4
P2 6
P3 8

Assume that all the processes arrive at time 0 in sequence P0, P1, P2 P3. Time slice for
each process is 5.

29 | P a g e

#include <stdio.h> int flag ;


int main(){ printf("Gantt chart for given processes and burst
int n = 4 ; times:\n\n");
int timeSlice= 5 ; do{
int tSlice[4] ; flag = 0 ;
int bTime[4] ; for ( int i = 0 ; i<n; i++ ){
bTime[0] = 10 ; for (int j = 0 ; j<tSlice[i]; j++ ){
bTime[1] = 4 ; printf("P%d", i );
bTime[2] = 6 ; bTime[i]-- ;
Operating Systems Lab manual for BSSE
Department of CS, LGU
bTime[3] = 8 ; }
for (int i = 0 ; i<n ; i++ ){ tSlice[i] = bTime[i];
if (bTime[i] < timeSlice || printf("\t");
bTime[i] == 0) }
{
tSlice[i] = bTime[i]; for ( int i = 0 ; i<n; i++ ){
} if (bTime[i] != 0)
else {
{ flag = 1 ;
tSlice[i] = timeSlice ; }
} }
} }while (flag != 0);
return 0;
}
// end of main()

30 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

Operating Systems Instructor:


Miss. Afrozah Nadeem
Lab 11
Date: Time
Allowed : 120 min

Lab 11 Implementing Banker’s Algorithm for


Deadlock avoidance in C

In this algorithm, when a new process enters the system, it must declare the maximum
number of instances of each resource type that it may need, i.e., each process must a
priori claim maximum use of various system resources. This number may not exceed the
total number of instances of resources in the system, and there can be multiple
31 | P a g e
instances of resources. When a process requests a set of resources, the system must
determine whether the allocation of these resources will leave the system in a safe state.
If it will, the resources are allocated; otherwise the process must wait until some other
process releases enough resources. We say that a system is in a safe state if all of the
processes in the system can be executed to termination in some order; the order of
process termination is called safe sequence. When a process gets all its resources, it
must use them and return them in a finite amount of time. Let n be the number of
processes in the system and m be the number of resource types. We need the following
data structures in the Banker’s algorithm:

Available: A vector of length m. It shows number of available resources of each type. If


Available[i] = k, then k instances of resource Ri are available.
Max: An n×m matrix that contain maximum demand of each process. If Max[i,j] = k, then
process Pi can request maximum k instances of resource type Rj.
Operating Systems Lab manual for BSSE
Department of CS, LGU
Allocation: An n×m matrix that contain number of resources of each type currently
allocated to each process. If Allocation[i,j] = k, then Pi is currently allocated k instances
of resource type Rj.
Need: An n×m matrix that shows the remaining resource need of each process. If
Need[i,j] = k, then process Pi may need k more instances of resource type Rj to
complete the task.

The output of program is as shown below:

32 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

33 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU

#include<stdlib.h>
#include <stdio.h>
int current[5][5], maximum_claim[5][5], available[5];
int allocation[5] = {0, 0, 0, 0, 0};
int maxres[5], running[5], safe = 0;
int counter = 0, i, j, exec, resources, processes, k = 1;
34 | P a g e

int main()
{
printf("\nEnter number of processes: ");
scanf("%d", &processes);

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


{
running[i] = 1;
counter++;
}

printf("\nEnter number of resources: ");


Operating Systems Lab manual for BSSE
Department of CS, LGU
scanf("%d", &resources);

printf("\nEnter Claim Vector:");


for (i = 0; i < resources; i++)
{
scanf("%d", &maxres[i]);
}

printf("\nEnter Allocated Resource Table:\n");


for (i = 0; i < processes; i++)
{
for(j = 0; j < resources; j++)
{
scanf("%d", &current[i][j]);
}
}

printf("\nEnter Maximum Claim Table:\n");


for (i = 0; i < processes; i++)
{
for(j = 0; j < resources; j++)
{
scanf("%d", &maximum_claim[i][j]);
}
35 | P a g e
}

printf("\nThe Claim Vector is: ");


for (i = 0; i < resources; i++)
{
printf("\t%d", maxres[i]);
}

printf("\nThe Allocated Resource Table:\n");


for (i = 0; i < processes; i++)
{
for (j = 0; j < resources; j++)
{
printf("\t%d", current[i][j]);
Operating Systems Lab manual for BSSE
Department of CS, LGU
}
printf("\n");
}

printf("\nThe Maximum Claim Table:\n");


for (i = 0; i < processes; i++)
{

for (j = 0; j < resources; j++)

{
printf("\t%d", maximum_claim[i][j]);
}
printf("\n");
}

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


{
for (j = 0; j < resources; j++)
{
allocation[j] += current[i][j];
}
}
36 | P a g e

printf("\nAllocated resources:");
for (i = 0; i < resources; i++)
{
printf("\t%d", allocation[i]);
}

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


{
available[i] = maxres[i] - allocation[i];
}

printf("\nAvailable resources:");
for (i = 0; i < resources; i++)
Operating Systems Lab manual for BSSE
Department of CS, LGU
{
printf("\t%d", available[i]);
}
printf("\n");

while (counter != 0)
{
safe = 0;
for (i = 0; i < processes; i++)
{
if (running[i])
{
exec = 1;
for (j = 0; j < resources; j++)
{
if (maximum_claim[i][j] - current[i][j] > available[j])
{
exec = 0;
break;
}
}
if (exec)
{
printf("\nProcess%d is executing\n", i + 1);
37 | P a g e
running[i] = 0;
counter--;
safe = 1;

for (j = 0; j < resources; j++)

{
available[j] += current[i][j];
}
break;
} }
}
if (!safe)
{
printf("\nThe processes are in unsafe state.\n");
Operating Systems Lab manual for BSSE
Department of CS, LGU
break;
} else {
printf("\nThe process is in safe state");
printf("\nAvailable vector:");

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


{
printf("\t%d", available[i]);
}

printf("\n");
}
}
system(“pause”);}

38 | P a g e

Potrebbero piacerti anche