- 論壇徽章:
- 0
|
- #include <stdio.h>;
- int main(void)
- {
- int pfd[2],pfd1[2];
- int pid;
- int pid1;
- char buf[2000];
- if (pipe(pfd) == -1)
- {
- perror("pipe failed");
- exit(1);
- }
- if ((pid = fork()) < 0)
- {
- perror("fork failed");
- exit(2);
- }
-
- if(pid == 0)
- {
- close(pfd[0]);
- dup2(pfd[1], 1);
- close(pfd[1]);
- execlp("ps", "ps","-x", (char *) 0);
- perror("ps failed");
- exit(4);
- }
- dup2(pfd[0],0);
- close(pfd[0]);
- read(0,buf,2000);
- if (pipe(pfd1) == -1)
- {
- perror("pipe failed");
- exit(1);
- }
- if ((pid1 = fork()) < 0)
- {
- perror("fork failed");
- exit(2);
- }
-
- if(pid1>;0)
- {
- close(pfd1[0]);
- write(pfd1[1],buf,2000);
- }
- if (pid1 == 0)
- {
- close(pfd1[1]);
- dup2(pfd1[0], 0);
- close(pfd1[0]);
- // execlp("grep", "grep","smb", (char *) 0);
- read(0,buf,2000);
- write(1,buf,2000);
- exit(3);
- }
- exit(0);
- }
復(fù)制代碼
在上述代碼中,
在pid2進(jìn)程可讀到pid的并可以輸出了,但為什么執(zhí)行
execlp("grep", "grep","smb", (char *) 0);
則,沒(méi)有任何結(jié)果輸出?
請(qǐng)各大蝦指點(diǎn)!
在終端執(zhí)行: ps -x |grep smb
2229 ? S 0:00 smbd -D
8907 ? S 0:01 smbd -D
13499 pts/1 S 0:00 grep smb |
|