发布时间:2014-10-22 19:00:18编辑 分享查询网我要评论

after the introdution about the fork() function ,  i do think that you have find that the result these two process print is so messy , how to make it neat , let do it now. in  <<begin linux programming>> there are three functions which can join the process you fork earlier.but here we mainly talk about two of them which we use frequently . #include <sys/types.h> #inlcude <sys/wait.h> pid_t wait(int * stat_loc); pid_t waitpid(pid_t pid,int * stat_loc,int options); the wait system call causes a parent process to pause until one of its child processes is stopped, the call returns the pid of the child process, and as for the parameter stat_loc , if stat_loc is not a null pointer , the exit status information will be written to the location to which it points. one thing you should know about the stat_loc parameter is that the exit information can't be read correctly if you just print them with ("%d",stat_loc);you will meet some error if you do so, and so am i. one way you can get the correct information about the exitcode is make use of the macro(宏) as follows which defined in sys/wait.h      WIFEXITED(stat_val)-----Nonzero if the child is terminated normally WEXITSTATUS(stat_val)-----If WIFEXITED is nonzero,this returns child exit code WIFSGNALED(stat_val)-----Nonzero if the child is terminated on an uncaught signal WTERMSIG(stat_val)-----If WIFSIGNALED is nonzero , this returns a signal number now let come to an example about how to use the wait() funtion: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> /* *test wait function() *author @ suilingxi *2014-2-10 */ void dis(int n); int main(){ pid_t new_pid; new_pid=fork(); int stat_loc; int n=0; switch(new_pid){ case -1:{ perror("fork error\n"); exit(0); } case 0:{ n=10; dis(n); break; } default:{ n=10; dis(n); } } /* * as we all know , the parent proces and the child process share the code segment but with their own variables * so you must make it sure it is only the parent can execute this part by using new_pid != 0 to clarify these * two process */ if(new_pid != 0 ){ int return_pid; return_pid=wait(&stat_loc); printf("the return child_pid is %d\n",return_pid); if(WEXITSTATUS(stat_loc)==3){ printf("the exit code is %d\n",WEXITSTATUS(stat_loc)); }else{ printf("exit code error and the exit code is %d\n",stat_loc); } return 0; } /* * only the child process can execute this code for the parent process will return 0 the time * he enter into the above part, i really wish that you can remeber one sentence the parent process * and the child process share the code but with their own data space , environment and file descreptors. */ exit(3); } void dis(int n){ for(int i=0;i<n;i++){ printf("%d--%d\n",getpid(),i); sleep(1); } } the waitpid() system call is similar to the wait system call in some way but with more facilites , as you can see the prototype above , the pid argument specifies the PID of a  particular child process to wait for, if it is -1 , waitpid will return information for any child process , just like wait, the same to wait , the argument stat_loc is still used to stort the return information , the third paramter worth you attention , which can allows you to modify the behaviror of waitpid. here show you an example how to make use of the waitpid() function , the explaination will beside the code: #include <stdio.h> #include <stdlib.h> #include <sys/wait.h> #include <sys/types.h> #include <unistd.h> void dis(int n); int main(){ pid_t new_pid; int stat_loc; int n; new_pid=fork(); switch(new_pid){ case -1:{ printf("fork error\n"); exit(0); } case 0:{ n=10; dis(n); break; } default:{ n=3; dis(n); break; } } /*the reason is the same as above*/ if(new_pid != 0){ pid_t return_pid; /*the waitpid function is not a periodical function , so you should use it with while so that you can check the status of the child process periodically the "WNOHANG" argument plays an important role in checking the child process periodically. */ while((return_pid=waitpid(new_pid,&stat_loc,WNOHANG)) != new_pid){ if(return_pid==-1){ perror("waitpid error\n"); exit(0); }else if(return_pid==0){ printf("child process %d is still running\n",new_pid); sleep(1);// use it for it would print the message so fast if not } } printf("child process %d has terminated and exitcode is %d\n",return_pid,WEXITSTATUS(stat_loc)); return 0; } exit(3); } void dis(int n){ for(int i=0;i<n;i++){ printf("%d---%d\n",getpid(),i); sleep(1); } }

上一篇:java 泛型


关键词: process