Process:
A sequence of operations performed by a program in execution on a given set of input data
Process identifier: PID unique
Non negative integer
Although a PID is unique,UNIX reuses the number of terminate processes
No mater in what kinds of computer systems,after the processed terminate all the system should release the resources which is occupied by the processes.Otherwise the resources of the system will be running out.
#include<unistd.h>
pid_t getpid(); //process id
Pid_t getppid(); //parent process id
Uid_t getuid(); //get the real user id
Gid_t geteuid(); //get the effective id
Process creation:
System call fork() create a new child process
- The child is a copy of the parent excluding the Process ID return by fork
The parent process receives the child PID.A process may have more than one child that can identify on the basis of its PID
The child process receives the value of 0. It can identify its parent by means of the system call getppid()
- Fork is issued once in the parent process,but returns in two different processes,and returns different values to the parent ,and to the child.
#include <unistd.h>
...
Pid_t pid;
...
Pid=fork();
Switch(pid){
Case -1: //fork failure
...
Exit(-1);
Case 0: //child Zero in the child process
...
Default: //parent Child PID in the parent process
...
}
- Write a concurrent program that, given has its argument, generates n children processes Each child process outputs its PID and terminates
System call wait() and waitpid()
When a process terminates normally or not
The kernal sends a signal (SIGCHLD)to it parent
For the parent this is an asynchronous event
The parent process may
Manage the child termination and or the signal
Ignore the default event
#include <sys/wait.h>
*Pid_t wait(int statLoc);
- Blocks the calling process if all its children are running(none is already terminated)
Wait will return as soon as one of its children terminates
- Returns an error if the calling process has not children
#include<sys/wait.h>
Pid_t waitpid(
Pid_t pid;
Int statLoc;*
Int option);
If a paremt want to wait a specific child it is better to use waitpid,which suspends execution of the calling process until a child,specified by pid argument,has changed state.By default,waitpid() waits only for terminated children.
System call exec()
System call exec substitutes the process code with the executable code of another program
The new program begins its execution as usual from main
Exec:
Does not creat new process
Substitutes the calling process image with the image of another program
The process PID does not change
Fork->duplicates an existent process
Exec->executes a new program
- 6 versions of exec system call
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg, ..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],char *const envp[]);
参数
L arguments are a list of strings
V arguments is a vector of strings char **arguments
path参数表示你要启动程序的名称包括路径名
arg参数表示启动程序所带的参数
返回值:成功返回0,失败返回-1
System call system()
#include<stdlib.h>
*Int system(const char string)
System cal system
Forks a shell,which execute the string command,while the [parent process waits the termination of the shell command
Returns -1 or 127 on error
The exit value of the shell that executed the command (with the format of waitpid)
Reference:
Linux下进程终止过程
http://blog.csdn.net/tuzhutuzhu/article/details/30029877
进程与线程的一个简单解释
http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html
Linux系统编程:fork函数的使用【循环创建N个子线程】
http://blog.csdn.net/qq_29542611/article/details/79513845
创建N个子进程
http://blog.csdn.net/sum_tw/article/details/53671227
linux中fork()函数详解(原创!!实例讲解)
http://blog.csdn.net/jason314/article/details/5640969
linux下的僵尸进程处理SIGCHLD信号
http://www.cnblogs.com/wuchanming/p/4020463.html
linux系统编程之进程(三):exec系列函数和system函数
https://blog.csdn.net/jnu_simba/article/details/8930400