System Calls - Processes
Process
Characteristics
A process contains:
- A unique ID
- A thread of control
- Some private data
Parent-Child Links:
- parent process: executes the request for creation of a process
- child process: the created process
- Noted: A child process inherits all the properties of its parent when it is created.
Process ID
Process ID (PID):
- Each process has a unique ID number.
- It’s use to identify different processes.
User ID (UID):
- Each user has a unique user ID.
- Each process is associated with the the owner of the process, the owner has privileges with respect to the process.
Obtain ID in C:
getpid()- obtain the child’s PID.getppid()- obtain the parent’s PID.getuid()- obtain the owner’s UID.1
2
3
4
5
6
7
8
9
10
11
12
13/* 1-getpid */
int main(void) {
printf("My PID is %d, UID is %d, My Parent PID is %d\n",
getpid(), getuid(), getppid());
return 0;
}
/*
My PID is 4003, UID is 501, My Parent PID is 3843
*/
- Obtain ID in terminal:
ps- lists the current processes.ps -a- lists more processes, including the processes that being run by other users and at other terminals.ps -l- prints more information lines, including UID, PID, PPID, process status, etc.
Process Operations
fork System Call
Description:
- To create a process (child process).
Syntax:
int fork()Return:
- To child: return 0
- To parent: return child’s PID

- Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25/* 2-fork */
int main(void){
int ret_from_fork;
printf("Before: my pid is %d\n", getpid());
ret_from_fork = fork(); // create a child
sleep(1);
printf("After: my pid is %d, return value from fork():%d\n",
getpid(), ret_from_fork);
return 0;
}
/*
Before: my pid is 3991
After: my pid is 3994, return value from fork():0
After: my pid is 3991, return value from fork():3994
*/
- Task:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31/* How many child processes were created? */
fork();
fork();
fork();
/* 6-3fork */
int main(void){
fork();
fork();
fork();
printf("PID - %d, PPID -%d\n", getpid(), getppid());
sleep(1);
return 0;
}
/*
PID - 5207, PPID -5141
PID - 5210, PPID -5207
PID - 5209, PPID -5207
PID - 5211, PPID -5209
PID - 5208, PPID -5207
PID - 5213, PPID -5208
PID - 5212, PPID -5208
PID - 5214, PPID -5212
*/
wait System Call
Description:
- After
fork()system call, both parent and child proceed independently. - Therefore, if a parent wants to wait unitl the child finishes, then executes
wait()orwaitpid().
- After
Syntax:
pid_t wait(int *stat)stat: the child process
Return:
- If because a child terminated: return the PID of that child
- Otherwise, return -1
- Working Process:
- it causes the caller process to pause,
- until the a child terminates or stops, or
- until the caller recieves a signal
- it returns right away,
- if the process has no child, or
- if the child has already terminated or stopped but has not yet been waited for
- it causes the caller process to pause,
- Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41/* 7-mywait */
int main(void)
{
int i;
int n = 5;
pid_t childpid;
int status;
pid_t waitreturn;
// repeat 5 times
for (i = 1; i < n; ++i){
// create a child then break -> new process becomes parent
if((childpid = fork()) > 0)
break;
}
// wait the child precess terminates
if((waitreturn = wait(&status)) < 0)
// if there is no child (wait returns -1 which < 0)
printf("wait(): Returns - %d.\n", waitreturn);
printf("I am process %d, my parent is %d, childpid:%d, i:%d\n",
getpid(), getppid(), childpid, i);
return 0;
}
/*
wait(): Returns - -1.
I am process 4119, my parent is 4118, childpid:0, i:5
I am process 4118, my parent is 4117, childpid:4119, i:4
I am process 4117, my parent is 4116, childpid:4118, i:3
I am process 4116, my parent is 4115, childpid:4117, i:2
I am process 4115, my parent is 3843, childpid:4116, i:1
*/
exec System Call
- Description:
- To change processes.
- The
forksystem call creates a copy of the calling process (child inherits all the properties of its parent). - However, many applications require the child process to executes code different from the parent’s.
- Therefore, executes
execfamily system calls to overlay the calling process with a new executable module.
- Working Process:
execwill load a new executable into the process image, copies the arguments into the process and callsmain(args, argy).- If succeed,
execnever returns. The calling process is completely overlaid by the new program. - The
fork-execcombination can let the child execute the new program while the parent continues to execute the original code.
Variations of
execsystem call:execlcalls (execl,execlp,execle)- pass the command-line arguments as a list of pointers.
execvcalls (execv,execvp,execve)- pass the command-line arguments in an argument array, which is an array of pointers to null-terminated strings (ie. must be terminated by a NULL pointer).
- Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42/* 8-myexecl */
int main(void){
pid_t childpid;
int status;
char *cmd = "ls";
char *argv[2];
argv[0] = "ls";
argv[1] = NULL;
printf("%d", wait(&status));
// create a child to execute new program
if ((childpid = fork()) == -1){
printf("Error: fork()\n");
return -1;
}else if(childpid == 0) {
/* child code (childpid for child is 0) */
if (execvp(cmd, argv) < 0) {
printf("Error: execvp()\n");
return -1;
}
}else if(childpid != wait(&status)){
/* parent code */
printf("Error: wait()");
return -1;
}
return 0;
}
/*
Results: print the list
*/
Process Termination
Two Terminations
Normal Process Termination
- A process terminates normally when,
- a
returnfrommain - an implicit return from main
- a call to the
_exitsystem call - a call to the C function
exit
- a
- A process terminates normally when,
Abnormal Process Termination
- A process terminates abnormally when,
- calling
abort, causing the SIGABRT signal to be sent to the calling process. - processing a signal that causes termination.
- calling
- User-installed exit handler will not be called upon abnormal termination.
- A process terminates abnormally when,
Orphan Process
it occurs if when the child still executing but its parent has terminated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29/* 9-orphan */
int main(void)
{
pid_t childpid;
if((childpid = fork()) == -1){
printf("Error in the fork");
return -1;
}else if(childpid == 0) {
/*child code*/
printf("Before: I am the child (PID - %d, PPID -%d)\n", getpid(), getppid());
sleep(10); // the child has not been terminated
printf("Before: I am the child (PID - %d, PPID -%d)\n", getpid(), getppid());
return 0;
}else{
/* parent code */
sleep(1); // the parent is terminated
printf("I am the parent (PID - %d, PPID -%d)\n", getpid(), getppid());
return 0;
}
}
Zombie Process
- it occurs if the parent of the terminating process is not currently executing a
wait(). Noted: the zombie process is not an orphan process.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30/* 9-1-zombie */
int main(void){
pid_t childpid;
if ((childpid = fork()) == -1){
printf("Error in the fork");
return -1;
} else if (childpid == 0) {
/*child code*/
printf("Before: I am the child (PID - %d, PPID -%d)\n", getpid(), getppid());
return 0;
}else{
/* parent code */
sleep(100); // child process is terminated but parent does not
printf("I am the parent (PID - %d, PPID - %d)\n", getpid(), getppid());
return 0;
}
}
/*
ls -l
UID PID PPID S
501 4276 4275 Z+ // this is the zombie process
*/
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.


