Linux下进程控制编程(父进程写入数据,子进程读出数据)

本文详细介绍了在Linux环境下如何通过进程控制进行数据通信。主要内容包括创建父进程和子进程,以及如何实现父进程写入数据到共享资源,子进程从同一资源中读取并处理数据的过程。通过实例代码展示了具体的实现步骤和技术要点。
摘要由CSDN通过智能技术生成
//父进程   write   子进程   read
//需要用到的函数  open,write,memset(清空缓存buf),read,fork(创建子进程),sleep,
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

void Write()//父进程,写数据
{
        char buf[32] = "hello world!";
        int fd = open("hello.txt",O_CREAT|O_EXCL|O_RDWR,S_IRWXU);
        if(-1 == fd)
        {
                perror("Write open");
                exit(1);
        }

        int ret = write(fd,buf,strlen(buf));
        if(-1 == ret)
        {
                perror("write");
                exit(1);
        }

        close(fd);
}

void Read()//子进程,读数据
{
        char buf[32] = {0};
        int fd = open("hello.txt", O_RDONLY);
        if(-1 == fd)
        {
                perror("Read open");
                exit(1);
    
  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值