//父进程 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);
Linux下进程控制编程(父进程写入数据,子进程读出数据)
最新推荐文章于 2024-05-18 20:48:47 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)