• 多进程小例子--fork+pipe


     1 #include<stdio.h>
      2 #include<unistd.h>
      3
      4 #define m 6
      5 int main()
      6 {
      7         int pipefd[2];
      8         int pid;
      9         int m1;
     10
     11         if(pipe(pipefd)<0)
     12         {
     13                 printf("unable to create pipe! ");
     14                 return 1;
     15         }
     16         pid=fork();
     17         if(pid>0)    //parent
     18         {
     19                 m1=m;
     20                 close(pipefd[0]);  //close read end
     21                 write(pipefd[1],&m1,sizeof(int)); //read m1
     22                 wait(NULL);  // wait for child complete
     23                 close(pipefd[1]);
     24         }
     25         else if(pid==0)
     26         {
     27                 close(pipefd[1]);
     28                 read(pipefd[0],&m1,sizeof(int));  //read m1
     29                 while(m1>0)
     30                 {
     31                         printf("hello world! ");
     32                         m1--;
     33                 }
     34                 close(pipefd[0]);
     35         }
     36         else
     37         {
     38                 printf("unable to firk! ");
     39                 return 1;
     40         }
     41         return 0;
     42 }

  • 相关阅读:
    LeetCode周赛#209
    CodeBlocks相关配置
    LeetCode双周赛#36
    LCCUP 2020 秋季编程大赛 补题
    while和do while区别
    完全背包--piggy-bank
    数组定义在哪里???重要!!!
    01背包---点菜问题
    java源程序命名规则
    hdu1159-----最长公共子序列LCS
  • 原文地址:https://www.cnblogs.com/zhangyabin---acm/p/3175627.html
Copyright © 2020-2023  润新知