UNIX Environment Advanced Programming Learning Note (V): Process Control

Source: Internet
Author: User
Tags terminates

1 getpid function,getppid function , get process ID, get parent process ID

#include <unistd.h>pid_t getpid (void) pid_t getppid (void) uid_t Getuid (void) gets the actual user iduid_t Geteuid (void) to obtain a valid user idgid_t Getgid (void ) Get the actual group idgid_t getegid (void) to get a valid group ID

2 Fork function, the current process creates a new process

#include <unistd.h>pid_t fork (void)

The child process returns 0, the parent process returns the child process ID, and an error returns -1

3 When the parent process terminates before the child process, the parent process of the child process is converted to the init process, guaranteeing that each process has a parent process .

When a child process terminates before the parent process, the kernel retains some information for each terminating process, and the parent process can call wait, andthewaitpid function obtains this information.

If a process has been terminated, but the parent process does not dispose of it (getting information about it, freeing the owning resource), such a process is called a zombie process

When a child process becomes a child process of the INIT process, when it terminates,theinit process immediately calls waitto prevent it from becoming a zombie process

4 When a child process terminates (normal or abnormal), the kernel sends a SIGCHLD signal to the parent process , and the default option is to ignore the signal

5 Wait function

#include <sys/wait.h>pid_t wait (int *intint options)


6 exec function, starting from the main function to re-execute the new program

#include <unistd.h>execl take pathname and parameter EXECLP search for executable file in path, execle use new environment list EXECV parameter list

7 system functions, very easy to perform some operations

#include <stdlib.h>int system (char* cmdstring);

the system principle is to call fork, exec a shell in the subprocess, and Cmdstring , the main process calls the waitpid child process for aftercare

UNIX Environment Advanced Programming Learning Note (V): Process Control

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.