2月7日 进程间通信

代码示例展示了如何创建fifo管道实现两个进程(A和B)之间的通信,进行交替打印对话,直到接收到quit为止。进程A打开fifo1进行读取,打开fifo2进行写入;进程B则相反。在第二个练习中,通过引入线程,使得AB进程可以同时进行读写操作,提高了交互的实时性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 实现A,B进程对话。

A进程传递信息给B进程,B进程打印

B进程传递信息给B进程,A进程打印

直到收到quit后,结束A,B进程

#include<stdio.h>

#include<unistd.h>

#include<sys/stat.h>

#include<sys/types.h>

#include<errno.h>

#include<fcntl.h>

#include<string.h>

int main(int argc, const char *argv[])

{

charbuf1[32];

charbuf2[32];

intret;

if(mkfifo("./fifo1",0777)<0){

if(errno!=17){

perror("mkfifo2error");

return-1;

}

}

if(mkfifo("./fifo2",0777)<0){

if(errno!=17){

perror("mkfifo2error");

}

}

printf("makefifo succeed\n");

intfd_r=open("./fifo1",O_RDONLY);

if(fd_r<0){

perror("openerror 1");

return-1;

}

intfd_w=open("./fifo2",O_WRONLY);

if(fd_w<0){

perror("openerror 2");

return-1;

}

while(1){

ret=read(fd_r,buf2,sizeof(buf2));

if(ret==0){

break;

}

printf("%s\n",buf2);

fgets(buf1,sizeof(buf1),stdin);

buf1[strlen(buf1)-1]='\0';

if(strcmp(buf1,"quit")==0){

break;

}

write(fd_w,buf1,sizeof(buf1));

}

close(fd_r);

close(fd_w);

return0;

}

#include<stdio.h>

#include<unistd.h>

#include<sys/stat.h>

#include<sys/types.h>

#include<errno.h>

#include<fcntl.h>

#include<string.h>

int main(int argc, const char *argv[])

{

charbuf1[32];

charbuf2[32];

int ret;

intfd_w=open("./fifo1",O_WRONLY);

if(fd_w<0){

perror("openerror");

return-1;

}

intfd_r=open("./fifo2",O_RDONLY);

if(fd_r<0){

perror("open2 error");

return-1;

}

while(1){

fgets(buf1,sizeof(buf1),stdin);

buf1[strlen(buf1)-1]='\0';

if(strcmp(buf1,"quit")==0){

break;

}

write(fd_w,buf1,sizeof(buf1));

ret=read(fd_r,buf2,sizeof(buf2));

if(ret==0){

break;

}

printf("%s\n",buf2);

}

close(fd_r);

close(fd_w);

return0;

}

2.在上述练习基础上实现AB进程对话,要求AB进程能够随时收发。

#include<stdio.h>

#include<unistd.h>

#include<sys/stat.h>

#include<sys/types.h>

#include<errno.h>

#include<fcntl.h>

#include<string.h>

#include<pthread.h>

void* callread(void* arg)

{

intret=0;

charbuf2[32]="";

intfd_r=*((int*)arg);

while(1){

ret=read(fd_r,buf2,sizeof(buf2));

if(ret==0){

_exit(5);

}

printf("%s\n",buf2);

}

}

void* callwrite(void* arg)

{

charbuf1[32]="";

intfd_w=*((int*)arg);

while(1){

fgets(buf1,sizeof(buf1),stdin);

buf1[strlen(buf1)-1]='\0';

if(strcmp(buf1,"quit")==0){

_exit(5);

}

write(fd_w,buf1,sizeof(buf1));

}

}

int main(int argc, const char *argv[])

{

if(mkfifo("./fifo1",0777)<0){

if(errno!=17){

perror("mkfifo2error");

return-1;

}

}

if(mkfifo("./fifo2",0777)<0){

if(errno!=17){

perror("mkfifo2error");

}

}

printf("makefifo succeed\n");

intfd_r=open("./fifo1",O_RDONLY);

if(fd_r<0){

perror("openerror 1");

return-1;

}

intfd_w=open("./fifo2",O_WRONLY);

if(fd_w<0){

perror("openerror 2");

return-1;

}

pthread_ttid1,tid2;

if(pthread_create(&tid1,NULL,callwrite,(void*)&fd_w)){

fprintf(stderr,"pthread_create");

return-1;

}

if(pthread_create(&tid2,NULL,callread,(void*)&fd_r)){

fprintf(stderr,"pthread_create");

return-1;

}

/*

while(1){

}

*/

pthread_join(tid1,NULL);

pthread_join(tid2,NULL);

close(fd_r);

close(fd_w);

return0;

}

#include<stdio.h>

#include<unistd.h>

#include<sys/stat.h>

#include<sys/types.h>

#include<errno.h>

#include<fcntl.h>

#include<string.h>

#include<pthread.h>

void* callread(void* arg){

intfd_r=*((int*)arg);

int ret=0;

char buf2[32]="";

while(1){

ret=read(fd_r,buf2,sizeof(buf2));

if(ret==0){

_exit(5);

}

printf("%s\n",buf2);

}

}

void* callwrite(void* arg){

intfd_w=*((int*)arg);

charbuf1[32]="";

while(1){

fgets(buf1,sizeof(buf1),stdin);

buf1[strlen(buf1)-1]='\0';

if(strcmp(buf1,"quit")==0){

_exit(5);

}

write(fd_w,buf1,sizeof(buf1));

}

}

int main(int argc, const char *argv[])

{

intfd_w=open("./fifo1",O_WRONLY);

if(fd_w<0){

perror("openerror");

return-1;

}

intfd_r=open("./fifo2",O_RDONLY);

if(fd_r<0){

perror("open2 error");

return-1;

}

pthread_ttid1,tid2;

if(pthread_create(&tid1,NULL,callread,(void*)&fd_r)){

fprintf(stderr,"pthread_create");

return-1;

}

if(pthread_create(&tid2,NULL,callwrite,(void*)&fd_w)){

fprintf(stderr,"pthread_create");

return-1;

}

/*

while(1){

*/

pthread_join(tid1,NULL);

pthread_join(tid2,NULL);

close(fd_r);

close(fd_w);

return0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值