管道实例

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h>

#include <sys/types.h>

#include <sys/wait.h>

int main(void)

{

  int nCount=0,i,flag=1;

  int pfd[2];

  char szBuffi[1] = {0};

  char szBuffo[1] = {0};

  int  status;

  pid_t pid;

 

  pipe(pfd);

 

   if(fork() == 0){

     nCount = 1;

     sprintf(szBuffi, "%d", nCount);

     printf("szBuffi=%s\n",szBuffi);

     write(pfd[1], szBuffi, strlen(szBuffi));

     close(pfd[0]);

     close(pfd[1]);

     exit(0);

  }else{

      while(flag){

        read(pfd[0], szBuffo, 1);

           if(szBuffo[0]=='1'){

               printf("szBuffo=%s\n",szBuffo);

               pid = wait(&status);

               i = WEXITSTATUS(status);

               printf("child is %d ,exitstatus =%d\n", pid, i);

               flag=0; 

           }

     }

   }

  close(pfd[0]);

  close(pfd[1]);

  pipe(pfd);

  close(pfd[0]);

  close(pfd[1]);

  return 0;

}

你可能感兴趣的:(include)