- 论坛徽章:
- 0
|
- #include<unistd.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- int main(void)
- {
- int pipefd;
- int pipe_fd[2];
- int length;
- pid_t pid;
- //char buf[100];
- char kin[100],kout[100];
- if(pipe(pipe_fd)!=0)
- {
- printf("Error in pipe!");
- exit(0);
- }
- if((pid=fork())==-1)
- {
- printf("Error in fork!\n");
- exit(0);
- }
- if(pid>0)
- {
- close(pipe_fd[0]);
- printf("This is the father process,please input characters!\n");
- gets(kin);
- // puts(kin);
- if(write(pipe_fd[1],kin,100)!=-1)
- {
- length=strlen(kin);
- printf("%d characters were write into the subprocess!\n",length);
- // printf("%s were written to subprocess!\n",puts(kin));
- }
- }
- if(pid==0)
- {
- close(pipe_fd[1]);
- sleep(15);
- if(read(pipe_fd[0],kout,100)==-1)
- {
- length=strlen(kout);
- printf("%d characters written to subprocess!\n",length);
- // printf("%s were written to subprocess!\n",puts(kout));
- }
- }
- }
复制代码 |
|