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 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制