You are on page 1of 7

LAB 2

Advance Operating System

Submitted by:

M Rauf Tabassam
MSCS18030
Spring – 2019

2/15/2019
Lab 2 – Advance Operating System
Program 1:
Write a program that calls fork(). Before calling fork(), have the main process access a variable (e.g., x)
and set its value to something (e.g., 100).

What value is the variable in the child process?


The value of variable is same as assigned in parent process to it.

What happens to the variable when both the child and parent change the value
of x?
Child process has its own x and parent process has its own. Both changing variable values don’t impact
on their own variable.

RESULT of code is as follows:

M Rauf Tabassam
MSCS18030
Program 2:
Write a program that opens a file (with the open() system call) and then calls fork() to create a new
process.

Can both the child and parent access the file descriptor returned by open()?
Yes, both child and parent can access the file returned by the open().

What happens when they are writing to the file concurrently, i.e., at the same
time?
Both processes can’t write on file at same time but turn by turn.

RESULTS of code is as follows:

File Contains:

Hello, I am child process.


Hello, I am parent process.

M Rauf Tabassam
MSCS18030
Program 3:

Write another program using fork(). The child process should print “hello”; the parent process should
print “goodbye”. You should try to ensure that the child process always prints first;

Can you do this without calling wait() in the parent?


Yes, we can do this by keeping parent process on sleep. But it is only suitable when we know our child
process will take time less than sleep time of parent process.

M Rauf Tabassam
MSCS18030
Program 5:
Now write a program that uses wait() to wait for the child process to finish in the parent.

What does wait() return?


wait() function returns the process ID of terminated child, if there is no child it returns -1.

What happens if you use wait() in the child?


When we use wait() in child, it returns -1 as it have no child of it. The statements of child executed and no
error is generated.

RESULTS of code is as follows:

M Rauf Tabassam
MSCS18030
Program 6:
Write a slight modification of the previous program, this time using waitpid() instead of wait().

When would waitpid() be useful?


waitpid() is used when we want to wait or a specific child process rather than waiting for all child
processes to exit. It also allows us to specify more behaviors.

waitpid() parameters are as follows:

first argument: pid_t pid


It is the pid of process for which you Wait for the process to exit

second argument : int *status


it is the current status of the program, you can use the macros to test for each condition, such as
WIFEXITED , WIFSIGNALED

Third argument: int options


The options argument should be 0.

RESULTS of code is as follows:

M Rauf Tabassam
MSCS18030
Program 7:
Write a program that creates a child process, and then in the child closes standard output (STDOUT
FILENO).

What happens if the child calls printf() to print some output after closing the
descriptor?
If we close stdout file descriptor we can't be able to write something on the screen using printf(). However
no error would occurred.

M Rauf Tabassam
MSCS18030

You might also like