Linux Processes Flashcards
Process Creation
A process can be created from other processes, which is used very frequently in linux.
exec()
Allows us to execute one process from another process, leading the child process to take over. For example, in user system calls, we can call the execl() function to run another program in our program we have created, like execl(“/bin/ls”, “ls”, “-l”, NULL). This gives the location of the process and the process name, with NULL indicating the end of the parameters being passed.
fork()
Allows us to clone the original process, so that we can alter the cloned process without alternating the original. This cloned process is also a child process, and takes over. For example, when a user logs in and starts a graphical session, the login manager may use fork() to create a child process to handle the user’s session.
wait()
This allows us to stop the parent process when fork() or exec() is called until the child process is done.
The first process in linux
Systemd is the first process in linux. When executed, it continues to run in the background, offering on-demand spawning of other services/processes, maintaining log files and keeping track of other processes and kernel settings.
GRUB
GNU Grand Unified Bootloader. Stored in ROM and is a set of processes which help set up Linux when first loaded up, specifically loading the Linux kernel into memory and passing control to it, as well as configuring the system hardware and initializing the system services.
sshd
Secure shell daemon application; spawned by systemd. This is responsible for managing oncoming ssh connections (ssh is used to connect to a linux server). This uses the system calls fork() and exec() for the login process.
Login Process with sshd
1) We use ssh to connect to linux server
2) The sshd daemon uses fork() to spawn a child process
3) The child uses exec() to run login process
4) The login process checks credentials
5) Then the login process uses exec() to run our preferred shell process
Zombies and Orphans
Zombies -> if a parent does not execute the wait() syscall, it will continue to terminate during the execution of the child process. Once the child has finished, there is no wait() syscall so the parent has continued and therefore does not require a signal to terminate the child. This gives us a zombie process, which is a child that has finished and needs to be terminated, but cannot be.
Orphan -> we also have the same situation that if there is no wait() called, and the parent finishes its process before the child, the child has no parent and becomes an orphan.
Systemd performs wait() on the zombies and orphans, which instructs them to return their exit status, terminating them.
Daemons
These processes normally run in the background, performing background operations of the OS, like subsystem managers. These needs their own time on the CPU, and run with higher priority (this is in connection with the OS requiring time on the CPU).
Linux Signals and Responses to Signals
These interrupt signals are sent between processes with the syscall signal(), and can be used to terminate a process. For example, to terminate a process we input kill -s SIGKILL …
These signals can be responded to one of three ways:
- performing the action
- ignoring the signal
- catch the signal and run some other code instead
There is one signal which cannot be ignored/caught, however, which is SIGKILL.
Using Linux Signals for Zombies
We can actually use SIGKILL to kill the parent of the zombie. This way, the systemd will adopt the zombie and call wait().
Inter-Process Communications and Examples
Communications between processes. Four examples include:
- shared memory
- shared files
- pipes
- sockets
Shared memory and files
Allows two processes to access the same memory location or file at the same time, which can cause synchronisation issues and requires semaphores and locks.
Pipes
Form of IPC between two children who have the same parent, normally triggered by typing a command at the prompt. We can join two processes via the | symbol.