GNU/Linux Process User Commands
In Linux, PID indicates process ID. We list some useful commands called ps
, pstree
, kill
.
List all processes in Linux and show less because you want to see columns. You can move like VIM in normal mode with less
.
ps -e | less
ps -e | grep Process # find a process
kill -KILL PID # 'system call to send any signal to process' from https://man7.org/linux/man-pages/man2/kill.2.html
pstree -ap | less # display a tree of processes
Fork, exec, wait
1. The loop and fork make child processes where is argc-1
; they are executed in parallel after.
curl https://raw.githubusercontent.com/sanchezcarlosjr/operating-systems/main/fork.c > fork.c && gcc fork.c -o fork
./fork f1 f2 f3
Open three terminals and search your shell with ps -ef | grep zsh
. You need pseudo terminal pts.
./fork /dev/pts/1 /dev/pts/2 /dev/pts/3 /dev/pts/4
2. The loop and fork make child processes where is argc-1
; they are executed in a sequential way after.
curl https://raw.githubusercontent.com/sanchezcarlosjr/operating-systems/main/forkwait.c > forkwait.c && gcc forkwait.c -o forkwait
./forkwait f1 f2 f3
When should you use fork
?
1. A parallel process with the same code and different arguments, for instance, writing files.
2. And when you have to isolate a process from a parent using exec
varieties, for instance, systemd
software. UNIX takes a clever design decision: they separate creating a new process (fork
) and running a program (exec
).
Exercise.
Make a console software that removes n
files by parameters using fork
. Hint: https://man7.org/linux/man-pages/man3/remove.3p.html
Notes and references
https://man7.org/linux/man-pages/man2/fork.2.html
Linux Programming By Example. The fundamentals by Arnold Robbins. Chapter 9. Process Management and Pipes.
https://www.dit.upm.es/~joaquin/comunix/comunix-4.html
https://www.geeksforgeeks.org/fork-system-call/