process group: https://cs162.org/static/readings/ic221_s16_lec17.html
signal: https://man7.org/linux/man-pages/man7/signal.7.html
pipe: https://man7.org/linux/man-pages/man2/pipe.2.html
strace: https://linux.die.net/man/1/strace
Pipe
- https://stackoverflow.com/questions/24766013/is-it-really-necessary-to-close-the-unused-end-of-the-pipe-in-a-process
- after dup2, pipe output end should be close so that the input end knows that the file ends
- stdout will be close automatically, while result of pipe will not, which results in waiting
- debug for 2h ……
- https://man7.org/linux/man-pages/man7/pipe.7.html
- If a process attempts to read from an empty pipe, thenread(2)
will block until data is available.
- If all file descriptors referring to the write end of a pipe have
been closed, then an attempt toread(2) from the pipe will see
end-of-file (read(2) will return 0).
- If all file descriptors referring to the read end of a pipe have been closed, then a
write(2) will cause aSIGPIPEsignal to be generated for the
calling process. If the calling process is ignoring this signal,
thenwrite(2) fails with the errorEPIPE. An application that
usespipe(2) andfork(2) should use suitableclose(2) calls to
close unnecessary duplicate file descriptors; this ensures that
end-of-file andSIGPIPE/EPIPEare delivered when appropriate.
Signal
-
Generally, job control is a feature of the shell and supported by the terminal device driver.
- The shell manages which jobs are stopped or running and notifies the terminal driver which job is currently in the foreground.
- The terminal device driver listens for special keys, like
Ctrl-c
or Ctrl-z
, and delivers the appropriate signal to the foreground process, like terminate or stop.
-
sleep 10 | sleep 20 | sleep 30 | sleep 50
- A pipeline executing in sequence would have a single process running at a time for each total job
- while executing in parallel, however, would have multiple currently running processes per job.
- in fact, system use the parallel solution, use
ps -o pid,pgid,ppid,args
to check!
- A process group is a way to group processes into distinct jobs that are linked, and a session is way to link process groups under a single interruptive unit, like the terminal.
- parent pid (
ppid
) and the process group (pgid
) in addition to the process id (pid
) and the command arguments (args
).
-
Retrieving pid's or pgid's:
pid_t getpid()
/getppid()
/getpgrp()
: get the pid/ppid/pgid
pid_t getpgid(pid_t pid)
: get the pgid for [the process identified by PID
]
-
Setting pgid's:
pid_t setpgrp()
: set the process group of the calling process to iteself, i.e. after a call to setpgrp()
, getpid() == getpgrp() holds.
pid_t setpgid(pid_t pid, pid_t pgid)
: Set the process group ID of the process matching PID
to PGID
.
- If
PID
is zero, the current process's pgid is set.
- If
PGID
is zero, the pid of that process is used as PGID
.
- For shell, jobs are given a different pgid from the shell
- when fork, both setpgid and setpgrp will be used to avoid race condition
- signal will be given to all process with the same pgid as the foreground process
pid_t tcgetpgrp(int fd) / int tcsetpgrp(int fd, pid_t pgrp)
- get the foreground process in controling terminal associated with
FD
- The
FD
should be 0 for “standard input”.