qq_33555452 2017-01-02 14:19 采纳率: 100%
浏览 990

为什么父进程中 把sleep放到35行就不执行子进程的逻辑了呢 放到32行就很正常

 #include <stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc , char **argv)
{
    int fd[2] , n ;
    char c ; 
    pid_t childpid ; 
    pipe(fd);
#if 1
    if((childpid = fork()) == 0)
    {
        if((n = read(fd[0],&c ,1))!= 1)
            perror("no data\n");
        printf("child %c %d %d \n",c ,fd[0],fd[1]);
        write(fd[1],"c",1);
        exit(0);
    }
#endif
    write(fd[1],"p",1);//31
    if((n = read(fd[0],&c ,1))!= 1)
        perror("no data\n");
    printf("parent %c %d  %d \n",c,fd[0],fd[1]);
    sleep(6);//35
    exit(0);

}
  • 写回答

1条回答

  • threenewbee 2017-01-02 15:13
    关注

    sleep会阻塞住当前线程,而管道是异步获取的,你单步调试下

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题