文章目录
- 使用有名管道实现,一个进程用于给另一个进程发消息,另一个进程收到消息后,展示到终端上,并且将消息保存到文件上一份
- 使用有名管道实现两个进程间相互通信
使用有名管道实现,一个进程用于给另一个进程发消息,另一个进程收到消息后,展示到终端上,并且将消息保存到文件上一份
#include<myhead.h>
//子进程获取数据并写入
void getandwrite()
{
//打开zy1文件
int getfd = open("./zy1",O_RDONLY);
if(getfd == -1)
{
perror("getfd error");
return ;
}
//打开zy1.txt文件要是没有就创建,有就追加
int writefd = open("./zy1.txt",O_WRONLY|O_CREAT|O_APPEND,0664);
if(writefd ==-1)
{
perror("writefd error");
return ;
}
while(1)
{
sleep(1);
char getbuf[128] = "";//存储读取的数据
//初始化getbuf
bzero(getbuf,sizeof(getbuf));
read(getfd,getbuf,sizeof(getbuf));
if(strcmp(getbuf,"quit")==0)
{
break;
}
printf("收到消息为:%s\n",getbuf);
//将getbuf中的数据写入zy1.txt文件
write(writefd,getbuf,strlen(getbuf));
write(writefd,"\n",1);
}
close(getfd);
exit(EXIT_SUCCESS);
}
//父进程发送数据
void set()
{
int setfd = open("./zy1",O_WRONLY);
if(setfd == -1)
{
perror("setfd error");
return ;
}
while(1)
{
sleep(1);
printf("请输入>>>>");
char setbuf[128] = "";//存储输入的数据
//初始化setbuf
bzero(setbuf,sizeof(setbuf));
fgets(setbuf,sizeof(setbuf),stdin);
//去除换行符
setbuf[strlen(setbuf)-1] = 0;
//将setbuf中的数据写入zy1文件
write(setfd,setbuf,strlen(setbuf));
if(strcmp(setbuf,"quit")==0)
{
break;
}
}
close(setfd);
}
/*********************主程序********************/
int main(int argc, char const *argv[])
{
if (mkfifo("./zy1",0644) == -1)
{
perror("mkfifo error");
return -1;
}
pid_t pid = fork();
if(pid ==0)
{
getandwrite();//子进程获取数据并写入
printf("子进程\n");
}
else if(pid >0)
{
set();//父进程发送数据
printf("父进程\n");
}
else
{
printf("fork error\n");
}
getchar();
system("rm ./zy1");//删除生成的管道文件
return 0;
}
使用有名管道实现两个进程间相互通信
cerate.c
#include<myhead.h>
int main(int argc, const char *argv[])
{
if (mkfifo("./1",0664) ==-1)
{
perror("mkfifo error");
return -1;
}
if (mkfifo("./2",0664) ==-1)
{
perror("mkfifo error");
return -1;
}
getchar();
system("rm ./1");
system("rm ./2");
return 0;
}
1.c
#include <myhead.h>
void set() //发送给2的进程函数
{
int fd = open("./1", O_WRONLY);
if (fd == -1)
{
perror("open error");
return;
}
char buf[128] = "";
while (1)
{
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
printf("上面你要发给2的信息:\n");
buf[strlen(buf) - 1] = 0;
write(fd, buf, strlen(buf));
if (strcmp(buf, "quit") == 0)
{
break;
}
}
close(fd);
}
void get() //接收2的进程函数
{
int fd = open("./2", O_RDONLY);
if (fd == -1)
{
perror("open error");
return;
}
char buf[128] = "";
while (1)
{
bzero(buf, sizeof(buf));
read(fd, buf, sizeof(buf));
if (strcmp(buf, "quit") == 0)
{
break;
}
printf("从2接收到的信息:%s\n", buf);
}
close(fd);
}
int main(int argc, char const *argv[])
{
pid_t pid = fork();
if (pid > 0)
{
set();
}
else if (pid == 0)
{
get();
exit(EXIT_SUCCESS);
}
else
{
printf("error\n");
return -1;
}
wait(NULL);
return 0;
}
2.c
#include <myhead.h>
void set() //发送给1的进程函数
{
int fd = open("./2", O_WRONLY);
if (fd == -1)
{
perror("open error");
return;
}
char buf[128] = "";
while (1)
{
bzero(buf, sizeof(buf));
fgets(buf, sizeof(buf), stdin);
printf("上面你要发给1的信息:\n");
buf[strlen(buf) - 1] = 0;
write(fd, buf, strlen(buf));
if (strcmp(buf, "quit") == 0)
{
break;
}
}
close(fd);
}
void get() //接收1的进程函数
{
int fd = open("./1", O_RDONLY);
if (fd == -1)
{
perror("open error");
return;
}
char buf[128] = "";
while (1)
{
bzero(buf, sizeof(buf));
read(fd, buf, sizeof(buf));
if (strcmp(buf, "quit") == 0)
{
break;
}
printf("从1接收到的信息:%s\n", buf);
}
close(fd);
}
int main(int argc, char const *argv[])
{
pid_t pid = fork();
if (pid > 0)
{
set();
}
else if (pid == 0)
{
get();
exit(EXIT_SUCCESS);
}
else
{
printf("error\n");
return -1;
}
wait(NULL);
return 0;
}