Uploaded by Umer Tahir Studios

17f-8240 Umer Tahir D OS Lab 05

advertisement
National University of Computer & Emerging Sciences
Department of Computer Science
CL 205 Operating System Lab
Lab # 05
Process Creation Part-I
Instructions:
1. Make a word document with the convention “SECTION_ROLLNO _LAB-NO”.
2. You have to submit a Word File containing your shell scripts with comments and
screenshots of their running outputs.
3. Plagiarism is strictly prohibited, negative marks would be given to students who cheat
Objectives – Introduction of fork, exit(), wait(), waitpid(),
getpid() and getppid()
Name Umer Tahir
Roll No: 17f-8240
Section: D
1|Page
Problem statement 1– Write a program to create one parent with three child using only two
fork() function calls where each process find its Id with greeting to its parent process. For
example:
Output:
parent
28808 28809
my id is 28807
First child
0 28810
my id is 28808
Second child
28808 0
my id is 28809
third child
00
2|Page
Q-1 :
3|Page
OUTPUT :
4|Page
Problem statement 2– Write a code which accepts a command-line arguments n and forks a child
process. The parent should calculate the product of numbers 1 to n and the child process should
calculate the sum of numbers 1 to n. Separate your sum and product codes into functions. It should
be written in C.
Q-2
5|Page
OUTPUT:
Problem statement 3– The program declares a counter variable, set to zero, before fork()ing.
After the fork call, we have two processes running in parallel, both incrementing their own version
of counter. Each process will run to completion and exit. Because the processes run in parallel, we
have no way of knowing which will finish first. Running this program will print something similar
to what is shown below, though results may vary from one run to the next. For example:
Output:
--beginning of program
parent process: counter=1
parent process: counter=2
parent process: counter=3
child process: counter=1
parent process: counter=4
child process: counter=2
parent process: counter=5
child process: counter=3
--end of program-child process: counter=4
child process: counter=5
--end of program—
6|Page
Q-3
7|Page
OUTPUT
8|Page
Download