zoukankan      html  css  js  c++  java
  • 【资料】父子进程可以共享变量吗? 使用pipeline

    使用全局变量 ,静态变量都没有意义

    #include         <unistd.h>
    #include         <stdio.h>
    #include         <sys/types.h>
    #define   MAXLINE   100
    int
    main(void)
    {
            int   n,fd[2];
            pid_t   pid;
            char   line[MAXLINE];
            if   (pipe(fd) <0)
            {
                    printf( "pipe   error ");
                    exit(1);
            }
            if   ((pid   =   fork())   <   0)
            {
                    printf( "fork   error ");
                    exit(1);
            }
            else   if   (pid> 0)
            {
                    close   (fd[0]);
                    write   (fd[1], "how   are   you?\n ",12);
                    sleep(2);
            }
            else
            {
                    close   (fd[1]);
                    n=read(fd[0],line,MAXLINE);
                    write(STDOUT_FILENO,line,n);
            }
            exit(0);
    }
    我上面是说的是在多线程编程中的情况,在fork时这个管道的例子会对你有帮助。

  • 相关阅读:
    自己搭建二维码接口
    HTML CSS SPRITE 工具
    Codeforces Round #636 (Div. 3) 题解
    Codeforces Round #612 (Div. 1+Div. 2)
    计树问题小结 version 2.0
    Educational Codeforces Round 85 (Rated for Div. 2) 题解
    luogu6078 [CEOI2004]糖果
    luogu [JSOI2012]分零食
    多项式全家桶
    生成函数小结
  • 原文地址:https://www.cnblogs.com/titer1/p/2390609.html
Copyright © 2011-2022 走看看