A8517-Operating Systems Laboratory
A8517-Operating Systems Laboratory
(AUTONOMOUS)
Affiliated to JNTUH, approved by AICTE, Accredited by NAAC with A++ Grade
ISO 9001:2015 Certified
Kacharam, Shamshabad, Hyderabad – 501218, Telangana, India
Laboratory Manual
Operating Systems Laboratory
(II B. Tech- II SEMESTER)
(VCE-R22)
Course Code-A8517
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
PO11
PO12
PSO1
PSO2
CO#/
PO1
PO2
PO3
PO4
PO5
PO6
PO7
PO8
PO9
POs
A8517.1 3 2 2 2
A8517.2 3 2 2
A8517.3 3 2 2 2
A8517.4 3 2 2
Note: 1-Low, 2-Medium, 3-High
LIST OF PROGRAMS FOR PRACTICE:
Tools and
No Title of the Experiment Expected Skills/Ability
Techniques
Practice commands - like mkdir, rmdir,
1
cat,nl, ls, cp, mv, rm, man.
Practice commands - like wc, uniq,
Identify the various File
2 comm, cmp, diff, ln, unlink, chmod, du,
handling Utilities
df.
Practice commands - head, tail, sort,
3
grep, egrep, fgrep, cut, paste, join.
Process Management System calls fork Examine process
4
(), exec () and wait (). creation and execution
a) Two-way Communication using Pipes.
5 Implement IPC
b) Process Communication using FIFOs.
mechanisms using UNIX
6 Implement Shared Memory form of IPC. API
7 Implement Message Queue form of IPC. Open source
Ubuntu/Linux
Use UNIX/LINUX system
Operating
calls to simulate
8 Simulate cp, head and tail commands. System.
commands
C/C++
Compiler
Shell Script programs using control Write Shell Script
9
statements. Programs
Shell Script programs using control Write Shell Script
10
statements. Programs
Implement Page
Program to implement FIFO AND LRU
11 Replacement
Page replacement algorithms.
algorithms.
Program to implement FCFS and SSTF Implement Disk
12
Disk Scheduling algorithm. Scheduling algorithms.
Implement Process
13 Implement Semaphore form of IPC. Synchronization using
Semaphores
ASSESSMENT SCHEME R22
Max. Marks
S.NO# EVALUATION METHOD ASSESSMENT TOOL
Marks Total
A write-up on day-to-day experiment in
10
the laboratory
Viva-voce (or) tutorial (or) case study (or)
application (or) poster 10
Continuous Internal
presentation of the course
Evaluation (CIE)
1 40
Internal practical examination 10
Laboratory Project, which consists of the
Design (or) Software / Hardware Model
10
Presentation (or) App Developm (or)
Prototype Presentation
Write-up 10
Experiment/program 15
Semester End Evaluation of results 15
2 60
Examination (SEE)
Presentation on another
10
experiment/program
Viva-Voce 10
LAB SESSION PLAN
CO BLOOM’s
No Title of the Experiment
LEVEL
Practice commands - like mkdir, rmdir, cat,nl, ls, cp, CO-1 L3
1
mv, rm, man.
Practice commands - like wc, uniq, comm, cmp, diff, ln, CO-1 L3
2
unlink, chmod, du, df.
Practice commands - head, tail, sort, grep, egrep, CO-1 L3
3
fgrep, cut, paste, join.
Process Management System calls fork (), exec () and CO-2 L3
4
wait ().
a) Two-way Communication using Pipes. CO-2 L3
5
b) Process Communication using FIFOs.
6 Implement Shared Memory form of IPC. CO-2 L3
7 Implement Message Queue form of IPC. CO-2 L3
8 Simulate cp, head and tail commands. CO-3 L3
9 Shell Script programs using control statements. CO-3 L3
10 Shell Script programs using control statements. CO-3 L3
Program to implement FIFO AND LRU Page CO-4 L3
11
replacement algorithms.
Program to implement FCFS and SSTF Disk Scheduling CO-4 L3
12
algorithm.
13 Implement Semaphore form of IPC. CO-2 L3
Introduction to UNIX
What is UNIX?
There are many different versions of UNIX, although they share common similarities.
1. BSD (Berkeley Software Distribution) UNIX
2. SUN SOLARIS (Sun Microsystems) [Servers & Work Stations]
3. NOVELL NETWARE (Novell)
4. IBM AIX (International Business Machines)
5. HP – UX (Hewllet Packard)
6. TRU 64 (Hewllet Packard)
7. MacOS X
8. GNU/Linux
9. Fedora Linux [servers and desktop PCs]
Advantages of UNIX
Full multitasking with protected memory. Multiple users can run multiple
programs each at the same time without interfering with each other or crashing
the system.i.e. Multi User and Multi-Tasking System.
Most of UNIX flavors are Open Source. (Freeware)
UNIX is Extensible and Scalable Operating System.
Very efficient virtual memory, so many programs can run with a modest
amount of physical memory.
Access controls and security.
A rich set of small commands and utilities that do specific tasks
Ability to string (combine) commands and utilities together in unlimited ways
to accomplish more complicated tasks .
A powerfully unified file system. Everything is a file: data, programs, all
physical devices and Directories.
A lean kernel that directly communicate with Hardware.
Available on a wide variety of machines - the most truly portable operating
system.
Disadvantages of UNIX
The traditional command line shell interface is user hostile -- designed for the
programmer, not for the casual user.
Commands often have cryptic names and give very little response to tell the
user what they are doing. Much use of special keyboard characters.
To use UNIX well, you need to understand some of the main design features. Its
power comes from knowing how to make commands and programs interact
with each other.
Richness of utilities (over 400 standard ones) often overwhelms novices.
Documentation is short on examples and tutorials to help you figure out how
to use the many tools provided to accomplish various kinds of tasks.
UNIX BASIC COMMANDS
9 man (manual)
Used to display the user manual of any -k : Displays the related commands.
command that we can run on the terminal.
It is used to obtain offline documentation of $man [OPTION]... [COMMAND NAME]...
any command. $ man printf
It provides a detailed view of the command $man cat
which includes name, synopsis, description, $man –k sort
options, exit status, return values, errors,
files, versions, examples, authors and see
also.
If no options, It displays the whole manual
of the command.
In Output
c – Change, d – Delete, a- Append
head myfile1 //displays top 10 lines -q: It is used if more than 1 file is given. Because
head myfile1 myfile2 of this command, data from each file is not
//displays top 10 lines from both the files proceeds by its file name.
$head -4 myfile1 //display top 4 lines $head -q –n 5 myfile1 myfile2
if(pid < 0)
{
printf("\n Fork failed to create a child \n");
return 1;
}
else if(pid > 0) // Parent process
{
printf("\n The parent process id is %d \n" , getpid());
printf("\n Parent Process Running \n");
wait(NULL); //Parent waits for child to complete
printf("\n Child Completed \n");
}
else
{
printf(" \n Child Process running \n");
printf("\n The child process id is %d",getpid());
printf("\n The parent of child is %d" , getppid());
execlp("/bin/ls" ,"ls", “-l” ,NULL);
}
return 0;
}
1. What are the header files required to use unix process system calls?
2. The wait () system call gets status from …………………
3. getppid() returns the process id of invoking ………………………process.
Post-Lab
4. What happens when we invoke fork () system call?
Questions 5. Is it possible to assign a new task to child process? If so, demonstrate with an
example.
6. How to create a child process in UNIX?
a) Two-way Communication using Pipes.
Week-5
b) Process Communication using FIFOs.
1. If a process calls fork() system call n-times. How many child processes are
created.?
2. If a process calls fork() system call 4 times. How many total number of processes
Pre-Lab
concurrently executed in the system?
Questions 3. Does exec() call can be made in parent process? If so, what is the need of it.
4. What is the return value of fork() system call in parent and child process?
5. When a process is terminated from the system?
if(pipe(p2fd)==-1) {
printf("pipe failed\n");
return;
}
pid=fork();
if(pid<0)
{
printf("fork failed\n");
return;
}
if(pid>0)//parent
{
close(p2fd[1]);
close(p1fd[0]);
printf("enter the msg to child (Client)--->Parent\n");
scanf("%s",msg);
write(p1fd[1],msg,strlen(msg)+1);
sleep(5);//sleep of 5 seconds
read(p2fd[0],msg,100);
printf("the msg given by child (client) is %s --->Parent\n",msg);
}
else
{
close(p2fd[0]);
close(p1fd[1]);
sleep(3);
read(p1fd[0],msg,100);
printf("the msg given by parent is %s --->child\n",msg);
sleep(3);//sleep of 3 seconds
printf("enter the msg to parent --->child\n");
scanf("%s",msg);
write(p2fd[1],msg,strlen(msg)+1);
}
}
key = 5678;
}
//Shared memory - Access Process – shmaccess.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define SHMSZ 27
void main(){
int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
printf("shared memory cannot be opened");
exit(1);
}
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("Can not be attached to shared memory");
exit(1);
}
//Read Alphabets from shared memory
for (s = shm; *s != NULL; s++)
putchar(*s);
putchar('\n');
int main()
{
int msqid;
int msgflg = IPC_CREAT|0666;
key_t key;
message_buf sbuf;
size_t buf_length;
char msg[100];
key = 1234;
int main()
{
int msqid;
key_t key;
message_buf rbuf;
key = 1234;
if ((msqid = msgget(key, 0666)) < 0)
{
printf("msgget- can not open Message Queue");
exit(1);
}
exit(0);
}
1. Identify the system calls needed for implementing message queues in UNIX?
2. Write the syntax to create and open a message queue?
Post-Lab 3. How to perform read and write operations on message queue?
Questions 4. What is the return value of msgget() system call?
5. How to delete a message queue using unix system call?
6. Differentiate between shared memory and message queues.
Simulate cp, head and tail utilities using file handling
Week-8
system calls.
1. How the cp command works?
2. List various options of cp command?
Pre-Lab
3. How to display beginning lines of a file?
Questions 4. How to display the ending lines of a file?
5. What are command line arguments?
//cp command simulation
#include <stdlib.h>
int main(int argc , int argv[]) {
char ch;
FILE *fp1, *fp2;
if (fp1 == NULL) {
printf("Press any key to exit...\n");
exit(1);
}
fp2 = fopen(argv[2], "w");
if (target == NULL) {
fclose(source);
printf("Press any key to exit...\n");
exit(0);
}
while ((ch = fgetc(source))! = EOF)
fputc(ch, target);
printf("File copied successfully.\n");
fclose(source);
fclose(target);
return 0;
}
for(i=0; i<m;i++)
mf[i] =-1;
if(k==m)
printf("\t\tThe page fault no. is %d \n" , pf);
printf("\n");
if(count == m)
count =0;
}
printf(" The total page faults is using FIFO replacement is ---> %d \n ", pf);
}
LRU Page replacement algorithm
#include<stdio.h>
void main() {
int i,j,k,min;
int rs[25], flag[25];
int n,f,pf=0,next=1;
int count[20], m[20];
printf("Enter the length of reference string \n ");
scanf("%d",&n);
printf("\n Enter the reference string --->\n");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]);
flag[i] = 0;
}
printf(" \n Enter the number of frames \n");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i] = 0;
m[i] = -1;
}
printf("\n The Page Replacement process is --->\n" );
for(i=0;i<n;i++) {
for(j=0;j<f;j++) {
if(m[j]==rs[i]) {
flag[i] = 1;
count[j]= next;
next++;
}
if(flag[i] == 0)
{
if(i <f )
{
m[i] = rs[i];
count[i]= next;
next++;
}
else
{
min = 0;
for(j=1;j<f;j++)
if(count[min] > count[j])
min = j;
m[min] = rs[i];
count[min] = next;
next++;
}
pf++;
}
for(j=0 ; j<f;j++)
printf("%d \t",m[j]);
if(flag[i] == 0)
printf(" The Page Fault num is ...>%d" ,pf);
printf("\n");
}
printf("\n The number of page faults using LRU is ---> %d \n" , pf);
}
total_dist += distance;
first = current;
}
printf("Total seek distance is %d" , total_dist);
printf("\n The Seek Sequence is \n");
for (i = 0; i < n; i++) {
printf("%d ",arr[i]);
}
}
int main(){
int arr[100];
int n,i;
int start ;
printf("Enter the total number of cylinder requests in queue");
scanf("%d", &n);
printf("Enter the cylinder numbers requested");
for(i=0;i<n;i++)
scanf("%d", &arr[i]);
printf("Enter the initial head position");
scanf("%d",&start);
fcfs_disk_scheduling(arr, n, start);
return 0;
}
Output:
Enter the total number of cylinder requests in queue 8
Enter the cylinder numbers requested 98
183
37
122
14
124
65
67
Enter the initial head position53
Total seek distance is 640
The Seek Sequence is
98 183 37 122 14 124 65 67
int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");