The process group process Group is a collection of one or more processes. Each process belongs to a process group in addition to a process ID. Each process group has a unique process group ID. Each process group can have a leader process. The process group ID of the leader process is equal to its process ID. Whether the process group exists and has no team leader is irrelevant. Each process belongs to a process group, there is no independent process, and unless there is only one process in the process group, it can be said that the process is independent. The leader process can create a process group, create a process in that group, and then terminate. However, as long as a process exists, the process group exists, regardless of whether its leader process terminates. Typically, they are associated with the same job and can receive various signals from the same terminal. The "job" shell does not control the process but the job (job) or the process group (Process group) in front and back of the table. (1) Job control: A foreground job can be composed of multiple processes, a background can be composed of multiple processes, the shell can run a foreground job and any number of background jobs, which is called job control. The daemon will not be killed by CTRL + C when we open a terminal program, after the terminal is closed, the background program will stop. (2) The difference between a job and a process group: If a process in the job has created a child process, the child process is not part of the job, but the child process belongs to its process group. Once the job runs, the shell mentions itself to the foreground, and if the original foreground process still exists (if the child process has not yet terminated), it automatically becomes the background process group. Sessions sessions are composed of multiple jobs or process groups and are collections of one or more process groups. A session can have a control terminal. This is usually the terminal device (in the case of terminal landing) or the pseudo terminal device (in the case of network Landing). The first process of establishing a session connected to a control terminal is called the control process. Several process groups in a session can be divided into one foreground process group and one or more background process groups. Therefore, a session should include the control process (session first process), a foreground process group, and any background process group. Open a terminal, that is, to start a session, another session to run a process, you can view its status at another terminal, when the Kill command is released, the system will kill it to view the terminal equipment each process can access its control terminal through a special device file/dev/tty. In fact, each terminal equipment corresponds to a different device file,/dev/tty provides a common interface, a process to access its control terminal, either through the/dev/tty can also be accessed through the device files corresponding to the terminal equipment. The Ttyname function can be used by the file descriptor to detect the corresponding file name, the file descriptor must point to a terminalDevice and not can be any file. #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main () {printf ("%d ----> %d \n ", 0,ttyname (0));p rintf ("%d ----> %d \n ", 1,ttyname (1)); printf ("%d ----> %d \n", 2,ttyname (2)); return 0;} My result is not the directory is an address:0 ----> 140038152 1 ----> 140038152 2 ----> 140038152 re-open a run the following:0 ----> 134569992 1 ----> 134569992 2 ----> 134569992 Daemon daemon, also known as Sprite process (Daemon), is a special process running in the background. It is independent of the control terminal and periodically performs some sort of task or waits to handle certain occurrences. A daemon is a process that has a long lifetime. They are often started when the system boots and are terminated only when the system shuts down. Because they do not have control terminals, they are running in the background. Daemon Features: (1) Linux system startup will start a lot of system services process, the daemon does not control the terminal, cannot directly interact with the user (2) Other processes are created when the user logs on or runs the program, terminates at the end of the run or user logoff, However, the daemon is not affected by user log-off, only affected by power-on or shutdown. ps ajx |grep -er ' d]$ ' [[email in System view Protected] 20160729]$ ps ajx |grep -er ' d]$ ' 0 2 0 0 ? -1 s 0 0:00 [ kthreadd] 2 17 0 0 ? -1 S 0 0:00 [kacpid] 2 22 0 0 ? -1 S 0 0:00 [ksuspend_usbd] 2 23 0 0 ? -1 s 0 0:00 [khubd] 2 24 0 0 ? -1 s 0 0:00 [kseriod] 2 28 0 0 ? -1 S 0 0:00 [khungtaskd] 2 30 0 0 ? -1 sn 0 0:00&nbsP [ksmd] 2 38 0 0 ? -1 S 0 0:00 [pciehpd] 2 40 0 0 ? -1 s 0 0:00 [kpsmoused] 2 71 0 0 ? -1 s 0 0:00 [kstriped] 2 954 0 0 ? -1 s 0 0:00 [kauditd][Unified -1 tpgid] and terminal independent self-made user group, From a session, you can create sub-threads Reason for the daemon: The daemon function exists because the control terminal causes some signals to be sent for some reason (such as disconnecting the terminal link). The default action of receiving processing these signals causes the process to exit. These signals are generated by tapping some special keys on the terminal. daemon and background process differences: (1) daemon is a background process, the background process is not necessarily the daemon (2) daemon Run is terminal-independent, is not able to hit the terminal message (3) The Daemon session group and the current directory, file descriptors are independent. Running in the background is just a fork in the terminal, allowing the program to execute Create daemons in the background: the most critical step in creating a daemon is to call the SETSID function to create a new session and become a session leader. #include <unistd.h>pid_t setsid (void), which returns the ID of the newly created session (in fact, the ID of the current process) when the function call succeeds, and returns 1 for the error. Note that the current process does not allow the leader of the process group until this function is called, otherwise the function returns-1. It is also easy to ensure that the current process is not a leader of the process group, as long as you fork and then call Setsid. The child process created by fork and the parent process are in the same input group, the leader of the process group must be the first process of the group, so the child process cannot be the first process in the group, and calling Setsid in the child process isThere's no problem. The result of a successful call to this function is: (1) Create a new session, the current process becomes session leader, the ID of the current process is the session ID. (2) Create a new process group, the current process becomes the leader of the process group, and the ID of the current process is the ID of the process group. (3) If the current process originally has a control terminal, it loses the control terminal and becomes a process without control terminal. The so-called loss of control terminal is that the original control terminal is still open, can still read and write, but only a normal open file instead of the control terminal. Steps to create the daemon: (1) Call Umask to set the file mode creation mask to 0 (2) The parent process fork out the child process, and then the child process calls Setsid, the parent process exits directly (guaranteeing that the child process is not a group leader of the process), (3) Call Setsid to create a new session (the invocation succeeds in making the calling process the first process of a fresh session, and becomes the leader process of a process group. Call process does not have control terminal) (4) Change current working directory to root (5) Close not required file descriptor (6) Ignore SIGCHLD signal #include <stdio.h> #include <stdlib.h># include<signal.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h>void Create_daemon () { int i,f; pid_t pid; struct Sigaction sa; umask (0); if (Pid=fork () <0); if (pid!=0) exit (0); setsid (); sa.sa_handler=sig_ign; sigemptyset (&sa.sa_ Mask); sa.sa_flags=0; if (Sigaction (sigchld,&sa,null) <0) return ; if (Pid=fork () <0) return; if (pid!=0) Exit (0); if (ChDir ("/") <0) return; close (0); f=open ( "/dev/null", O_RDWR); dup2 (f,1); dup2 (f,2);} Int main () { create_daemon (); while (1) {sleep (1); } getchar ();} after compiling runs, PS&NBSP;&NBSP;AJX view PPID PID pgid sid tty tpgid stat uid time command 1 14550 14550 14550 ? -1 ss 500 0:00 ./a.out14550 14552 14550 14550 ? -1 s 500 0:00 ./a.out (1) invokes the function of the fork: The role of the first fork is to let the shell think that this command has been terminated, do not hang on the terminal input, there is for the subsequent Setsid service, because the call setsid function process can not be the process group leader, if not fork out the child process, then the parent process is the process group leader, You cannot call Setsid. After the child process calls the SETSID function, the child process is the session leader is also the process group leader, and out of control terminal, at this time, regardless of the control terminal operation, the new process will not receive some signal to make the process exit. (2) The role of the second fork: Although the current closure and terminal connection, but later may be wrong operation opened the terminal. Only session first process can open terminal equipment, that is, fork again, and then quit the parent process, again fork the child process as a daemon to continue to run, to ensure that the wizard process is not the first process of the conversation, the second is not necessary, is optional, some open source projects on the market is also fork a process Front and rear switcher mode program int main () { while (1) { printf ("Wzzx"); } getchar ();} Run [[Email protected] 20160729]$ ./a.out zxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzXwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzx wzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzx^z[1]+ stopped . /a.out[[email protected] 20160729]$ jobs View [1]+ Stopped ./a.out[[email protected] 20160729]$ fg 1 Front Run (in the dead loop printing can be ctrl+z or ctrl+c dispose of) Zxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzz Xwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzxwzzx Wzzxwzzxwzzx^z[[email protected] 20160729]$ bg 1 Background run (dead loop print cannot be processed by ctrl+z or ctrl+c just shut down the terminal session)
/*
http://blog.csdn.net/asd7486/article/details/51956929
http://blog.csdn.net/yh1548503342/article/details/41891047
Http://blog.sina.com.cn/s/blog_6642cd020101g3tl.html
*/
"LINUX" session process Group job terminal