• 管道学习


       1:  /*
       2:  @@
       3:      Author:    JustinZhang
       4:      Email:    uestczhangchao@gmail.com
       5:      Time:    2012-9-1 23:32:22
       6:      desc:    Example from man, write by hand, test under ubuntu gcc.
       7:              For the purpose of reviewing pipe.
       8:  @@
       9:  */
      10:   
      11:  #include <sys/wait.h>
      12:  #include <stdio.h>
      13:  #include <stdlib.h>
      14:  #include <unistd.h>
      15:  #include <string.h>
      16:  int main(int argc, char *argv[])
      17:  {
      18:      int pipefd[2];
      19:      pid_t cpid;
      20:      char buf;
      21:      if(argc !=2)
      22:      {
      23:          fprintf(stderr, "Usage:%s <string>\n",argv[0]);
      24:          exit(EXIT_FAILURE);
      25:      }
      26:      if(pipe(pipefd)==-1)
      27:      {
      28:          perror("pipe");
      29:          exit(EXIT_FAILURE);
      30:      }
      31:      cpid = fork();
      32:      if(cpid == -1)
      33:      {
      34:          perror("fork");
      35:          exit(EXIT_FAILURE);
      36:      }
      37:      /*return from child*/
      38:      if(cpid==0)
      39:      {
      40:          /*close the write side of pipe*/
      41:          close(pipefd[1]);
      42:          while(read(pipefd[0], &buf, 1)>0)
      43:          {
      44:              write(STDOUT_FILENO, &buf, 1);
      45:          }
      46:          write(STDOUT_FILENO, "\n", 1);
      47:          close(pipefd[0]);
      48:          _exit(EXIT_SUCCESS);
      49:      }
      50:      else /*return from parent, write arg[1] to pipe*/
      51:      {
      52:          close(pipefd[0]);
      53:          write(pipefd[1],argv[1],strlen(argv[1]));
      54:          close(pipefd[1]);
      55:          wait(NULL);
      56:          exit(EXIT_SUCCESS);
      57:      }
      58:      return 0;
      59:  }
  • 相关阅读:
    工作中碰到的一些问题以及解决方法
    OPEN(SAP) UI5 学习入门系列之二: 最佳实践练习(上)
    OPEN(SAP) UI5 学习入门系列之一:扫盲与热身(下)
    OPEN(SAP) UI5 学习入门系列之一:扫盲与热身(上)
    SAP Fiori和UI5的初学者导航
    ios开发证书,描述文件,bundle ID的关系
    微信小程序实例教程(四)
    微信小程序实例教程(三)
    微信小程序实例教程(二)
    微信小程序实例教程(一)
  • 原文地址:https://www.cnblogs.com/justinzhang/p/2667204.html
Copyright © 2020-2023  润新知