操作系统上机题目(多进程1)

  • 1、创建1个子进程
  • 2、程通过管道与子进程连接
    • 子进程的标准输出连接到管道的写端
    • 主进程的标准输入连接到管道的读端
  • 3、进程中调用exec(“echo”, “echo”, “hello world”, NULL)
  • 4、进程中调用read(0, buf, sizeof(buf)),从标准输入中获取子进程发送的字符串,并打印出来

code:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
int main(){
    pid_t pid;
    int fd[2];
    char buf[30];

    int count;
    int i=0;

    pipe(fd);

    pid=fork();
    if(pid<0){
        perror("fork():");
    }
    if(pid==0){
        dup2(fd[1],1);
        execlp("echo","echo","hello world",NULL);
    }
    else{
        dup2(fd[0],0);
        count=read(0,buf,30);
        write(1,buf,count);
    }
    close(fd[0]);
    close(fd[1]);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值