System Calls Process Creation Termination
System Calls Process Creation Termination
DEFINITION: A system call is a procedure that provides the interface between a process and the
operating system.
As shown in diagram above, (1) a process is executing in user mode until a request is made via a
system call. (2) The system call is then executed on a priority basis in the kernel mode. (3) After the
execution of the system call is over, the control returns to the user mode and (4) the execution of
the user process is resumed in user mode.
To the programmer, the system call appears as a normal C function call. However, since a system
call executes code in the kernel, there must be a mechanism to change the mode of a process from
user mode to kernel mode. The C compiler uses a predefined library of functions (the C library) that
have the names of the system calls. The library functions typically invoke an instruction that changes
the process execution mode to kernel mode and causes the kernel to start executing code for
system calls. The instruction that causes the mode change is often referred to as an "operating
system trap" which is a software generated interrupt.
Pg. 1 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002
2 Process Management
Basic process management is done with a number of system calls, each with a single, simple
purpose. The following table is a list of the more important system calls in Unix related to Processes.
These system calls can be combined to implement more complex behaviours, however for this
lesson we will examine the system calls related to process creation and termination.
Pg. 2 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002
2.1 fork()
Processes use this system call to create processes that are a copy of themselves. The fork system call
does not take an argument. The process that invokes the fork() is known as the parent and the new
process is called the child. If the fork system call fails, it will return -1. If the fork system call is
successful, the process ID of the child process is returned in the parent process and a 0 is returned in
the child process.
Note: The execution of the parent process will be suspended till the child process executes.
2.1.1 Example
The following is a simple example of fork. It will allow the parent process to print Hello,
before calling fork(). At this point a child process will be created and print bye (with return
number), before doing the same for the parent process.
#include <stdio.h>
#include <unistd.h> Output
int main(void)
{
printf("Hello \n");
int pn = fork ();
printf("bye PID:%d\n", pn);
return 0;
}
When a fork() system call is made, the OS generates a copy of the parent process which becomes the
child process. The OS will pass to the child process most of the parent's process information.
However, some information is unique to the child process:
Every time fork() is called the total number of processes is 2n, where n is number of fork system calls.
Pg. 3 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002
2.2 exec()
This system call runs when an executable file in the context of an already running process that
replaces the older executable file. However, the original process identifier remains as a new process
is not built, but stack, data, head, data, etc. are replaced by the new process. If successful,
the exec system calls do not return to the invoking program as the calling image is lost.
There’s not a syscall under the name exec(). By exec()we usually refer to a family of calls:
int execl(char *path, char *arg, ...);
int execv(char *path, char *argv[]);
int execle(char *path, char *arg, ..., char *envp[]);
int execve(char *path, char *argv[], char *envp[]);
int execlp(char *file, char *arg, ...);
int execvp(char *file, char *argv[]);
2.2.1 Example
The parent code on the left is called ex1.c, when executed it will print the process ID of
parent. It will then call the child process on the left to execute. This exec() call will allow the
child to overwrite the parent space but keep the parent’s original process ID.
#include <stdio.h>
#include <stdio.h>
#include <unistd.h> #include <unistd.h>
int main() int main()
{
{ printf ("Now in ex2.c\n");
printf ("PID of ex2.c: %d\n", getpid());
printf ("PID of ex1.c: %d\n", getpid()); }
char *args[]={"./ex2",NULL};
execv(args[0],args);
2.3 wait()
In some systems, a process needs to wait for another process to complete its execution. This type of
situation occurs when a parent process creates a child process, and the execution of the parent
process remains suspended until its child process executes. The suspension of the parent process
automatically occurs with a wait() system call. When the child process terminates, it returns an exit
status to the OS, which is then returned to the waiting parent process. The parent process then
resumes execution.
A child process that terminates but is never waited on by its parent becomes a zombie process. Such
a process continues to exist as an entry in the system process table even though it is no longer an
actively executing program. Such situations are typically handled with a special "reaper" process that
Pg. 4 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002
locates zombies and retrieves their exit status, allowing the operating system to then de-allocate
their resources.
2.4 exit():
The exit() system call is used to terminate program execution. Specially in the multi-threaded
environment, this call defines that the thread execution is complete. The OS reclaims resources that
were used by the process after the use of exit() system call. Image below shows the exit() system call
also putting the process into the zombie state.
3 Activity
a. Investigate the fork code below. Why does the PARENT prints after the CHILD. How could get a
reverse effect.
1. #include <stdio.h>
2. #include <unistd.h>
3. #include <sys/wait.h>
4.
5. #define SIZE 5
6. int nums[SIZE] = {0,1,2,3,4};
7.
8. int main()
9. {
10. int i;
11. pid_t pid;
12. pid = fork();
13. if (pid == 0) {
14. for (i = 0; i < SIZE; i++) {
15. nums[i] *= -i;
16. printf("CHILD: %d ",nums[i]);
17. }
18. printf("\n");
19. }
20. else if (pid > 0) {
21. wait(NULL);
22. for (i = 0; i < SIZE; i++)
23. printf("PARENT: %d ",nums[i]);
24. printf("\n");
25. }
26. return 0;
27. }
Pg. 5 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002
b. How many lines with “Hello”. Draw fork() tree.
int doWork(){
fork();
fork();
printf("Hello world!\n");
}
int main() {
doWork();
printf("Hello\n");
exit(0);
}
c. In the bolded line below explain how the wait() system call works.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(){
pid_t pid = fork();
if (pid == 0) {
printf("HC: hello from child\n");
exit(17);
} else {
int child_status;
printf("HP: hello from parent\n");
waitpid(pid, &child_status, 0);
printf("CT: child result %d\n", WEXITSTATUS(child_status));
}
printf("Bye\n");
return 0;
}
4 References
Department of information technology, Uppsala university. (2018). Process management. Retrieved
from Operating systems:
http://www.it.uu.se/education/course/homepage/os/vt18/module-2/process-
management/
Guru99. (2021, August 22). System Call Operating System. Retrieved from Guru99:
https://www.guru99.com/system-call-operating-system.html
The Informatics Department of the University of Évora. (2021). UNIX System Calls. Retrieved from
http://www.di.uevora.pt/~lmr/syscalls.html
Pg. 6 of 6