- 论坛徽章:
- 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);
- }
复制代码
在上述代码中,
在pid2进程可读到pid的并可以输出了,但为什么执行
execlp("grep", "grep","smb", (char *) 0);
则,没有任何结果输出?
请各大虾指点!
在终端执行: ps -x |grep smb
2229 ? S 0:00 smbd -D
8907 ? S 0:01 smbd -D
13499 pts/1 S 0:00 grep smb |
|