Processes Management

22 Sept 2020

Obed N Munoz

Cloud Software Engineer

Process Context

2

Process management system calls

3

Overview of fork(), exit(), wait() and execve()

4

Create a new process - fork()

The fork() system call creates a new process, the child, which is an almost exact duplicate of the calling process, the parent.

The two processes are executing the same program text, but they have separate copies of the stack, data, and heap segments.

pid_t childPid; /* Used in parent to record PID of child */

switch (childPid = fork()) {
  case -1:
    /* fork() failed */
    /* Handle error */
  case 0:
    /* Child of successful fork() comes here */
    /* Perform actions specific to child */
  default:
    /* Performs actions specific to parent */

Take a look on procexec/t_fork.c from The Linux Programming Interface.

5

Memory Semantics of fork() (1/2)

Conceptually, we can consider fork() as creating copies of the parent’s text, data, heap, and stack segments. It would be wasteful in many cases.

Most modern UNIX implementations, including Linux, use two techniques to avoid such wasteful copying:

6

Memory Semantics of fork() (2/2)

7

Race Conditions after fork()

After a fork(), it is indeterminate which process—the parent or the child—next has access to the CPU. In a complex system, this kind of issues or bugs can be hard to find.

Take a look on procexec/fork_whos_on_first.c from The Linux Programming Interface.

Let's test:

for i in $(seq 1 1 1000); do ./fork_whos_on_first 1; done | ./fork_whos_on_first.count.awk
for i in $(seq 1 1 1000); do ./fork_whos_on_first 5; done | ./fork_whos_on_first.count.awk
for i in $(seq 1 1 1000); do ./fork_whos_on_first 10; done | ./fork_whos_on_first.count.awk
8

Process termination

There are 2 general ways for terminating a process.

#include <unistd.h>
void _exit(int status);

Programs will generally use the exit() library function that will provide extra functionality.

#include <stdlib.h>
void exit(int status);

The following actions are performed by exit():
- Exit handlers
- stdio stream buffers are flushed
- The _exit() system call is invoked with the provided status code.

9

Exit handlers

Sometimes, an application needs to automatically perform some operations on process termination.

#include <stdlib.h>
int atexit(void (*func)(void));  /* Returns 0 on success, or nonzero on error */

In case you need the exit status code, there's a nonstandard alternative provided by glibc

#define _BSD_SOURCE /* Or: #define _SVID_SOURCE */
#include <stdlib.h>
int on_exit(void (*func)(int, void *), void *arg); /* Returns 0 on success, or nonzero on error */

The func argument is a pointer to a function as follows:

void func(int status, void *arg) {
  /* Perform cleanup actions */
}

Take a look on procexec/exit_handlers.c from The Linux Programming Interface.

10

Daemons and Services

A daemon is a process with the following characteristics:

Examples of common services:

What are the considerations we need to have when running our programs as daemons?

11

Systemd

systemctl status
systemctl list-units
journalctl -f
systemctl status <service>
12

Let's code: Add syslog support to your Logger

classify.obedmr.com/get-lab/ap-labs/advanced-logger

13

Communication between Processes (1/2)

Facilities of Inter-Process Communication.

Note: we'll go deeper on IPC in the following chapters.

14

Communication between Processes (2/2)

Facilities of Inter-Process Communication.

15

Processes' priority and relationship

Read Chapter 35 from The Linux Programming Interface book and summarize:

Note: Run the procpri/* code examples and comment about.

16

Resources and Credits

This material is genereated thanks to some extracts from following resources:

17

Thank you

Obed N Munoz

Cloud Software Engineer

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)