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 | less
ps -e | grep Process # find a process
On the left-hand side, you see ‘Julia’ process, on the right-side side, you see its PID.
💡
<CTRL>+Z suspends the process.
<CTRL>+C kills the process.

https://superuser.com/questions/262942/whats-different-between-ctrlz-and-ctrlc-in-unix-command-line
kill -KILL PID # 'system call to send any signal to process' from https://man7.org/linux/man-pages/man2/kill.2.html
On the right side, you see I kill ‘Julia’ process with its PID.
pstree -ap | less # display a tree of processes
On the left-hand side, you see a new ‘Julia’ process, on the right-side side, you see its tree process. ‘systemd’ is the root of the user space’s process tree.

Fork, exec, wait

1. The loop and fork make nn child processes where nn 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
fork make three

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 nn child processes where nn 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/