[Linux] Process

binh12A3
4 min readSep 3, 2023

fork(), execl(), exit(), wait(), …

daemon, crontab ???

nohup, kill zombie process

daemon : Thuật ngữ này đồng nghĩa với khái niệm “service”, một tác vụ chạy liên tục, thường không có sự tương tác của người dùng.

https://cuongquach.com/daemon-trong-linux-la-gi.html

https://viblo.asia/p/basic-process-management-quan-ly-tien-trinh-trong-unixlinux-co-ban-LzD5der0KjY

  1. Overview
  • A process is created from fork() or execl()
  • In a terminal, when we call a command (i.e : ls -ltr), in the terminal’s code, it’ll fork the process, the parent process will continue running the terminal GUI, the child process will call execl() and run your command line.

2. What is the difference between “Thread” and “Process” ?

Source : https://gist.github.com/binh12A3/6e27bdc1494be7b51d80573db2ddc778

3. fork()

#include <unistd.h>
pid_t fork(void);
  • It’ll duplicate the current program, by cloning the global variables and the code after the fork()
  • The child process is now independent with parent process and could be finished firstly.
Source : https://gist.github.com/binh12A3/1d3892da4a2a40c4664ffc0a4cf1cc15

Output:

[PID:65] Hello world
[PID:65] Fork pid : 66
[PID:66] Fork pid : 0
[PID:65] This is Parent process, forked pid = 66, a = 1
[PID:66] This is Child process, forked pid = 0, a = -1
[PID:65] This is Parent process, forked pid = 66, a = 2
[PID:66] This is Child process, forked pid = 0, a = -2
[PID:65] This is Parent process, forked pid = 66, a = 3
[PID:66] This is Child process, forked pid = 0, a = -3
[PID:65] Goodbye, a = 3
[PID:66] This is Child process, forked pid = 0, a = -4
[PID:66] This is Child process, forked pid = 0, a = -5
[PID:66] Goodbye, a = -5
  • We could monitor a process by using “ps” command
Source : https://gist.github.com/binh12A3/095caa9d444e01a633ab16651195d92b

Output:

[03/09/2023 22:17:27][PID:166][PPID:119][INFO ] Hello world
[03/09/2023 22:17:27][PID:166][PPID:119][INFO ] Parent process
[03/09/2023 22:17:27][PID:167][PPID:166][DEBUG] Child process
[03/09/2023 22:17:28][PID:166][PPID:119][INFO ] Parent process
[03/09/2023 22:17:29][PID:166][PPID:119][INFO ] Parent process
[03/09/2023 22:17:30][PID:166][PPID:119][INFO ] Parent process
...
...
  • Open a new session to monitor the processes
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux$ ps -u btnguyen -o user,pid,ppid,tty,stat,start,time,command | grep -v grep | grep -E 'test|PID'
USER PID PPID TT STAT STARTED TIME COMMAND
btnguyen 79 1 ? S 22:14:09 00:00:00 ./test
btnguyen 109 1 ? S 22:14:58 00:00:00 ./test
btnguyen 166 119 tty3 S 22:17:27 00:00:00 ./test
btnguyen 167 166 tty3 Z 22:17:27 00:00:00 [test] <defunct>
btnguyen@DESKTOP-UAIA29B:/mnt/h/DEVELOPER/Linux$

stat = ‘Z’ means “Zombie”

4. execl()

#include <unistd.h>
int execl(const char* pathname, const char* arg(), (char*)0)
  • It’ll replace/override all the parent process memory.
  • It could init a new program.
  • When the execl() is called, all the code after it will be ignored.
Source : https://gist.github.com/binh12A3/036e6d6e6d110c2406040c9529c707eb

Output:

Hello world
mylib test test.cpp

5. exit()

  • When a child process is active ending (i.e : calls exit()) or passive ending (i.e : killed/terminated by signal), kernel will send an exit status and SIGCHILD to the parent process.
  • In the meantime, the child process will become a “Zombie process” and waitting for the confirmation signal by wait() from the parent process.
  • A zombie process can’t be killed.

6. wait()

pid_t wait(int* wstatus)
  • The child process returns 5, then the parent process will return wstatus is 5.
  • When calling wait() the parent process will be blocked (sleep) until the child process ends.

7. setuid() and getgid()

int setuid(uid_t uid);
int setgid(gid_t gid);
  • When user-id and group_id is changed, then the process’s rights is changed.
  • It will set according to the process’s tendency to reduce permissions.
  • When using chmod() to grant the process a root permission, then normal user will need to use “sudo” to run it. While we dont’t need “sudo” if we use these functions.
  • Just in case we pass 0 to setuid(), then we have to use “sudo” else we will receive an error (0 means root).

--

--