Processes and Fork
Processes and Fork
Processes
In modern operating systems a ton of processes can be running in the background.
To see the processes running in linux use:
What does this mean?
Each task has a Process ID (PID) that uniquely identifies it.
A task is sleeping if it is waiting for a signal from a child process.
Calling Linux System
Functionality in
Important types in:
Important Commands:
getpid -> returns a PID for the calling process as type pid_t (signed int)
execlp -> swaps process with a program on the linux PATH
fork -> generates a new process that has an independent copy of the stack.
Example: Get PID
PID.c
Makes a system call that returns the PID of the calling process.
Example: Running GetPID
externally with execlp
execlp runs the program PID replacing existing process immediately.
What do you think prints?
Fork this Process
exec runs a program but doesn’t create a new process.
This isn’t ideal in multi-threaded environments.
Often what you want to do is start a new process, fork does this.
The code starts for the child at
exactly the point where fork was called
The only difference in memory
is in the return of fork:
0 = child
PID of child = parent
negative = fail
The first forking example
Race Conditions
The order the forked
processes are
executed is not
deterministic.
Most times the parent process will be given priority over the child
but that isn’t guaranteed.
In this example, the result is benign, but what if they both wrote to
a file?
This situation is known as a race condition and we’ll worry about dealing with it next lecture.
/docProps/thumbnail.jpeg