Process Group
Collection Process group ID: positive integer for one or more processes
two functions
getpgid (0) =getpgrp ()
Eg: shows the process group ID of the child process and the parent process
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
5 int main () {
6 pid_t pid;
8 if ((Pid=fork ()) <0) {
9 printf ("fork error!");
Ten }else if (pid==0) {
One by one printf ("The child process PID is%d.\n", getpid ());
printf ("The Group ID is%d.\n", getpgrp ());
printf ("The Group ID is%d.\n", Getpgid (0));
printf ("The Group ID is%d.\n", Getpgid (Getpid ()));
exit (0);
+ }
sleep (3);
printf ("The parent process PID is%d.\n", getpid ());
printf ("The Group ID is%d.\n", getpgrp ());
return 0;
23}
Process Group ID = parent Process ID, which is the parent process for the leader process
Leader process leader Process ID: its process group id== its Process ID leader process can create a process group, create a process in the process group, and then terminate as long as there is a process in the process group exists, the process group exists, and the leader process terminates regardless of the process group lifetime: Process group creation to the last process left ( Terminate or transfer to another process group) a process can assume that
own or child processesSet Process Group ID Setpgid () Join an existing process group or create a new process group
Eg: parent process changes itself and group ID of child process
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 int main () {
6 pid_t PID;
7
8 if ((Pid=fork ()) <0) {
9 printf ("fork error!");
Ten exit (1);
}else if (pid==0) {
printf ("The child process PID is%d.\n", getpid ());
printf ("The Group ID of Child is%d.\n", Getpgid (0)); Return Group ID
Sleep (5);
printf ("The Group ID of child was changed to%d.\n", Getpgid (0));
+ exit (0);
17}
18
Sleep (1);
Setpgid (PID,PID); Change the group ID of the child process to the child process itself
21st
Sleep (5);
printf ("The parent process PID is%d.\n", getpid ());
printf ("The parent of the parent process PID is%d.\n", getppid ());
printf ("The Group ID of the parent is%d.\n", Getpgid (0));
Setpgid (Getpid (), Getppid ()); Change the parent process's group ID to parent process
printf ("The Group ID of the parent is changed to%d.\n", Getpgid (0));
28
return 0;
30}
Session: A collection of one or more process groups starts at the end of a user logon and the user exits this period for all processes that belong to this session
To establish a new session: Setsid () function The calling process is the leader process, then an error is returned first call fork, the parent process terminates, the child process calls the calling process is not the leader process, then create a new session? The process becomes the new session header session Leader process for a new process group. The process does not control the terminal, if there is before, it will be interrupted the team leader process can not become new session first process, new session of the first process will certainly become the leader process ...
Session ID: Process group ID of Session first process get session id:getsid () function
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
5 int main () {
6 pid_t pid;
8 if ((Pid=fork ()) <0) {
9 printf ("fork error!");
Ten exit (1);
}else if (pid==0) {
printf ("The child process PID is%d.\n", getpid ());
printf ("The Group ID of Child is%d.\n", Getpgid (0));
printf ("The Session ID of Child is%d.\n", GetSID (0));
sleep (10);
Setsid ();//Sub-process non-leader process, it becomes new session first process, and become the leader process. The Process group ID is the session process
-printf ("changed:\n");
printf ("The child process PID is%d.\n", getpid ());
printf ("The Group ID of Child is%d.\n", Getpgid (0));
printf ("The Session ID of Child is%d.\n", GetSID (0));
Sleep (20);
exit (0);
(+ }
return 0;
26}
After calling Setsid () in a child process, the child process becomes the new session first process and becomes a leader process with the process group ID equal to the session ID
Daemon Linux Most of the services are implemented through the daemon process, completing many system tasks 0: The scheduling process, called the exchange process (swapper), the kernel part, the system process 1:init process, the kernel call, responsible for booting the Linux system after the kernel boot no terminal restrictions let a process not Because users, terminals, or other changes are affected, you must turn this process into a daemon
Daemon Process Programming steps
1. Create child process, parent process exits?
all work is done in child processes?
formally out of control terminal
2. Create a new session in a child process?
Setsid () function?
make a child process completely independent, out of control
3. Change the current directory to the root directory?
chdir () function?
prevents the use of an unmounted file system?
can also be replaced by other paths
4. Resetting the file permission mask?
umask () function?
prevent inherited file creation masks from denying certain permissions?
Increased daemon Flexibility
5. Close the file descriptor?
inherited open files are not used, waste system resources, cannot be uninstalled?
getdtablesize ()?
returns the number of entries in the file descriptor for the process, that is, the text that the process opens
Number of pieces
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/wait.h>
6 #include <sys/types.h>
7 #include <fcntl.h>
8
9 int main () {
Ten pid_t pid;
int i,fd;
Char *buf= "This is a daemon program.\n";
13
if ((Pid=fork ()) <0) {
printf ("fork error!");
+ exit (1);
}else if (pid>0)//fork and exit parent process
Exit (0);
19
Setsid (); Creates a new session in a child process.
ChDir ("/"); Set working directory as root
Umask (0); Set the permission mask
(I=0;i<getdtablesize (); i++)//getdtablesize returns the number of items in the child process file descriptor descriptor
Close (i); Close the file descriptors that are not going to be used
25
(1) {//Dead loop characterization it will always run
27//Read-write to open "/tmp/daemon.log", the returned file description assigned to FD
if (Fd=open ("/tmp/daemon.log", o_creat| o_wronly| o_append,0600)) <0) {
printf ("Open file error!\n");
Exit (1);
31}
32//write BUF to FD
Write (Fd,buf,strlen (BUF) +1);
Close (FD);
Sleep (10);
printf ("Never output!\n");
37}
38
0;
40}
Because StdOut was turned off, so "never ouput!" Not output.
View/tmp/daemon.log, which shows that the program is always running
Reproduced linux--Process Group, session, Daemon