返回当前进程ID
#include<unistd.h> int main() { printf("The current process ID is %d\n",getpid()); exit(0); } 8.2 fork调用(调用成功时对父进程返回子进程的PID,对子进程返回0,调用失败时返回-1)
#include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <string.h> extern int errno; int main() { char buf[100]; pid_t cld_pid; int fd; int status; if ((fd=open("temp",O_CREAT|O_TRUNC | O_RDWR,S_IRWXU)) == -1) { printf("open error %d",errno); exit(1); } strcpy(buf,"This is parent process write\n"); if ((cld_pid=fork()) == 0) { /* 这里是子进程执行的代码 */ strcpy(buf,"This is child process write\n"); printf("This is child process\n"); printf("My PID(child) is %d\n",getpid()); /*打印出本进程的ID*/ printf("My parent PID is %d\n",getppid()); /*打印出父进程的ID*/ write(fd,buf,strlen(buf)); close(fd); exit(0); } else { /* 这里是父进程执行的代码 */ printf("This is parent process\n"); printf("My PID(parent) is %d\n",getpid()); /*打印出本进程的ID */ printf("My child PID is %d\n",cld_pid); /*打印出子进程的ID*/ write(fd,buf,strlen(buf)); close(fd); } wait(&status); } exec实例
#include <stdio.h> int main(int argc, char *argv[], char *envp[]) { printf("\n### ARGC ###\n%d\n", argc); printf("\n### ARGV ###\n"); while(*argv) printf("%s\n", *(argv++)); printf("\n### ENVP ###\n"); while(*envp) printf("%s\n", *(envp++)); return 0; }
#include <unistd.h> main() { char *envp[]={"PATH=/tmp", "USER=lei", "STATUS=testing", NULL}; char *argv_execv[]={"echo", "excuted by execv", NULL}; char *argv_execvp[]={"echo", "executed by execvp", NULL}; char *argv_execve[]={"env", NULL}; if(fork()==0) if(execl("/bin/echo", "echo", "executed by execl", NULL)<0) perror("Err on execl"); if(fork()==0) if(execlp("echo", "echo", "executed by execlp", NULL)<0) perror("Err on execlp"); if(fork()==0) if(execle("/usr/bin/env", "env", NULL, envp)<0) perror("Err on execle"); if(fork()==0) if(execv("/bin/echo", argv_execv)<0) perror("Err on execv"); if(fork()==0) if(execvp("echo", argv_execvp)<0) perror("Err on execvp"); if(fork()==0) if(execve("/usr/bin/env", argv_execve, envp)<0) perror("Err on execve"); }
僵尸进程
#include <sys/types.h> #include <unistd.h> main() { pid_t pid; pid=fork(); if(pid<0) /* 如果出错 */ printf("error occurred!\n"); else if(pid==0) /* 如果是子进程 */ exit(0); else /* 如果是父进程 */ sleep(60); /* 休眠60秒,这段时间里,父进程什么也干不了 */ wait(NULL); /* 收集僵尸进程 */ } wait调用
#include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <stdlib.h> main() { pid_t pc,pr; pc=fork(); if(pc<0) /* 如果出错 */ printf("error ocurred!\n"); else if(pc==0){ /* 如果是子进程 */ printf("This is child process with pid of %d\n",getpid()); sleep(10); /* 睡眠10秒钟 */ } else{ /* 如果是父进程 */ pr=wait(NULL); /* 在这里等待 */ printf("I catched a child process with pid of %d\n"),pr); } exit(0); }
实战参数status [ pit_t wait(int *status) ]
#include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h>
int main(int argc,char **argv) { int status; pid_t pc,pr; pc=fork(); if(pc<0) /* 如果出错 */ printf("error ocurred!\n"); else if(pc==0){ /* 子进程 */ printf("This is child process with pid of %d.\n",getpid()); exit(3); /* 子进程返回3 */ } else{ /* 父进程 */ pr=wait(&status); if(WIFEXITED(status)){ /* 如果WIFEXITED返回非零值 ,提取子进程的返回值如果调用exit(5)则返回5*/ printf("the child process %d exit normally.\n",pr); printf("the return code is %d.\n",WEXITSTATUS(status)); }else /* 如果WIFEXITED返回零 */ printf("the child process %d exit abnormally.\n",pr); } return 0; }
waitpid实例
#include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <sys/wait.h>
int main(int argc, char **argv) { pid_t pc, pr; pc=fork(); if(pc<0) /* 如果fork出错 */ printf("Error occured on forking.\n"); else if(pc==0){ /* 如果是子进程 */ sleep(10); /* 睡眠10秒 */ exit(0); } /* 如果是父进程 */ else{ do{ pr=waitpid(pc, NULL, WNOHANG); /* 使用了WNOHANG参数,waitpid不会在这里等待 */ if(pr==0){ /* 如果没有收集到子进程 */ printf("No child exited\n"); sleep(1); } }while(pr==0); /* 没有收集到子进程,就回去继续尝试 */ if(pr==pc) printf("successfully get child %d\n", pr); else printf("some error occured\n"); } }
|