LINUX Process Control (2)

Source: Internet
Author: User
Article Title: LINUX Process Control (2 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

3. Process Control and Termination

3.1. Process Control

After a process creates a child process, if the child process exits first, the system will not automatically clean up the environment of the child process, but the parent process must call the wait or waitpid function to complete the cleaning, if the parent process does not clean up, the child process that has exited will become a zombie process (defunct). If too many zombie processes exist in the system, the system performance will be affected, therefore, the zombie process must be processed.

In another case, if the parent process exits before the child process, the child process becomes an orphan process. After the orphan process exits, the cleaning work of the parent process init is automatically processed.

Function prototype:

# Include

# Include

Pid_t wait (int * status );

Pid_t waitpid (pid_t pid, int * status, int options );

Both wait and waitpid wait for an exited sub-process and clean up the process;

The wait function randomly waits for an exited sub-process and returns the pid of the sub-process;

Waitpid waits for the child process of the specified pid;

The status parameter is an outgoing parameter that stores the exit code of the sub-process;

Options has two Optional options: WNOHANG and WUNTRACED. The two parameters are available | merge. WNOHANG indicates that the system will return immediately no matter whether the sub-process exits or not, and WUNTRACED is rarely used.

Example:

//{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ {{{{{{{{{{{{{{{{{{{{{{{

# Include

# Include

# Include

# Include

# Include

# Include

Void SignChildPsExit (int iSignNo)

{

Pid_t pid;

Int iExitCode;

Pid = wait (& iExitCode );

Printf ("SignNo: % d child % d exit \ n", iSignNo, pid );

}

Int main ()

{

Signal (SIGCHLD, SignChildPsExit );

Printf ("Parent process id: % d \ n", getpid ());

Pid_t iRet = fork ();

If (iRet <0 ){

Printf ("Create child process fail! \ N ");

Return-1;

}

//}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} }}}}}}}}}}}}}}}}}}}}}}}}}

3.2. Process Termination

There are five methods to terminate a process:

The natural return of the main function;

Call the exit function

Call the _ exit function.

Call the abort Function

Receive a signal that can cause Process Termination

The first three methods are normal termination, and the last two are non-normal termination. However, either way, when the process is terminated, it will execute the same closed open files and release the occupied memory and other resources. Only the last two termination methods will cause some code of the program to fail to be executed normally, such as the object structure and the execution of the atexit function.

Kill Function

Prototype:

# Include

# Include

Int kill (pid_t pid, int sig );

The kill function sends a signal sig to the process pid. You can run the shell command kill-l to view the signal that can be sent. If the process receiving the signal does not process the signal, it will be automatically terminated.

4. Inheritance of files opened between processes

4.1. use fork to inherit open files

After fork, the child process automatically inherits the files opened by the parent process. After inheritance, the files opened by the parent process when it is disabled will not affect the child process.

Example:

# Include

# Include

# Include

# Include

# Include

Int main ()

{

Char szBuf [32] = {0 };

Int iFile = open ("./a.txt", O_RDONLY );

If (fork ()> 0) {// parent process

Close (iFile );

Return 0;

}

// Child process

Sleep (3); // wait for parent process closing fd

If (read (iFile, szBuf, sizeof (szBuf)-1) <1 ){

Perror ("read fail ");

} Else {

Printf ("string: % s \ n", szBuf );

}

Close (iFile );

Return 0;

}

[1] [2] [3] Next page

Related Article

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.