Processes
2024-11-14
What is a process?
Definition: Process
Source: Tanenbaum, Modern Operating Systems 4e, (c) 2014 Prentice-Hall, Inc. All rights reserved.
Which resources are associated to a process?
Definition: Hardware Context
The hardware context describes the content of the CPU registers during process execution.
Definition: System Context
The information the operating system stores about a process is called the system context. Each process can be uniquely identified by a subset of this information.
How many process states must a process model contain at least?
Source: Prof. Christian Baun
Processes in state idle are stored in a queue (\(\rightarrow\) the runqueue), in which they wait for execution
Source: Prof. Christian Baun
-20
and +19
are available-20
is the highest priority and +19
is the lowest priority.0
0
to 19
root
) can assign negative values too0
and 15
are available0
is the highest priority and 15
is the lowest priority.7
\(\Longrightarrow\) Process state model with 3 states
Each process is in one of the following states:
running:
For many implementations the introduction of two additional states is useful:
new: The process (process control block) has been created by the OS but not yet in ready state
exit: The execution of the process has finished or was terminated but the process control block still exists
The sum of all processes may exceed the amount of physical main memory \(\Rightarrow\) memory belonging to currently not running processes is swapped out \(\Longrightarrow\) swapping
The OS outsources processes which are in blocked state
A zombie process has completed execution (via the system call exit
) but its entry in the process table exists until the parent process has fetched (via the system call wait
) the exit status (return code)
What do you already know? Let’s go to the survey again:
https://fra-uas.particifyapp.net/p/66824346
Which cache write policy yields the best performance?
Which types of context information does the OS store per process?
To which states are there valid transitions from the ready state in a Linux system?
What does one need to do in order to implement an application that can be run on a variety of computers?
fork
fork()
is the only way to create a new processfork()
, an identical copy is started as a new process
fork
on Linuxfork()
, an exact copy is created
fork()
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void main(void) {
int return_value = fork();
if (return_value < 0) {
// If fork() returns -1, an error happened.
// Memory or processes table have no more free capacity.
...
}
if (return_value > 0) {
// If fork() returns a positive number, we are in the parent process.
// The return value is the PID of the newly created child process.
...
}
if (return_value == 0) {
// If fork() returns 0, we are in the child process.
...
}
}
fork()
But which process forms the root of this hierarchy?
init
or systemd
(PID 1) is the first process in Linux/UNIX
All running processes originate from init
\(\rightarrow\) init (or systemd) = parent of all processes
The commands pstree
and ps f
return an overview about the processes, running in Linux/UNIX, as a tree according to their parent/child relationships
$ ps fax
1 ? Ss 0:01 /usr/lib/systemd/systemd --switched-root --system
...
1211 ? Ss 0:00 dhcpcd: [manager] [ip4] [ip6]
1214 ? S 0:00 \_ dhcpcd: [privileged proxy]
7775 ? S 0:00 | \_ dhcpcd: [BPF ARP] enp0s31f6 10.2.0.190
7778 ? S 0:00 | \_ dhcpcd: [BPF ARP] wlan0 10.51.134.219
1215 ? S 0:00 \_ dhcpcd: [network proxy]
1216 ? S 0:00 \_ dhcpcd: [control proxy]
1339 ? Ss 0:00 /usr/lib/systemd/systemd --user
1340 ? S 0:00 \_ (sd-pam)
1465 ? Ss 0:00 \_ /usr/bin/dbus-daemon --session --nofork
1511 ? Ssl 0:00 \_ /usr/lib/at-spi-bus-launcher
1519 ? S 0:00 | \_ /usr/bin/dbus-daemon --address=unix:path=/run/user/1000/at-spi/bus
$ ps -eFw
UID PID PPID C SZ RSS PSR STIME TTY TIME CMD
root 1 0 0 5456 12860 2 12:06 ? 00:00:01 /usr/lib/systemd/systemd
root 1311 1 0 1998 4992 4 12:06 ? 00:00:00 login -- oleg
oleg 1339 1 0 5110 11828 4 12:07 ? 00:00:00 /usr/lib/systemd/systemd --user
oleg 1347 1311 0 1122763 171300 0 12:07 tty1 00:00:51 sway
oleg 8031 1 0 285131 31908 3 13:16 ? 00:00:02 foot
oleg 8033 8031 0 4948 15160 7 13:16 pts/2 00:00:02 /usr/bin/zsh
oleg 14043 1 3 949647 569960 4 13:26 ? 00:01:33 /usr/lib/firefox/firefox
oleg 14077 1 0 261432 165640 2 13:26 tty1 00:00:06 Xwayland :0 -rootless -core
oleg 22367 1 0 285340 35712 3 13:54 ? 00:00:01 foot
oleg 22369 22367 0 3710 9548 2 13:54 pts/1 00:00:00 /usr/bin/zsh
root 25003 2 0 0 0 6 14:05 ? 00:00:00 [kworker/6:2-events]
root 25097 2 0 0 0 0 14:05 ? 00:00:00 [kworker/0:2-i915-unordered]
oleg 25202 22369 0 3187 4564 3 14:05 pts/1 00:00:00 ps -eFw
Child : 0
Child : 1
...
Child : 21019
Parent: 0
...
Parent: 50148
Child : 21020
...
Child : 129645
Parent: 50149
...
Parent: 855006
Child : 129646
...
The output demonstrates the switches between the processes
The value of the loop variable i
proves that parent and child processes are independent of each other
The result of execution can not be reproduced!
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void main(void) {
int pid_of_child;
pid_of_child = fork();
// An error occured --> program abort
if (pid_of_child < 0) {
perror("\n fork() caused an error!");
exit(1);
}
// Parent process
if (pid_of_child > 0) {
printf("\n Parent: PID: %i", getpid());
printf("\n Parent: PPID: %i", getppid());
}
// Child process
if (pid_of_child == 0) {
printf("\n Child: PID: %i", getpid());
printf("\n Child: PPID: %i", getppid());
}
}
The output is usually similar to this one:
Parent: PID: 20835
Parent: PPID: 3904
Child: PID: 20836
Child: PPID: 20835
This result can be observed sometimes:
Parent: PID: 20837
Parent: PPID: 3904
Child: PID: 20838
Child: PPID: 1
The parent process was terminated before the child process
If a parent process terminates before the child process, it gets init
as the new parent process assigned
Orphaned processes are always adopted by init
exec
exec()
replaces a process with another one
fork()
, and thenexec()
If no new process is created with fork()
before exec()
is called, the parent process is replaced
fork()
an identical copy of itselfexec()
Source: Prof. Christian Baun
$ ps -f
UID PID PPID C STIME TTY TIME CMD
user 1772 1727 0 May18 pts/2 00:00:00 bash
user 12750 1772 0 11:26 pts/2 00:00:00 ps -f
$ bash
$ ps -f
UID PID PPID C STIME TTY TIME CMD
user 1772 1727 0 May18 pts/2 00:00:00 bash
user 12751 1772 12 11:26 pts/2 00:00:00 bash
user 12769 12751 0 11:26 pts/2 00:00:00 ps -f
$ exec ps -f
UID PID PPID C STIME TTY TIME CMD
user 1772 1727 0 May18 pts/2 00:00:00 bash
user 12751 1772 4 11:26 pts/2 00:00:00 ps -f
$ ps -f
UID PID PPID C STIME TTY TIME CMD
user 1772 1727 0 May18 pts/2 00:00:00 bash
user 12770 1772 0 11:27 pts/2 00:00:00 ps -f
exec
, the ps -f
command replaced the bash and got its PID (12751) and PPID (1772)exec
Example#include <stdio.h>
#include <unistd.h>
int main(void) {
int pid;
pid = fork();
// If PID!=0 --> Parent process
if (pid) {
printf("...Parent process...\n");
printf("[Parent] Own PID: %d\n", getpid());
printf("[Parent] PID of the child: %d\n", pid);
}
// If PID=0 --> Child process
else {
printf("...Child process...\n");
printf("[Child] Own PID: %d\n", getpid());
printf("[Child] PID of the parent: %d\n", getppid());
// Current program is replaced by "date"
// "date" will be the process name in the process table
execl("/bin/date", "date", "-u", NULL);
}
printf("[%d ]Program abort\n", getpid());
return 0;
}
exec()
does not exist as wrapper functionexec()
function existexecl()
Helpful overview about the different variants of the exec()
function:
http://www.cs.uregina.ca/Links/class-info/330/Fork/fork.html
exec
Example$ ./exec_example
...Parent process...
[Parent] Own PID: 25646
[Parent] PID of the child: 25647
[25646 ]Program abort
...Child process...
[Child] Own PID: 25647
[Child] PID of the parent: 25646
Di 24. Mai 17:25:31 CEST 2016
$ ./exec_example
...Parent process...
[Parent] Own PID: 25660
[Parent] PID of the child: 25661
[25660 ]Program abort
...Child process...
[Child] Own PID: 25661
[Child] PID of the parent: 1
Di 24. Mai 17:26:12 CEST 2016
After printing its PID via getpid()
and the PID of its parent process via getppid()
, the child process is replaced via date
If the parent process of a process terminates before the child process, the child process gets init
as new parent process assigned :::
Since Linux Kernel 3.4 (2012) and Dragonfly BSD 4.2 (2015), it is also possible that other processes than PID=1 become the new parent process of an orphaned process http://unix.stackexchange.com/questions/149319/new-parent-process-when-the-parent-process-dies/177361#177361
fork()
a new, identical processexec()
a new process and terminates itself this way because it gets replaced by the new processfork()
a new, identical process, which replaces itself via exec()
by a new processA fork bomb is a program, which calls the fork()
system call in an infinite loop
Objective: Create copies of the process until there is no more free memory
Only protection option: Limit the maximum number of processes and the maximum memory usage per user
What types of data are being accessed by a process?
The structure of processes on 64 bit systems is not different from 32 bit systems. Only the address space is larger and thus the possible extension of the processes in the memory.
exec()
reads the text segment from the program fileint sum = 0;
exec()
reads the data segment from the program fileThe user space in the memory structure of the processes is the user context. It is the virtual address space (virtual memory) allocated by the operating system
int i;
malloc()
0
on startsize
returns the size (in bytes) of the text segment, data segment, and BSS of program files
0
at process creation$ size /bin/c*
text data bss dec hex filename
46480 620 1480 48580 bdc4 /bin/cat
7619 420 32 8071 1f87 /bin/chacl
55211 592 464 56267 dbcb /bin/chgrp
51614 568 464 52646 cda6 /bin/chmod
57349 600 464 58413 e42d /bin/chown
120319 868 2696 123883 1e3eb /bin/cp
131911 2672 1736 136319 2147f /bin/cpio
Sources UNIX-Systemprogrammierung, Helmut Herold, Addison-Wesley (1996), P.345-347
Betriebssysteme, Carsten Vogt, Spektrum (2001), P.58-60
Moderne Betriebssysteme, Andrew S. Tanenbaum, Pearson (2009), P.874-877
You should now be able to answer the following questions:
Operating Systems - Processes - WS 24/25