CITS2002 Systems Programming |
CITS2002 | CITS2002 schedule | |||||
Creating a new process using fork()fork() is very unusual because it returns different values in the (existing) parent process, and the (new) child process:
c: child's value of pid=0 c: child's pid=5642 c: child's parent pid=5641 p: parent's value of pid=5642 p: parent's pid=5641 p: parent's parent pid=3244 Of note, calling sleep(1) may help to separate the outputs, and we fflush() in each process to force its output to appear.
CITS2002 Systems Programming, Lecture 9, p1, 19th August 2024.
Where does the first process come from?The last internal action of booting a Unix-based operating system results in the first single 'true' process, named init. init has the process-ID of 1. It is the ancestor process of all subsequent processes. In addition, because the operating system strives to maintain a hierarchical relationship amongst all processes, a process whose parent terminates is 'adopted' by the init process.
CITS2002 Systems Programming, Lecture 9, p2, 19th August 2024.
The general calling sequence of system callsIf a single program has two distinct execution paths/sequences, then the parent and child may run different parts of the same program. Typically the parent will want to know when the child terminates.
CITS2002 Systems Programming, Lecture 9, p3, 19th August 2024.
Waiting for a Process to TerminateThe parent process typically lets the child process execute, but wants to know when the child has terminated, and whether the child terminated successfully or otherwise. A parent process calls the wait() system call to suspend its own execution, and to wait for any of its child processes to terminate. The (new?) syntax &status permits the wait() system call (in the operating system kernel) to modify the calling function's variable. In this way, the parent process is able to receive information about how the child process terminated.
CITS2002 Systems Programming, Lecture 9, p4, 19th August 2024.
Memory in Parent and Child ProcessesThe (existing) parent process and the (new) child process continue their own execution.
CITS2002 Systems Programming, Lecture 9, p5, 19th August 2024.
Running a New ProgramOf course, we do not expect a single program to meet all our computing requirements, or for both parent and child to conveniently execute different paths through the same code, and so we need the ability to commence the execution of new programs after a fork(). Under Unix/Linux, a new program may replace the currently running program. The new program runs as the same process (it has the same pid, confusing!), by overwriting the current process's memory (instructions and data) with the instructions and data of the new program. The single system call execv() requests the execution of a new program as the current process:
On error, -1 is returned, and errno is set appropriately (EACCES, ENOENT, ENOEXEC, ENOMEM, ....). The single system call is supported by a number of library functions (see man execl) which simplify the calling sequence. Typically, the call to execv() (via one of its library interfaces) will be made in a child process, while the parent process continues its execution, and eventually waits for the child to terminate.
CITS2002 Systems Programming, Lecture 9, p6, 19th August 2024.
Why the exit status of a program is importantTo date, we've always used exit(EXIT_FAILURE) when a problem has been detected, or exit(EXIT_SUCCESS) when all has gone well. Why? The operating system is able to use the exit status of a program to determine if it was successful. Consider the following program which exits with the integer status provided as a command-line argument:
CITS2002 Systems Programming, Lecture 9, p7, 19th August 2024.
Why the exit status of a program is important, continuedMost operating system shells are, themselves, programming languages, and they may use a program's exit status to direct control-flow within the shells - thus, the programming language that is the shell, is treating your programs as if they are external functions. Shells are typically programmed using files of commands named shellscripts or command files and these will often have conditional constructs, such as if and while, just like C. It's thus important for our programs to work with the shells that invoke them. We now compile our program, and invoke it with combinations of zero, and non-zero arguments:
CITS2002 Systems Programming, Lecture 9, p8, 19th August 2024.
|