And inter-process communication (2)

1 Create a process

fork () function to create a new process, prototype is as follows:

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

fork () function running process split off another child process, it creates a copy of the parent process child process by the way. Child and parent processes have the same code space, file descriptors, and other resources

 

 

fork () function to create a process, if successful, will return to the parent and child each time, which returns the child process to the parent process PID, a child process returns 0; failure to return an error code is less than zero.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main (int argc, char * the argv []) {
pid_t PID;
PID = the fork (); / * creation process * /
IF (pid == 0) {/ * * child process returns 0 /
the printf ( "IS Child Here, My% D = PID, PID = parent apos% D \ n-", getpid (), getppid ()); / * print Sons process the PID * /
Exit (0);
} the else IF (PID> 0) {/ * parent process returns sub-process the PID * /
the printf ( "Here IS parent, My PID =% D, child apos PID =% D \ n- ", getpid (), PID);
} the else {/ * * the fork error /

perror("fork error\n");
}
return 0;
}

2 terminate the process: divided into normal termination and abnormal termination of two categories.

Normal termination method:

L () function returns the return from main;
L call type exit () function.

Abnormal termination methods are:

l calling abort () function;
l receiving a termination signal.

main () call a class by itself when the exit function uses the return instruction return () function to terminate the process. Terminates abnormally abort () function is realized by SIGABRT signal.

Usually in the Shell, you can use the $? Variable to obtain the exit status of the last run of the program. Use "echo $?" Command to get the program exits the state, to a value of 0, which indicates successful execution

3 exec family of functions

exec family of functions to replace the execution of the program of the calling process.

 

 

Call exec family of functions does not create a process, so ID is not changed before and after the process, exec just to replace the current process of implementation of the program with a new program.

Group exec function has six different exec function, the function prototype are as follows:
#include <unistd.h>
extern char ** Environ;
int the execl (const char * path, const char * Arg, ...);
int execlp ( File * char const, Arg const char *, ...);
int execle (const char * path, const char * Arg, ..., const char * envp []);
int to execv (const char * path, char * the argv const []);
int execvp (File const char *, const char * the argv []);
int execvpe (File const char *, const char * the argv [], char const * envp []);

 

Suffix the p- : indication filename as an argument, if the filename with "/", is considered the path name, or search for executable files in each directory specified by the PATH environment variable, as execlp () function. No suffix p is used to specify the name of the executable file path location, such as the execl () function.

Suffix E : represents a pointer to pass the environment string pointer array, NULL terminated array needs environment, such as execvpe () function. And without this suffix function using the existing environ environment variable for the new program to copy the calling process, as execv () function.

Suffix L : indication parameter list passed to form a new program, the new program all the parameters passed in the form of a variable parameter given from the Exec, the last parameter is needed to indicate the end of NULL as the execl () function.

Suffix V : indicates the parameters passed to the new program, the new program all parameters passed into a string array using vector form, to the end of the array to indicate the end of NULL as to execv () function.

 

exec family of functions will only return when something goes wrong, if successful, the function does not return, otherwise -1.

4 wait () function

wait () function is used to help the parent process to obtain the exit status of its child processes. When the process exits, the kernel to save a certain amount of exit status information for each process, the parent process to exit based on this information to determine the health of the child process. If the parent does not wait () function call, the child process exits information remains stored in memory.

l zombie process: if the child dies before, but not for its parent process calls wait () function, then the child process becomes a zombie process. Zombie process in which the parent calls wait for it () will always occupy a function before the system
memory resources.

l orphan process: If the parent process terminates before the child process has not been terminated will become orphaned. Orphan process will directly receive the init process tube, by the init process is responsible for collecting their exit status.

wait () Function Prototype

include <sys/types.h>

#include <sys/wait.h>

pid_t wait(int *status);

Status parameter is used to save a child process exit status pointer, then get to the PID function is successful exit status process returns, otherwise it returns -1.

 

 

 

5 daemon

    Daemon (Daemon) is running a special process in the background. It is independent of the control terminal and periodically perform some task or pending certain events occur, it does not require user input will be able to run and provide a service. The parent is init daemon process, because it really is the parent process after the fork in a child process exits before the child process exit, so it is an orphan inherited by init process.

daemon () function prototypes

#include <unistd.h>

int daemon(int nochdir, int noclose);

Parameters nochdir: working directory if you pass 0, the function will be called daemon process set to the root directory,

Otherwise, keep the original working directory unchanged;

Parameters noclose: If you pass 0, the daemon function will redirect standard input, standard output, standard error output to / dev / null file, or do not change these file descriptors.

This function returns 0 if successful, otherwise it returns -1 and sets errno.

 

Guess you like

Origin www.cnblogs.com/xiaoli94/p/11235704.html