栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Mit6.S081 pingpong 实现管道

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mit6.S081 pingpong 实现管道

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"


int main (int argc,char* argv[]){
    int pid,pid_s,pid_f,r,w;
    char r_buf[512],w_buf[512];
    int pfd[2];
    
    strcpy(w_buf,"pingpong");
    pipe(pfd);
    pid = fork();
    if(pid ==0){
        pid_s = getpid();
        close(pfd[1]);
        r = read(pfd[0],r_buf,sizeof(r_buf));
        printf("This is son %d read %d bytes , %sn",pid_s,r,r_buf);
        close(pfd[0]);
        exit(0);
    }//子进程
    else if (pid > 0) {
        pid_f = getpid();
        close(pfd[0]);
        w = write(pfd[1],w_buf,sizeof(w_buf));
        printf("This is father %d write %d bytes n",pid_f,w);
        close(pfd[1]);
        exit(0);
    }//父进程
    else {
        printf("fork errorn");
        exit(1);
    }//创建进程失败
}

 

 看打印似乎是成功了的,但是在父进程和子进程中加的打印出现乱码了,原因应该是子进程和父进程 互相抢占

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/320096.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号