Apue Study Notes (chapter eighth Process Control)

Source: Internet
Author: User

This chapter describes process Control for UNIX systems, including creating new processes, executing programs, and terminating processes.

Process identity

Each process has a non-negative integer representing the unique process ID, with the exception of the process ID, and there are other identifiers for each process. The following functions return these identifiers

#include <unistd.h>pid_t getpid (void);p id_t getppid (void); uid_t Getuid (void); uid_t geteuid (void); gid_t getgid (void); gid_t getegid (void);

function fork

An existing process can call the fork function to create a new process

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

A new process created by Fork is called a child process. the fork function is called once, but returns two times :

The return value of the child process is 0, and the return value of the parent process is the process ID of the new child process.

Whether the parent or child process executes after the fork is indeterminate depends on the scheduling algorithm used by the kernel.

The child process and the parent process continue to execute the instruction after the fork call, and the child process obtains a copy of the parent process data space, heap, and stack. Note that the parent-child process does not share these storage spaces, and only the body segment is shared.

function vfork

The Vfork function is used to create a new process, and the purpose of the new process is to exec a new program.

Vfork creates a child process just like fork, but it does not completely copy the address space of the parent process into the child process because the child process calls exec (or exit) immediately.

However, before the child process calls exec or exit, it runs in the space of the parent process.

Another difference between vfork and fork is that vfork guarantees that the child process runs first, and the parent process may be scheduled to run after it calls exec or exit.

function wait and waitpid

#include <sys/wait.h>pid_t wait (int *statloc);p id_t waitpid (pid_t pid,int *statloc,int options);

The differences between the two functions are as follows:

Before a child process terminates, wait causes its callers to block, and Waitpid has an option that allows the caller to not block.

Waitpid does not wait for the first terminating child process after its invocation, it has several options to control the process it waits for.

The function of the PID parameter in the Waitpid function is interpreted as follows:

Pid==-1 waits for either child process. Equivalent to wait

Pid>0 waiting for process ID to be equal to PID child process

pid==0 wait group ID equals any child process that invokes the process group ID

Pid<-1 any child process that waits for a group ID equal to the PID absolute value

The options parameter allows us to further control the operation of the Waitpid. This parameter is either 0, or the result of a bitwise OR operation in a constant

function exec

There are 7 different exec functions to use, and when a process calls an EXEC function, the program executed by the process is completely replaced by the new program.

#include <unistd.h>intEXECL (Const Char*path,Const Char*arg, ...);intEXECLP (Const Char*file,Const Char*arg, ...);intExecle (Const Char*path,Const Char*arg,...,Char*Constenvp[]);intExecvConst Char*path,Char*Constargv[]);intEXECVP (Const Char*file,Char*Constargv[]);intEXECVPE (Const Char*file,Char*ConstArgv[],Char*Constenvp[]);intFexecve (intFdChar*ConstArgv[],Char*ConstEnvp[]);

path Specifies that the path name is a parameter when file is specified as a parameter:

1 if file contains/, it is treated as a path name

2 Otherwise, follow the PATH environment variable and search for the executable file in the directory it specifies

The difference between 7 exec functions

These 7 functions only execve are system calls to the kernel. The other 6 are just library functions, and ultimately call the system call. Is the relationship between these 7 functions

function system

#include <stdlib.h>intchar *cmdstring);

Apue Study Notes (chapter eighth 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.