linux process job session control

Process group

Every process have a process id and a process group id
Every process group have a leader process[id is same as group]

Each process in group have relations, like a family tree.

All processes’ root is init process which id is 1
Process group will exist when process exist in group.

1
2
3
4
5
6
pi@raspberrypi:~ $ ps axj |grep "\s1\s"
0 1 1 1 ? -1 Ss 0 2:25 /sbin/init
1 145 145 145 ? -1 Ss 0 0:27 /lib/systemd/systemd-journald
1 150 150 150 ? -1 Ss 0 0:00 /lib/systemd/systemd-udevd
......
1 853 853 853 ? -1 Ss 0 0:00 /usr/sbin/sshd -D

Jobs

Shell control frontground and background by job or process group but not process.

A job can be composed by one or more process group.

A shell can run a frontground job and multi background jobs.

Use pip command | run multi process, & make job background.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
pi@raspberrypi:~ $ sleep 1000|more|less &
[1] 12033
pi@raspberrypi:~ $ ps axj | head -n1
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
pi@raspberrypi:~ $ ps axj |grep 12033
12031 12033 12033 12033 pts/0 12068 Ss 1000 0:00 -bash
12033 12061 12061 12033 pts/0 12068 T 1000 0:00 sleep 1000
12033 12062 12061 12033 pts/0 12068 T 1000 0:00 more
12033 12063 12061 12033 pts/0 12068 T 1000 0:00 less
12033 12068 12068 12033 pts/0 12068 R+ 1000 0:00 ps axj
12033 12069 12068 12033 pts/0 12068 S+ 1000 0:00 grep --color=auto 12033

pi@raspberrypi:~ $ ps axj |grep 12031
12024 12031 12024 12024 ? -1 S 1000 0:00 sshd: pi@pts/0
12031 12033 12033 12033 pts/0 12070 Ss 1000 0:00 -bash
12033 12071 12070 12033 pts/0 12070 S+ 1000 0:00 grep --color=auto 12031

pi@raspberrypi:~ $ ps axj |grep 12024
853 12024 12024 12024 ? -1 Ss 0 0:00 sshd: pi [priv]
12024 12031 12024 12024 ? -1 S 1000 0:00 sshd: pi@pts/0
12033 12074 12073 12033 pts/0 12073 S+ 1000 0:00 grep --color=auto 12024

pi@raspberrypi:~ $ ps axj |grep 853
1 853 853 853 ? -1 Ss 0 0:00 /usr/sbin/sshd -D
853 12024 12024 12024 ? -1 Ss 0 0:00 sshd: pi [priv]
12033 12076 12075 12033 pts/0 12075 S+ 1000 0:00 grep --color=auto 853

Difference between job and process group:
Sub process not belong to main process’s job alone.

Session

Session composed by one or more process group.[job?]
A session can have control terminal.

Builde a connection and connect with terminal’s session fist
process be called the control process bash in normal.

  • one control process bash
  • one frontground process
  • any number background process

Job control

job control is new charcter be added in 1980 by BSD.
It allow terminal execute multi process group.

  • need job control shell
  • kernal’s terminal driver support job control
  • kernal support job’s control signal

Signal

name opt signal
Interrupt Ctrl + c SIGINT
Quit Ctrl + \ SIGQUIT
Hang up Ctrl + z SIGSTP

Input Output

Only frontground process can read terminal’s input data.

When background process want to read data from terminal
it is not a error behaviour, just not be allowed.

When kernal driver detected background process want read
it will send a SIGTSTP signal to background job to stop process.

Then use fg command change process to front and run
[change to front and put SIGCONT signal to this group]

Background process write data to terminal can be selfdefine
stty tostop command formidden background write data to terminal.

1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
#include<stdlib.h>
int main()
{
char args[10];
while(1)
{
scanf("%s",&args);
printf("%s\n",args);
}
return 0;
}