National University of Computer and Emerging Sciences, Lahore Campus Course: Program: Duration: Paper Date: Section: Exam:
Operating Systems BS(Computer Science) 1 Hour and 40 minutes 19-March-2018 All Section Lab Mid Exam
Course Code: Semester: Total Marks: Weight Page(s):
CS205 Spring 2018 40 20 1
Instruction/Notes: 1. You must ensure that you have made proper submission of your code. No Issues will be entertained later on. 2. You have to write code for all questions on your assigned PCs. 3. Discussion with other students is not allowed. 4. Use of the Internet is strictly prohibited. 5. Plagiarism will result in F grade in lab.
Question # 1 (Basic Shell commands + Process Controls)
(15)
Write a program which create n number of main directories / folder and n number of sub directories / sub folders in each main directory / folder. Number and names of main directories and number and names of sub directories are passed as command line argument. Create all folders in current working directory, or you can make them at /home/oracle/Desktop. For example, ./Lab_mid_q1 3 mid1 2 q1 q2 mid2 2 q1 q2 mid3 1 q1 For above example, your program should create 3 main folders “mid1”, “mid2” and “mid3”. “mid1” has 2 sub-folders “q1” and “q2” “mid2” has 2 sub-folders “q1” and “q2” “mid3” has 1 sub-folder “q1”. Use mkdir and execv system calls. Use man command if you don’t know how to use these system calls.
Question # 2 (Pipes)
(15)
Write a main program which have 3 children. Child 1 will read data from a file and write them on a pipe. Child2 of parent will change the case of data on that pipe and write them on another pipe. Then child3 of parent will read those values from pipe and write them on output file.
Question # 3 (Threads)
(10)
You need to write a program to search an array using concurrent processes. Following is the code skeleton: int main() { const int N = ...; int a[N] = ...; int key, i; cout << "Enter a number: "; cin >> key; // Write code here to search the array cout << "The number found at index: " << i; return 0; } The program searches the array for a given number, and prints the index of the number in the array. If the number is not found then the program shall output -1. You are required to use two threads to speed up the search. The first thread shall search the first half of the array, while the second shall examine the other half. If any thread found element other should terminate immediately. You need not to use any fancy algorithm for the search; rather you can use the simple linear search. Assume no duplicates are there in the array. You can create only 1 Boolean global variable, and as many static and dynamic variables.