|
返回當(dāng)前進(jìn)程ID
#include<unistd.h> int main() { printf("The current process ID is %d\n",getpid()); exit(0); } 8.2 fork調(diào)用(調(diào)用成功時(shí)對(duì)父進(jìn)程返回子進(jìn)程的PID,對(duì)子進(jìn)程返回0,調(diào)用失敗時(shí)返回-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) { /* 這里是子進(jìn)程執(zhí)行的代碼 */ strcpy(buf,"This is child process write\n"); printf("This is child process\n"); printf("My PID(child) is %d\n",getpid()); /*打印出本進(jìn)程的ID*/ printf("My parent PID is %d\n",getppid()); /*打印出父進(jìn)程的ID*/ write(fd,buf,strlen(buf)); close(fd); exit(0); } else { /* 這里是父進(jìn)程執(zhí)行的代碼 */ printf("This is parent process\n"); printf("My PID(parent) is %d\n",getpid()); /*打印出本進(jìn)程的ID */ printf("My child PID is %d\n",cld_pid); /*打印出子進(jìn)程的ID*/ write(fd,buf,strlen(buf)); close(fd); } wait(&status); } exec實(shí)例
#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"); }
僵尸進(jìn)程
#include <sys/types.h> #include <unistd.h> main() { pid_t pid; pid=fork(); if(pid<0) /* 如果出錯(cuò) */ printf("error occurred!\n"); else if(pid==0) /* 如果是子進(jìn)程 */ exit(0); else /* 如果是父進(jìn)程 */ sleep(60); /* 休眠60秒,這段時(shí)間里,父進(jìn)程什么也干不了 */ wait(NULL); /* 收集僵尸進(jìn)程 */ } wait調(diào)用
#include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <stdlib.h> main() { pid_t pc,pr; pc=fork(); if(pc<0) /* 如果出錯(cuò) */ printf("error ocurred!\n"); else if(pc==0){ /* 如果是子進(jìn)程 */ printf("This is child process with pid of %d\n",getpid()); sleep(10); /* 睡眠10秒鐘 */ } else{ /* 如果是父進(jìn)程 */ pr=wait(NULL); /* 在這里等待 */ printf("I catched a child process with pid of %d\n"),pr); } exit(0); }
實(shí)戰(zhàn)參數(shù)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) /* 如果出錯(cuò) */ printf("error ocurred!\n"); else if(pc==0){ /* 子進(jìn)程 */ printf("This is child process with pid of %d.\n",getpid()); exit(3); /* 子進(jìn)程返回3 */ } else{ /* 父進(jìn)程 */ pr=wait(&status); if(WIFEXITED(status)){ /* 如果WIFEXITED返回非零值 ,提取子進(jìn)程的返回值如果調(diào)用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實(shí)例
#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出錯(cuò) */ printf("Error occured on forking.\n"); else if(pc==0){ /* 如果是子進(jìn)程 */ sleep(10); /* 睡眠10秒 */ exit(0); } /* 如果是父進(jìn)程 */ else{ do{ pr=waitpid(pc, NULL, WNOHANG); /* 使用了WNOHANG參數(shù),waitpid不會(huì)在這里等待 */ if(pr==0){ /* 如果沒有收集到子進(jìn)程 */ printf("No child exited\n"); sleep(1); } }while(pr==0); /* 沒有收集到子進(jìn)程,就回去繼續(xù)嘗試 */ if(pr==pc) printf("successfully get child %d\n", pr); else printf("some error occured\n"); } }
|