Lab Manual
Lab Manual
Lab Manual
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
4|Page
Operating Systems Lab manual for BSSE
Department of CS, LGU
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
6|Page
Operating Systems Lab manual for BSSE
Department of CS, LGU
7|Page
Operating Systems Lab manual for BSSE
Department of CS, LGU
8|Page
Operating Systems Lab manual for BSSE
Department of CS, LGU
9|Page
Operating Systems Lab manual for BSSE
Department of CS, LGU
• 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
• 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).
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
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
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] ;
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);
}
/* 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
} // end of main()
Lab 8
Date: Time
Allowed :120 min
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()
{
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.
25 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU
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:
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
28 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU
Date:
Time Allowed :120 min
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:
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
30 | P a g e
Operating Systems Lab manual for BSSE
Department of CS, LGU
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:
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);
{
printf("\t%d", maximum_claim[i][j]);
}
printf("\n");
}
printf("\nAllocated resources:");
for (i = 0; i < resources; i++)
{
printf("\t%d", 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;
{
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:");
printf("\n");
}
}
system(“pause”);}
38 | P a g e