Services and Processes
Lecture 4
owent, zoey
(slide credits kmo, rrchan, cooperc, keur)
Course Resources
Outline
Disclaimer
Processes
What is a process?
A process is a single instance of a program.
Processes are isolated from one another and have their own memory, threads, etc. (Additional isolation, such as filesystem or network isolation, is also possible.)
What is a process?
(and more…)
Init
Process Hierarchy
Each process is created by a parent
(Except PID 1)
Processes can have many children
Processes vs Threads
A process has one or more threads.
Aside: Why does Chrome spawn so many processes?
How are processes created?
A process will fork(2) into a two new processes, which continue from the same place
The parent keeps the original PID and the child gets a new PID
Optionally, the new process (the child) exec(3) and begin running a new program
How many “henlo”s get printed?
int main( void ) {� fork();� printf( "henlo: %d\n", getpid() );�}
How many “henlo”s get printed?
int main( void ) {� fork();� printf( "henlo: %d\n", getpid() );�}
int main( void ) {� fork();� printf( "henlo: %d\n", getpid() );�}
int main( void ) {� fork();� printf( "henlo: %d\n", getpid() );�}
PID 1000
PID 1000
PID 2000
How many “henlo”s get printed?
int main( void ) {� fork();
fork();
fork();� printf( "henlo: %d\n", getpid() );�}
2^1
2^2
2^3
Have you seen this code before?
:(){ :|:& };:
bomb() {
bomb | bomb &
} bomb
Making the child do something
int main( void ) {
if ( fork() > 0 ) {
/* parent process */
wait( NULL );
} else {
/* child process */
execv( “/bin/bash”, NULL );
}�}
htop exploration
What does this code do?
int main( void ) {
if ( fork() > 0 ) {
/* parent process */
sleep( 1 );
} else {
/* child process */
exit( 1 );
}�}
What does this code do?
\_ /usr/bin/zsh
\_ ./zombie-creator
\_ [zombie-creator] <defunct>
Parent
Child
Zombie Process
When a child has died but has not been “reaped”
Child metadata stays in process table so parent can collect exit status
Totally normal, all children that exit are zombies!
What happens when a process dies
if ( fork() > 0 ) {� /* parent process */� sleep( 1 );
} else {� /* child process */� exit( 1 );
}
When a process exits, returns int (exit code 0-255)
0 is success and anything else indicates an error
What happens when a process dies
if ( fork() > 0 ) {� /* parent process */� sleep( 1 );
} else {� /* child process */� exit( 123 );
}
When a process exits, returns int (exit code 0-255)
0 is success and anything else indicates an error
Parents need to wait() on children
int main( void ) {
if ( fork() > 0 ) {
int status;
sleep( 1 );
wait( &status );
printf( "%d\n", WEXITSTATUS ( status ) );
} else {
exit( 123 );
}
What happens if the parent exits first?
If parent exits first, orphan processes are re-parented by the init process
init reaps all orphans that are zombies
Orphans
int main( void ) {
if ( fork() > 0 ) {
/* parent process */
sleep ( 1 );
exit( 0 );
} else {
/* child process */
sleep( 100 );
exit( 123 );
}
Process tree after start
Process tree after 1 second
Parent
Child
Parent
Child
When can zombies become a problem?
Parent doesn’t wait() on children
Parent is long running process
Zombie child processes never become orphans
Resource leakage!
Zombie leakage in the wild
Inter-process Communication
Various ways in which processes can communicate
Process Signals
SIGKILL and SIGSTOP cannot be handled
Services
What is a service?
Services - What’s the point?
Systemd
Systemd Unit Files
A (simplified) Unit File: helloworld.service
[Unit]�Description=A simple unit file
[Service]�ExecStart=/usr/bin/helloworld�User=ocfstaff Restart=always
[Install]�WantedBy=multi-user.target
Description - what the service does
Commands
When this unit should get started
Example Unit file - Nginx
systemctl (no args) - Info about systemd units
systemctl status [name] - Gets info about a service’s status
More systemctl!
Behavior for these commands depends on the service, as defined in the unit file, but typically will do the things listed above.
Example - sshd
Example - nginx and httpd
Both examples of services which listen on port 80 and serve content.
Problem: systemd sucks
Problem: systemd sucks, and people care
Bonus: Runit
About Runit
Hierarchy
Example: dbus
Example: udevd
Service Management
Service Management
Minimal Philosophy
Like Really Bad (not OCF endorsed msg)