Process Control:
1, Process creation function: fork ();
Header file:
#include <sys/types.h>
#include <unistd.h>
Function Prototypes:
pid_t fork (void);
function return Value: 0: Indicates that this process is now a child process;
-1: Indicates an error;
The child process ID number, (an integer greater than 0): Indicates that the parent process is now the process, and the ID number received is the ID number of the child process;
2, fork () return-1 (that is, the cause of process creation error)
1, the system has too many processes, exceeding the system limits; (System level)
2, the user has too many processes, exceeding the Child_max limit; (User level);
3, the use of fork ();
1, the parent process would like to assign their own code, so that the parent-child process different code snippets, such as: network server Classic Code;
2, a process to execute a completely different program, from the fork () return, immediately call the EXEC () function, to execute another program;
4, the child process can be used by getpid to obtain their own ID number, or through getppid () to obtain the parent process ID number;
However, the parent process can only get its own ID number through GETPID, and cannot get the ID number of the child process through other functions; the only chance to get the child process ID number is to get its return value when the parent process calls fork (), which is the ID number of the child process;
5, process content switch: EXEC () function cluster;
Header file:
#include <unistd.h>
Function: The parameter consists of three parts: the path, the parameter passed to the process, the environment variable;
int execl (const char* path, const char* arg,...);
int execv (const char* path, char* const argv[]);
int execle (const char* path, const char* arg1,...., char* const envp[]);
int Execve (const char* path, char* const argv[], char* const envp[]); (a real system call);
int EXECLP (const char* file, const char* arg,....);
int EXECVP (const char* file, char* const argv[]);
Note: In addition to the exec characters,
The parameters of the function that contain "L" will be displayed as a list of arguments passed to the process; Remember, the last parameter is null, and as a sentinel, the argument list is complete; The first argument passed to the process is equivalent to argv[0 in main);
The function contains "V", indicating that the passed parameter will be exhibited as an array of pointers; The first parameter passed to the process is equivalent to argv[0 in main);
The function contains "P", indicating that no path is specified, the system will go to the path specified by the environment variable to find the central area, the expression does not contain P must indicate the path of the file;
The function contains "E", indicating that the user is required to specify environment variables, without p indicating that the process continues to inherit the parent process environment variables;
The return value is only available if there is an error;
Several possible causes of the error:
First, the path is not correct;
Second, the document does not exist;
Thirdly, insufficient authority;
Four, do not know;
Debug to use Perror ("function name:") to print debugging information;
6, System (char * string) function;
Function: Executes a command "string";
The system () function calls/bin/sh to execute the command specified by the parameter,/bin/sh is typically a soft connection, pointing to a specific shell, such as the BASH,-C option, which tells the Shell to read the command from the string command;
During the command execution, the SIGCHLD is blocked, SIGINT and sigquit are ignored, meaning that the process receives both signals without any action.
, the system () function actually performs a three-step operation:
1.fork a sub-process;
2. Call the EXEC function in the child process to execute the command;
3. Call wait in the parent process to wait for the child process to end.
For fork failure, the system () function returns-1.
Source:
int system (const char * cmdstring)
{
pid_t pid;
int status;
if (cmdstring = = NULL)
{
return (1); If cmdstring is empty, returns a value other than 0, typically 1
}
if ((PID = fork ()) <0)
{
status =-1; Fork failed, return-1
}
else if (PID = = 0)
{
Execl ("/bin/sh", "sh", "-C", Cmdstring, (char *) 0);
_exit (127); Exec execution Failure Returns 127, note that EXEC only returns to the current process if it fails, and if successful, the current process does not exist.
}
else/Parent Process
{
while (Waitpid (PID, &status, 0) < 0)
{
if (errno! = eintr)
{
status =-1; Returns 1 if the Waitpid is interrupted by a signal
Break
}
}
}
return status; Returns the return status of the child process if the waitpid succeeds
}
7. Suggestions for using the system () function
The system () function is sometimes convenient, but not abusive!
1), the recommended system () function is used only to execute shell commands, because in general, the system () return value is not 0 to indicate an error;
2), it is recommended to monitor the system () function after the completion of the errno value, for error when giving more useful information;
3), it is recommended to consider the alternative function of the system () function Popen ();
8. Process termination:
When the process terminates, the memory allocated in the "User space" is automatically freed, but the PCB is saved, the state of the exit is saved, and the parent process can use the wait () or waitpid () function to obtain the information from the child process PCB, and then completely clear the sub-process;
Zombie Process: When the parent process has not called wait () or waitpid () to clean up a child process that has already been terminated, the state of the child process is called a zombie state, and the zombie process is normally cleaned up by the parent process at once;
Kill only terminates the process and does not clean up the zombie process;
If a parent process terminates and its child process is not terminated, the parent process of the child process is changed to the INIT process;
Init process when a special process in the system, usually located in/sbin/init, process id = 1, the process at the start of the various system services to start, and then responsible for cleaning up the child process, as long as there is a child process termination, INIT will call the wait () function cleanup;
9. How the process terminates:
Normal termination mode:
1), void exit (int status);
2), void _exit (int status);
How to terminate abnormally:
1), call the abort;
2), terminated by a signal;
10. The difference between exit (int status) and _exit (int status):
Exit () performs some cleanup work first, such as calling each termination handler, writing the contents of the file buffer back to the file, closing all standard I/O streams, and then entering the kernel; header file is stdlib.h;
_exit () immediately enters the kernel; the header file used is unistd.h, the process is stopped, the memory space is cleared, and the various data structures in the kernel are destroyed, including the buffer content that is not written to the file, which is also cleaned out;
_exit (int status) is the underlying system call, and exit (int status) calls the _exit (int status) at the bottom;
Status is used to pass the state at the end of the process; Generally speaking, 0 means the normal end, other results indicate an abnormal end, and the wait () function can receive the return value of the child process, and then perform different processing according to the return value;
11, int atexit (void (* fun) (void));
When you call exit (note, not _exit ()), the various handlers are called before the cleanup work, and these handlers need to be registered with the atexit (void (*void) (void) function, a process can register up to 32 such functions once the registration succeeds , these functions are automatically executed when the system calls exit (), without user intervention, and it is worth noting that the execution of the handler function called by exit () is the opposite of the order in which these functions are registered;
12. The difference between wait and waitpid:
pid_t Wait (int *status):
If the status is null, the process that accepts the end of any state is accepted, and if the status is not NULL, the process that accepts the specified state end is accepted;
Waits for any process to terminate, the status is used to hold the child process's terminating state, and if the processing succeeds, the PID number of the child process is returned;
If the other child processes have ended, or there are no child processes, wait will return immediately;
pid_t waitpid (pid_t pid, init *status, int options);
Pid>0, it means only waiting for the end of a particular process;
Pid=0, say wait??????????????????????????????
Pid=-1, which means waiting for any one of the child processes to end, function and wait () the same;
When the pid<-1, he said???????????????????????
Status with wait ();
Options: When the value is: Wnohang, if the specified PID process is not terminated, the wait is not blocked, but returns immediately, the return value is 0;
When its value is: wuntraced,???????????????????????????
13. What can happen when you call wait and waitpid:
1, blocking: (If the other child processes are still in the running state, it will go into a blocking state to wait;)
2, with the termination information of the child process is returned (if a process has been terminated, is waiting for the parent process to read its terminating state information)
3, error immediately return-1; (if he does not have any sub-processes);
14, Wait () is only a special case of waitpid;
15, using Wait () or waitpid () can realize the synchronization relationship between processes;
16. The int* status in the Wait () and waitpid () parameters contains multiple fields, each of which can be extracted with a macro definition;
If the child process is terminated normally, the value of the wifexited field is not 0,wexitstatus, which is the state of its process exit;
If the child process is terminated abnormally, the field value that the Wifsignaled value field does not take out for 0,wtermsig is the signal number;
17. Daemon: (daemon process)
is a background service process in Linux, independent of the control terminal, and periodically performs some sort of task, or waits for some event to occur;
Starts when the system boots, terminates when the system shuts down;
This article is from the "10891086" blog, please be sure to keep this source http://10901086.blog.51cto.com/10891086/1917535
Process control and common process control functions under Linux