大家好,欢迎来到IT知识分享网。
进程01–父子进程
#include<sys/types.h> #include<unistd.h> #include<stdio.h> int main(int agrc,char *argv[]) {
pid_t pid = fork(); if(pid >0)//父进程返回值大于0,该值为创建的子进程ID {
int a =0; while(1) {
printf("a = %d\n",a++); sleep(1); pid_t parent_pid = getpid();//返回进程id /* printf("父进程死亡\n"); return 0;//返回可以杀死进程 */ } } if(pid==0)//子进程的返回值0 {
int b = 0; while(1) {
printf("b = %d\n",b++); sleep(1); pid_t sun_pid = getpid();//返回当前进程id pid_t parent_pid = getppid();//返回父进程id /* printf("子进程死亡\n"); return 0;//返回可以杀死进程 */ } } return 0; } 1.当父进程先于子进程死亡时,子进程成为“孤儿进程”,被系统进程收养; 2.当子进程先于父进程死亡时,子进程进入“僵尸态”,等到被父进程回收资源后才死亡; 3.子进程的数据等是从父进程继承的(类似于拷贝),但父子进程的内存空间是相互独立的, execl函数族 #include <unistd.h> extern char environ; --------------不带p的------------------------ int execl(const char *path, const char *arg, ...); int execle(const char *path, const char *arg, ..., char * const envp[]); int execv(const char *path, char *const argv[]); --------------带p的------------------------ int execlp(const char *file, const char *arg, ...); int execvp(const char *file, char *const argv[]); int execvpe(const char *file, char *const argv[], char *const envp[]); 带p的和不带p的功能一模一样,但是带p的会自动获取系统的环境变量; 所以执行程序时不需要添加绝对路径; 例子: int execl(const char *path, const char *arg, .../* (char*) NULL*/) 参数1:path->需要执行程序的绝对路径 参数2:arg->执行的程序的参数列表 注意:最后一个参数必须为NULL execl("/xx/xx/xx/testexe","./testexe","123","abc",NULL); 这里的"123","abc"需要在被调用程序中的argv[1],argv[2]接收;argv[0]是执行的程序名 类似于在程序所在目录打开终端中执行:./testexe 123 abc //调用环境中的程序 int execlp(const char *file, const char *arg, ...); 例子: execlp("testexe","testexe",NULL); 这时的testexe应该在环境目录下,即/bin/文件下,才会在调用执行这句时调用执行testexe程序 ------------------------------------------------- getpid() returns the process ID of the calling process. (This is often used by routines that generate unique temporary filenames.) getppid() returns the process ID of the parent of the calling process. --------------------------------- 父进程回收子进程资源 #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *status); 参数1:子进程的退出状态 返回值:退出的进程PID号 pid_t waitpid(pid_t pid, int *status, int options); 参数1:需要等待的子进程PID号 参数2:子进程的退出状态值 参数3:设置函数是否阻塞等待 0--阻塞 WNOHANG--非阻塞 int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options); /* This is the glibc and POSIX interface; see NOTES for information on the raw system call. */ waitpid() waits only for terminated children, but this behavior is mod‐ ifiable via the options argument, as described below. The value of pid can be: < -1 meaning wait for any child process whose process group ID is equal to the absolute value of pid. -1 meaning wait for any child process. 0 meaning wait for any child process whose process group ID is equal to that of the calling process. > 0 meaning wait for the child whose process ID is equal to the value of pid. -------------------------------------- 判断子进程结束的原因 #include <sys/wait.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char *argv[]) {
pid_t cpid, w; int status; cpid = fork(); if (cpid == -1) {
perror("fork"); exit(EXIT_FAILURE); } if (cpid == 0) {
/* Code executed by child */ printf("Child PID is %ld\n", (long) getpid()); if (argc == 1) pause(); /* Wait for signals */ _exit(atoi(argv[1])); } else {
/* Code executed by parent */ do {
w = waitpid(cpid, &status, WUNTRACED | WCONTINUED); if (w == -1) {
perror("waitpid"); exit(EXIT_FAILURE); } if (WIFEXITED(status)) {
printf("exited, status=%d\n", WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) {
printf("killed by signal %d\n", WTERMSIG(status)); } else if (WIFSTOPPED(status)) {
printf("stopped by signal %d\n", WSTOPSIG(status)); } else if (WIFCONTINUED(status)) {
printf("continued\n"); } } while (!WIFEXITED(status) && !WIFSIGNALED(status)); exit(EXIT_SUCCESS); } } --------------- int wstatus; WIFEXITED(wstatus) ; //正常退出 WIFSIGNALED(wstatus); //被信号杀死 ------------进程退出------- 1.在主函数中调用 return 2.进程中主动调用 exit族函数:--无论在主函数还是子函数调用都会使进程结束 #include<unistd.h> void _exit(int status);//直接退出,不进行清理缓冲区等工作 void exit(int status);//进行收尾的工作(如清理缓冲区等)后,再退出 #include<stdlib.h> void _Exit(int status); 3.主线程中调用 pthread_exit(); 4.被信号所杀死 exit(0); _exit(0);
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/118775.html