机器人107 2018-11-20 05:22 采纳率: 0%
浏览 570

利用映射的关系实现父子进程间的通信,子进程可以映射,为什么父进程映射失败了?

#include
#include
#include
#include
#include
#include
#include

int main()
{
char buf[5]={'0'};
int fd=open("hello.c",O_RDWR|O_CREAT,0666);
if(fd<0)
{
printf("open is erroe\r\n");
return -1;

}
write(fd,buf,5);


pid_t pid = fork();//建立子进程
if(pid < 0)
{

    printf("fork is error\r\n");
    return -1;
}else if(pid ==0)//child
{
    sleep(1);
    char * p = mmap(NULL,5, PROT_READ| PROT_WRITE , MAP_SHARED,fd,0);
    if(p == (char *)-1)
    {
    printf(" child mmap is error\r\n");
    return -1;
    }


    printf("This is child \r\n");
    puts(p);    
   //取消映射
    munmap(p,5);

}else{
char * p = mmap(NULL,5,PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED,fd,0);    
if(p == (char *)-1);
{
    printf(" father mmap is error\r\n");
    return -1;
}
 mencpy(p,"hello",5);

}

return 0;

}

  • 写回答

1条回答 默认 最新

  • 机器人107 2018-11-20 05:24
    关注

    被吃掉的头文件........
    #include
    #include
    #include
    #include
    #include
    #include
    #include

    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏