proc -x cshell fork()

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 #include <unistd.h>
 4 #include <string.h>
 5 #define MAXLINE 10
 6 int main(void)
 7 {
 8 char buf[MAXLINE];
 9 pid_t pid;
10 int status;
11 printf("%%");
12 while(fgets(buf,MAXLINE,stdin)!=NULL){
13     if(buf[strlen(buf)-1] == '\n')
14         buf[strlen(buf)-1] = 0;
15 if (( pid = fork()) < 0 )
16     printf("fork error\n");
17 else if (pid == 0){
18     execlp(buf,buf,(char *)0);
19     printf("could not execute : %s\n",buf);
20     exit(127);
21     }
22 if((pid = waitpid(pid,&status,0))<0)
23     printf("waitpid error\n");
24 }
25 }

 

posted @ 2015-05-16 16:17  pencil_zh  阅读(189)  评论(0编辑  收藏  举报