Sie sind auf Seite 1von 14

ADVANCED OPERATING SYSTEM

Lab Task II
Abstract
This assignment is consists of practical implementation of process API, child and parent control and system calls.

Muhammad Nabeel Sarwar


Nabeel.sarwar.nabeel@gmail.com
Lab Task 2
Advanced Operating Systems
File P3.c is attached. It folks a process and then exchanges the child process with “wc” or word
count process provided by a standard library. Syntax shows how to pass arguments to the wc
process.

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?
What happens to the variable when both the child and parent change the value of x?

Solution:
A copy of parent process is created using System call fork(). However, The address space of both
child and parent process is private address space and is exclusive of each other. The Adress space of
both processes do not interfere with each other. this implies that both child and parent processes
maintain own variables (memory locations).

Program:
Output:

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()? What
happens when they are writing to the file concurrently, i.e., at the same time?

Solution:
System call Open() opens a file descriptor, and both parent and child process can access it. Both can
write to the file but If we do not use wait() order of their file writing is un- deterministic.

Note: Possibility is that only one process was able to write overwriting the other one.

Program:
Output:

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?

Solution:
Yes, without system call wait() it is possible that child process always print first.

Program:
Output:

4. Write a program that calls fork() and then calls some form of exec() to run the program /bin/ls.
See if you can try all of the variants of exec(), including (on Linux) execl(), execle(), execlp(),
execv(), execvp(), and execvpe(). Why do you think there are so many variants of the same
basic call?

Solution:
Each variant has its own purpose even though with smaller modifications they can be used inplace of the
other.
### Rules

1. arguments are comma separated in Functions having “l” in their name


1. arguments are” (v ->) vector arrays in Function having v” in their names
Function having **e** need environment argument

Program:

Output:
5. Now write a program that uses wait () to wait for the child process to finish
in the parent. What does wait() return? What happens if you use wait ()
in the child?

Solution:

On successful wait() system call, it returns the process ID of terminated child while on error it returns -1.

Program:
Output:

6. Write a slight modification of the previous program, this time using waitpid () instead of wait().
When would waitpid() be useful?
Solution:

When we want to wait for specific child process instead of waiting for all child processes for exiting waitpid()
is useful. It permits us to be more specific about behaviours of processes.

Program:
Output:

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?

Solution:
After closing the file descriptor (STDOUT), printf() do not write anything on the screen.
Program:
Output:

8. Write a program that creates a child, and connects the standard output
of the child to a file using the indirection operator >.
For clarity you have to write code C that will execute the following command
shell#> wc p3.c > newfile.txt
the command is in bold! This simply means that the word count will now by conducted over file
p3.c and and instead of writing the result back to the terminal the result will be directed to a file
called newfile.txt.
Solution:
Program:
Output:
Actual output:
Terminal output:

9. Write a program that creates two children, and connects the standard output of one to the standard
input of the other, using the pipe() system call.
(You have to look into Pipes for Linux denoted by ‘I’ a vertical bar!)

Program:
Output:

Das könnte Ihnen auch gefallen