Master of Science in Computer Science - Computer Science Introductory
Course
Laboratory
Work 1: Processes
Translated
from a French version designed by Bertrand Dupouy
I.A
number of usefull shell commands
- Try the following commands:
who (list of users logged in)
who am i
ps -l (list of processes attached to the terminal)
ps -axl (list of running processes)
identify
the following processes : - init with
pid 1 and the swapper-scheduler with pid 0.
- daemons
(marked with ?),
- those executing
a shell (sh, csh ou ksh),
- Sending a command in the background ( use &) :
Sleep n results in the calling process pausing for n seconds.
Send
the following command sequence and observe the results.
ps -l and
sleep 20& and
sleep 30& and
ps -l
- Using exec.
- type: exec ps
What
happens ? Why ?
Hint : for exec, as for
other commands (built in commands) the shell does
not create a new process (does not call fork).
- After finding the pid of the process runing the
current shell, do: exec sh
What is the pid
of the process that executes sh
? Why ?
II.Introduction to programming in this environment
Create a directory in which you will do
subsequent work
cd To move to your home directory
mkdir TPproc
cd TPproc
To compile your program files, use the gcc command
(if the file to be comiled is named exo1.c :
gcc exo1.c -o exo1
or
gcc -Wall exo1.c -o exo1
III.
Exercises on process creation (fork)
III.1 Functions used
fork()
This function creates a new process. It returns a value
n that indicates: :
n > 0 |
we are in the parent process |
n = 0 | we are in the child process |
n = -1 | fork failed and no
process was created |
getpid():
returns the calling process identifier (pid)
getppid(): returns the parent pid
III.2 Exercise 1
- After compiling exo1.c, execute
exo1 several time.
What happens ? Why ?
To get a copy of exo1.c,
click here
- Add the following instruction after the fork
call:
if (value == 0) sleep (4);
What
happens ? Why ?
III. 3 Exercise 2
To get a copy of fork-mul.c,
click here.
Compile fork-mul.c, and execute it.
After execution and using the following diagram, note the
process numbers and note the order in which the printf
are executed in order to find the order in which processes are
executed.
Note:
If you see a process whose pid is 1, it
is the init process that is the parent of
all processes. Init adopts all orphan
processes (those whose parent terminated).
©(Copyright)
Bertrand Dupouy- Contact : Petr Kuznetsov (surname . name @ telecom-paristech.fr)
- Last Modified, september 26, 2013.