Advanced Programming for UNIX environments (eight) process relationships

Source: Internet
Author: User
Tags session id terminates

In this chapter, people indefinitely the feeling, mainly do not know how to use the actual. Learning from the previous chapters, each process has a parent process, and when the child process terminates, the parent process is notified and gets the exit status of the child process. First of all, the basic knowledge of this chapter is summarized as follows, in the future look at the time to summarize.

1. Terminal Login

The process of Terminal login Unix system is introduced. Read the file through the init process/etc/ttys,fork a child process to call the Exec execution Getty program to log in, when the user entered the user name, Getty the completion of the work, and then call the login program, similar to Execle ("/bin/login", " Login ","-P ", username, (char *) 0,ENVP).

2. Network Login

Through the kernel of the network interface driver, using pseudo-terminal driver, BSD in the inetd process waiting for the network connection. The process is: the inti process calls a shell, starts the daemon inetd, waits for TCP/IP connection requests to reach the host, and when a connection request arrives, fork a child process to execute the program.

3. Process Group

Each process, in addition to a process ID, belongs to a process group, which is a collection of one or more processes, and each process group has a unique process group ID, which can be obtained through the GETPGRP function. Each process group can have a leader process whose process group ID equals its process ID. Leader process You can create a process group and create a process in the reorganization. Add an existing group by using the Setpgid function or create a new process group.

4. Session

A session is a collection of one or more process groups, usually a set of several processes programmed by the Shell's pipeline lines. The process calls the SETSID function to create a new session, and the GETSID function returns the process group ID of the session's first process that called the process.

5. Control Terminal

A session can use a control terminal; A session first process connected to a control terminal is called a control process; Several process groups in a session can be divided into foreground process groups and background process groups; There is only one foreground process group in a session, but there can be multiple background process groups; interrupts (Ctrl+c,sigint), Exit (Ctrl+/,sigquit) and suspend (CTRL+Z,SIGTSTP) characters generate signals sent to the foreground process group.

6. Operation Control

Start multiple jobs (process groups) on one terminal, and control jobs run in front of the background. Job control requires 3 support and collaboration: (1) shell support, (2) kernel terminal driver Support, and (3) kernel support for job control signals.

7. Shell Execution Program

The PS command is used to output the process ID, parent process ID, process group ID, session ID, command.

ps-o Pid,ppid,pgid,sid,comm

8. Orphan Process Group

A process that has been terminated by a parent process is called an orphan process (orphan), which is adopted by the INIT process. when a control process is finished, its control terminal is also released, and the other session can use the terminal as the control terminal. In order to prevent the process in the old session from trying to continue using the terminal, the concept of orphan process Group was proposed. The orphan Process group refers to the group of processes that the session leader has terminated but continues to execute. When a process is composed of an orphan process group, the kernel sends a SIGHUP signal to all processes in that process group. For sighup signals, the process is terminated by default. However, the user can capture this signal, thus not being terminated, but still cannot access the terminal. Orphan process Group definition: The parent process for each member in the reorganization is either a member of the group or is not a member of the session to which the group belongs. A process group is not an orphan process group the condition is that the group has a process whose parent process is in another group that belongs to the same session.

Create an orphan process group program as follows:

1 #include <stdio.h>2 #include <stdlib.h>3 #include <sys/wait.h>4 #include <unistd.h>5 #include <sys/types.h>6 #include <errno.h>7 #include <signal.h>89Staticvoid Sig_hup (IntSigno)10{printf ("SIGHUP received,pid =%d\n", Getpid ());12}1314Staticvoid Pr_ids (char *Name15{printf ("%s:pid =%d,ppid =%d,pgrp =%d,tpgrp =%d\n",17Name,getpid (), Getppid (), Getpgrp (), Tcgetpgrp (Stdin_fileno));18Fflush (stdout);19}2021stIntMain ()22{23CharC24pid_t pid;25Pr_ids ("Parent");27if (PID = fork ()) <0)28{Perror ("Fork () Error");Exit (-1);31}32Elseif (PID >0)33{Sleep (5);Exit (0);36}37Else38{Pr_ids ("Child");40Signal (SIGHUP,SIG_HUP);41  Kill (Getpid (), SIGTSTP);  Pr_ids ( "child" 43 if (Read (Stdin_fileno,&c, 1)! = 1) 44 printf (" read error form controlling TTY, errno =%d\n ",errno);  0 }47}        

The results of the implementation are as follows:

Add:

PID = Process ID (generated by the kernel based on deferred reuse algorithm )
PPID = parent Process ID (can only be modified by the kernel)
Pgid = Process Group ID (child process, parent process can be modified)
SID = Session ID (the process itself can be modified, but with limitations, see below)
Tpgid= Control terminal process group ID (modified by control terminal to indicate the current foreground process group)

Zombie Process: Terminates before the parent process, but the parent process does not dispose of it (gets the information about terminating the child process, releasing the resources it still occupies). The only way to eliminate a zombie process is to terminate its parent process.
Orphan process: The process's parent process terminates before itself. It is characterized by the ppid=1 (the ID of the init process). An orphan process can be a self-made orphan process group.

A process belongs to a process group, a process group belongs to a session, a session may or may not have a control terminal

Advanced Programming for UNIX environments (eight) process relationships

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.