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仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘