Professional Documents
Culture Documents
Laboratory Manual
for
Operating Systems Lab
(CS 205)
Page 1 of 4
Spring 2019 OS-A
Objectives
After performing this lab, students shall be able to:
Input/output Redirection:
Dup2:
Dup2 system call is used to make alias of a file descriptor, for example:
Int fd=open(“file.txt”, O_WRONLY);
dup2(fd, 1);
cout << “This data will not be printed to the screen, but to the file”, 50);
The above system call will first close file descriptor 1 using close system call. Then, file
descriptor fd’s data will be copied to file descriptor 1. So, every write that is made using fd=1
will then be written to file.txt
Similarly,
Int fd=open(“file.txt”, O_RDONLY);
dup2(fd, 0);
char buffer[10];
cin>>buffer;
Cin will not get input from keyboard, but from file.txt in above piece of code. (Sample code is
given)
On shell, we make the input of one command as the output of another command by using |
symbol.
ls | sort
Page 2 of 4
Spring 2019 OS-A
ls displays directory contents and sort simply sorts the input data. In above command, we are
passing the output of ls command as input to sort command. The output shown on the screen
will be generated by sort command. Behind the scenes, it is done by named pipes and dup2.
To redirect the output of a command to a file, we can use > symbol, such as
Ls > 1.txt
The above command will redirect the output generated by ls command to file 1.txt
Question 1:
Implement a C++ program that executes the command:
I. ls | sort
II. ls | sort > 1.txt
III. ls | grep a
MakeFile
However, for a large project where we have thousands of source code files, it becomes
difficult to maintain the binary builds. The make command allows you to manage large
programs or groups of programs.
Page 3 of 4
Spring 2019 OS-A
The make program is an intelligent utility and works based on the changes you do in your
source files. If you have four files main.cpp, hello.cpp, factorial.cpp and functions.h, then
all the remaining files are dependent on functions.h, and main.cpp is dependent on both
hello.cpp and factorial.cpp. Hence if you make any changes in functions.h, then
the makerecompiles all the source files to generate new object files. However if you
make any change in main.cpp, as this is not dependent of any other file, then only
main.cpp file is recompiled, and help.cpp and factorial.cpp are not.
While compiling a file, the make checks its object file and compares the time stamps. If
source file has a newer time stamp than the object file, then it generates new object file
assuming that the source file has been changed.
Question 2:
Make a header file Calculator.h with 4 functions with prototype
int Add(int a, int b);
int Subtract(int a, int b);
int Mutiply(int a, int b);
int Divide(int a , int b);
Now create four separate cpp files to implement the body of the functions separately. Also create a driver
program that calls all the functions and test their result. Generate Makefile for the above source
files.Generate object files first, and then generate the final executable.
Page 4 of 4