The process Environment of LINUX_API (II.)

Source: Internet
Author: User

This article index:

1. Introduction

2. Terminal login

3. Process Group

4. Session Period

1. Introduction

Through the previous study, we already know how to control a process, fork The function copies the child process from the parent process, we can pass the exec The function lets the child process run a new program, which can be called by the exit Series functions ( return , _exit wait Span style= "font-family: the song Body;" > or waitpid

There are many processes running on the operating system at the same time, so what is the relationship between these processes? The answer is yes, at least the process is a parent-child relationship, of course, only here to remove the 0,1,2 of these three processes.

Then the main content of this article is to further discuss the interrelationship between these processes.

2. Terminal login

We are now logged into a Linux operating system is skilled, as long as the user name and password can be entered, but for this landing process is not very familiar with, we passed RS-232 The terminal landing process to a glimpse of the specific landing process is how.

3. Process Group

A process group is a collection of processes or processes, and each process belongs to a unique process group, and the group ID is the process ID of the process group leader ,getpgrp function to return the group ID of the process group in which the current process is located .

3.1,getpgrp functions

1), function prototypes and required header files

#include <unistd.h>

pid_t getpgrp (void);

2), function function: gets the group IDof the process group that is the current process that called the function.

3), function parameter: no parameter.

4), function return value: successful, returns the group IDof the process group where the current process is located, failure returns -1,errno is set.

5), note:

A), even if the team leader is dead, the process group still exists as long as there is a member process running in the group.

B), from the time the process group was created to the end of the last process in the group, we are called the lifetime of the process group.

C), The first process running from the shell terminal as the leader, whose process ID is the group ID, and then from the process all the processes that fork out are all part of the group.

D), of course, in addition to the leader process, any team member can be called by the setpgid function into a group or join its group.

6), test cases

TEST.c

#include <stdio.h>#include<unistd.h>intMainvoid){        intRET =0; RET=Fork (); if(0= = ret)//the first child process of the original parent process{ret=Fork (); if(0==ret) {printf ("pgrp =%d\n", Getpgrp ()); }                    Else if(Ret >0) {printf ("pgrp =%d\n", Getpgrp ()); }           }           Else if(Ret >0) {ret= Fork ();//the second child process of the original parent process                if(0==ret) {printf ("pgrp =%d\n", Getpgrp ()); }                    Else if(Ret >0){//Original Parent ProcessSleep1);//Let all child processes run to the end firstprintf"pgrp =%d\n", Getpgrp ()); }            }           return 0;} return 0;} 

The results of the above example run as follows:

PGRP = 4163

PGRP = 4163

PGRP = 4163

PGRP = 4163

You can see that all processes belong to a group, the leader is the original parent process, and all processes are copied from the original parent process to the group.

3.2,setpgid functions

1), function prototypes and required header files

#include <unistd.h>

pid_t Getpgid (pid_t pid, pid_t pgid);

2), function function: Call this function to specify that a process participates in an existing group or to create a new process group.

3), function parameters

pid_t PID: Specifies the process that needs to be modified or set to the new process group.

pid_t pgid: Process group ID, specifying a process group.

4), function return value: successfully returns the group IDof the process group, failure returns -1,errno is set.

5), note:

The function is to modify the process group of the PID process to the pgid corresponding process group. However, the following special cases need to be noted.

1, when the two parameters are equal, the PID corresponding to the process directly into the process group leader.

2, PID fill 0 ,thePID default to use the caller's process ID.

3, Pgid fill 0 ,pgid default to use the PID specified process ID is the process group ID.

4. A process can only set the process group IDfor itself or a child process, but once the child process has been exec it can no longer change its process group ID .

6), examples

A), when a new program is executed at the Shell command line,theshell will fork out the child process to run the new program ,theshell parent process calls the function to set the child process as the new process group leader, and the child process calls the function to set itself as the new process group leader, both of which have a redundancy, but this ensures that regardless of the parent-child process who first runs, Child processes can be set to the new process group leader, no one will produce a state, the result will depend on which process runs first, and then all the child processes copied from the child process belong to the process group.

b), Test cases

#include <stdio.h>#include<unistd.h>intMainvoid){        intRET =0; RET=Fork (); if(0= = ret) {//the first child process of the original parent processSetpgid (Getpid (), Getpid ()); printf ("In chaild pgrp =%d\n", Getpgrp ()); }           Else if(Ret >0){//original parent process, with shell parent process copiedSleep1); printf ("In pareant pgrp =%d\n", Getpgrp ()); }          return 0;} 

The results of the program run as follows:

In Child pgrp = 7930

In pareant pgrp = 7929

The child process that was originally copied and the original parent process belong to a process group, and the original parent process is the team leader, but the child process is called Setpgid (Getpid (), Getpid ()); turn yourself into a new process group, personally as the team leader, so we see that the parent-child process here belongs to their own group, so the child process copied from the child process will belong to a new group, the parent process son out of the child process belongs to the parent process group.

4. Session Period

We know that one or more processes are grouped together to form a group of processes, but when multiple process groups are grouped together, this set is called the session period, as shown in the session with three process groups, consisting of a shell Pipeline Connection between the process groups.

4.1 , Setsid function

1), function prototypes and required header files

#include <unistd.h>

pid_t setsid (void);

2) function function: The process that invokes the function becomes a new session period, but the process group leader cannot be set to a new session period.

3), function parameter: no parameter.

4), function return value: success, return new session ID, failure return -1,errno is set.

5), note

• The process group leader can not be set to a new session, the team leader called the function will be returned with an error, in order to avoid this situation, usually first fork out the child process, and then the parent process is terminated, the child process continues to transport the line, The new child process belongs to the same process group as the parent process, but the child process cannot be the process group leader.

• A process that creates a new session period is the first process in the session, and the session ID is the process ID of the process .

• The session first process also becomes the leader of the first process group in the session.

• This process does not control the terminal, and if the process has a control terminal before calling the Setsid function, the relationship is also dismissed.

6), test cases

5. Control Terminal

characteristics of 5.1 and conversational period

1), a session period can have a control terminal, this is usually our login terminal equipment or pseudo-terminal equipment.

2), establish a dialog with the control Terminal connection the first process is called the control process.

3), multiple process groups in a session are divided into one foreground process group and one or more background process groups.

4), a session period can have 0 or 1 foreground process groups, but at least one background process group.

5), if the session has a control terminal, there must be a foreground process group, the foreground process group has the right to interact with the control terminal, all the other process groups are background process groups.

6), whenever you press the interrupt key (Ctrl + C or ctrl+\), its SIGINT or sigquit signals are sent to all processes in the foreground process group.

7), if the interrupt interface has detected that the modem has detached or the terminal has been disconnected, the hang-up signal will be sent to the control process.

Example Description:

1, when we open a terminal, the terminal itself is a running process, the process creates a new session, the shell process is to create this session of the first process, is also a control process, this session only one process group, Is the group of the first session process, and also the foreground process group, when we can enter the Shell command directly to the Shell terminal, because the foreground process group has the right to control the terminal interaction.

2, when we ./a.out Run a new program on the shell terminal without adding &, without putting the program in the background to run the case, The first process of the program is set up by itself as a group leader, the process that it replicates belongs to this process group, the process group becomes the foreground process group, we have terminal interaction with the team leader of the process group in the foreground, if we put the new program in the background (such as ./a.out &). The new running program creates a new group of background process groups, andtheShell Terminal command line still controls the terminal interaction rights.

3. All process groups that are listed in the process group from which you are running the new program will all become background process groups.

The process Environment of LINUX_API (II.)

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.