National University of Computer and Emerging Sciences
Laboratory Manual for Operating Systems Lab (CS 205)
Course Instructor
Mr. Ibarhim Nadir
Lab Instructor(s)
Mr. Asad Ullag Ms. Syeda Farwa Batool
Section
CS-A
Semester
Spring 2019 Department of Computer Science FAST-NU, Lahore, Pakistan
Page 1 of 4
Spring 2019
OS-A
Objectives After performing this lab, students shall be able to:
Understand and create Makefile Understand I/O redirection Important Note:
Comment your code intelligently. Indent your code properly. Use meaningful variable names. Use meaningful prompt lines/labels for input/output. Use meaningful project and C/C++ file name
Input/output Redirection: To understand input/output redirection, we need to understand named pipes and dup2 system call first. 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. II. III.
ls | sort ls | sort > 1.txt ls | grep a
This will help: execlp("sort","sort",NULL); (use filing)
MakeFile
Make is Unix utility that is designed to start execution of a makefile. A makefile is a special file, containing shell commands, that you create and name makefile (or Makefile depending upon the system). While in the directory containing this makefile, you will type make and the commands in the makefile will be executed. If you create more than one makefile, be certain you are in the correct directory before typing make. Make keeps track of the last time files (normally object files) were updated and only updates those files which are required (ones containing changes) to keep the sourcefile up-to-date. If you have a large program with many source and/or header files, when you change a file on which others depend, you must recompile all the dependent files. Without a makefile, this is an extremely time-consuming task. As a makefile is a list of shell commands, it must be written for the shell which will process the makefile. A makefile that works well in one shell may not execute properly in another shell.
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