嵌入式4-2

今日作业:使用文件IO 实现父进程向子进程发送信息,并总结中间可能出现的各种问题

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, const char *argv\[\])
{
pid_t res=fork();
if(res>0)
{
int fd1=open("./test.c",O_CREAT|O_WRONLY|O_TRUNC,0664);
if(fd1==-1)
{
perror("open1");
return -1;
}
char str50={0};
puts("输入字符");
gets(str);
ssize_t w=write(fd1,str,50);
if(w==-1)
{
perror("write");
return -1;
}
close(fd1);
}else if(res==0)
{
sleep(3);
int fd2=open("./test.c",O_RDONLY);
if(fd2==-1)
{
perror("open2");
return -1;
}
char str150={0};
ssize_t r=read(fd2,str1,50);
if(r==-1)
{
perror("read");
return -1;
}
puts(str1);
close(fd2);
}else if(res==-1)
{
perror("fork");
return -1;
}
return 0;
}

相关推荐
CV-Climber2 小时前
检索技术的实际应用
人工智能·算法
hhzz3 小时前
Tiger AI Platform平台中增加人脸识别功能
图像处理·人工智能·算法·计算机视觉·大模型
从零开始的代码生活_3 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
TsingtaoAI4 小时前
3D高斯泼溅技术发展及其在具身智能领域的应用综述
人工智能·算法·ai·具身智能·高斯泼溅
atunet4 小时前
关于图算法中的连通分量检测与最小割问题7
算法
心运软件4 小时前
银行客户流失预测(Python 完整实战)
算法
邪神与厨二病5 小时前
牛客周赛 Round 153
python·算法
qizayaoshuap6 小时前
# ❌ 井字棋 — 鸿蒙ArkTS Minimax AI算法与博弈系统设计
人工智能·算法·华为·harmonyos
皓月斯语7 小时前
B3842 [GESP202306 三级] 春游 题解
数据结构·c++·算法·题解
atunet7 小时前
树状结构在查询优化中的作用与实现细节7
算法