0% found this document useful (0 votes)
271 views

System Calls Process Creation Termination

The document discusses system calls related to process creation and termination in operating systems. It defines what a system call is and how it works. It then describes common system calls for process management like fork(), exec(), and wait() that are used to create, run programs, and wait for child processes.

Uploaded by

reuelrichards
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
271 views

System Calls Process Creation Termination

The document discusses system calls related to process creation and termination in operating systems. It defines what a system call is and how it works. It then describes common system calls for process management like fork(), exec(), and wait() that are used to create, run programs, and wait for child processes.

Uploaded by

reuelrichards
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

University of Technology, Jamaica Operating Systems

School of Computing & Information Technology CIT3002

System Calls: Process Creation & Termination


1 What is a System Call?
When a process in user mode requires access to computer resources, it will make a request to the
kernel via a system call. System call provides the services of the operating system (OS) to the user
programs via Application Program Interface (API). It provides an interface between a process and
operating system to allow user-level processes to request services of the operating system. System
calls are the only entry points into the kernel system. All programs needing resources must use
system calls.

DEFINITION: A system call is a procedure that provides the interface between a process and the
operating system.

1.1 How a system call works

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.

1.2 Types of System Calls


In general, system calls are available as assembly language instructions and different operating
systems execute different system calls. There are mainly five types of system calls and within the
table below we can see examples each type under Windows and Linux.

Pg. 1 of 6
University of Technology, Jamaica Operating Systems
School of Computing & Information Technology CIT3002

Types of System Calls Windows Linux


CreateProcess() fork()
Process Management ExitProcess() exit()
WaitForSingleObject() wait()
CreateFile() open()
ReadFile() read()
File Management
WriteFile() write()
CloseHandle() close()
SetConsoleMode() ioctl()
Device Management ReadConsole() read()
WriteConsole() write()
GetCurrentProcessID() getpid()
Information Maintenance SetTimer() alarm()
Sleep() sleep()
CreatePipe() pipe()
Communication CreateFileMapping() shmget()
MapViewOfFile() mmap()

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.

Process Related System Calls


Process Creation and Termination exec()
fork()
wait()
exit()
Process Owner and Group getuid()
geteuid()
getgid()
getegid()
Process Identity getpid()
getppid()
Process Control signal()
kill()
alarm()
Change Working Directory chdir()

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.

1) To create a new process – fork () is used.


2) To run a new program = exec () is used.
3) To make the process to wait = wait () is used.
4) To terminate the process – exit () is used.

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:

1. The child has its own process ID (PID)


2. The child will have a different PPID than its parent
3. System imposed process limits are reset to zero
4. All recorded locks on files are reset
5. The action to be taken when receiving signals is different

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[]);

Here's what l, v, e, and p mean:


 l means an argument list,
 v means an argument vector,
 e means an environment vector, and
 p means a search path.

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);

printf ("Return to ex1.c");

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.

By calling wait(), the parent cleans up all its zombie children.


When the child process dies, an exit status is returned to the OS and a signal is sent to the
parent process.  The exit status can then be retrieved by the parent process via the wait
system call.

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

man pages from bash

Developed by: Khalilah Burrell-Battick (2021)

Pg. 6 of 6

You might also like