打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【Linux下C】Linux下多进程轮流写文件


文章目录

需求

程序中包括两个进程,P1和P2。它们分别向一个文件,轮流写入字符串。
例如:P1写入字符串“XXXXXX”,P2写入字符串“YYYYYY”。
关键词功能是轮流写入,P1写完后必须P2写,P2写完后必须P1写。

实现思路

我们利用fork函数可以创建一个子进程,假设我们先让子进程写文件,此时父进程不得写文件,怎么实现呢?
linux中的wait(函数)可以实现父进程等待子进程的死亡再执行。
根据这个思路可以写出伪代码:

while(true){   创建子进程;if(子进程){   写"I am child."到文件}if(父进程){   wait()   写文件}}

循环开始时父进程陷入等待,子进程写完以后父进程才能写文件,然后下一次循环子进程再次写文件。

演示动图

源码

#include <iostream>#include <string>#include <cstring>#include <cstdlib>#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/wait.h>#include <netdb.h>#include <arpa/inet.h>#include <fcntl.h>#include <dirent.h>#include <signal.h>using namespace std;void UI();char despath[200];int main(){UI();    pid_t pid;int cnt2=0;while(1){pid=fork();if(pid<0){cout<<"error"<<endl;}else if(pid==0){FILE* ch_fp=fopen(despath,"a");char buf[200];fprintf(ch_fp,"%s","child\n");            printf("子进程向%s写入了child\n",despath);sleep(1);fclose(ch_fp);exit(0);}else{wait(NULL);FILE* pa_fp=fopen(despath,"a");char buf[200];fprintf(pa_fp,"%s","parent\n");printf("父进程向%s写入了parent\n",despath);            fclose(pa_fp);sleep(1);}}    return 0;}void UI(){ printf("----------本程序实现了基于fork的多进程轮流写文件----------\n"); printf("      父子进程将轮流写入\"parent\"和\"child\"             \n");         printf("      您可以自定义目标文件的路径                          \n"); printf("      请给出目标文件的路径:                               \n"); scanf("%s",despath);}

使用g 编译运行即可。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
linux根据pid获取进程名和获取进程pid(c语言获取pid)
linux多进程之间的文件锁
linux操作系统下c语言编程入门
Linux的进程通讯之命名管道
Linux的进程间通信-管道
学习linux C编程实战-----linux进程控制
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服