文山网站建设公司,查询网站相关网址,公司简介范文 共10篇,商城微信公众号开发使用有名管道实现#xff0c;一个进程用于给另一个进程发消息#xff0c;另一个进程收到消息后#xff0c;展示到终端上#xff0c;并且将消息保存到文件上一份
代码#xff1a;
/*******************************************/
文件名#xff1a;create.c
/********…使用有名管道实现一个进程用于给另一个进程发消息另一个进程收到消息后展示到终端上并且将消息保存到文件上一份
代码
/*******************************************/
文件名create.c
/*******************************************/
#include myhead.h
int main(int argc, char const *argv[])
{//创建有名管道文件if (mkfifo(./linux, 0664) -1){perror(mkfifo error);return -1;}getchar();system(rm linux);return 0;
}
/*******************************************/
文件名send.c
/*******************************************/
#include myhead.h
int main(int argc, char const *argv[])
{//以写的形式打开管道文件int wfd open(./linux, O_WRONLY);if (wfd -1){perror(open error);return -1;}printf(管道文件已经打开\n);//发送数据char wbuf[128] ;while (1){printf(请输入);fgets(wbuf, sizeof(wbuf), stdin);wbuf[strlen(wbuf) - 1] 0;//将数据发送给到管道中write(wfd, wbuf, strlen(wbuf));//判断数据if (strcmp(wbuf, quit) 0){break;}}//关闭文件描述符close(wfd);return 0;
}/*******************************************/
文件名get.c
/*******************************************/
#include myhead.h
int main(int argc, char const *argv[])
{//以读的形式打开文件int rfd open(./linux, O_RDONLY);if (rfd -1){perror(open error);return -1;}int cfd open(./h.txt, O_WRONLY);if (cfd -1){perror(open error);return -1;}printf(管道文件读端打开\n);//定义接受容器char rbuf[128] ;while (1){bzero(rbuf, sizeof(rbuf));//读取数据read(rfd, rbuf, sizeof(rbuf));if (strcmp(rbuf, quit) 0){break;}printf(收到消息为%s\n, rbuf);write(cfd,rbuf,strlen(rbuf));}//关闭文件描述符close(rfd);close(cfd);return 0;
}结果 使用有名管道实现两个进程间相互通信
代码
/*******************************************/
文件名create1.c
/*******************************************/
#include myhead.h
int main(int argc, char const *argv[])
{//创建有名管道文件if (mkfifo(./linux_first, 0664) -1){perror(mkfifo error);return -1;}getchar();system(rm linux_first);return 0;
}/*******************************************/
文件名create2.c
/*******************************************/
#include myhead.h
int main(int argc, char const *argv[])
{//创建第二个有名管道文件if (mkfifo(./linux_next, 0664) -1){perror(mkfifo error);return -1;}getchar();system(rm linux_next);return 0;
}/*******************************************/
文件名one.c
/*******************************************/
#include myhead.h
sem_t sem;
void *task1(void *arg)
{//只读的形式打开管道文件2int rfdo open(./linux_next, O_RDONLY);if (rfdo -1){perror(open error);return NULL;}//定义接收容器char rbufo[128] ;while (1){bzero(rbufo, sizeof(rbufo));//读取数据read(rfdo, rbufo, sizeof(rbufo));if (strcmp(rbufo, quit) 0){sem_post(sem);break;}printf(对方的消息%s\n, rbufo);sem_post(sem);}//关闭文件描述符close(rfdo);pthread_exit(NULL); //退出线程
}
void *task2(void *arg)
{//只写的形式打开管道文件1int wfdo open(./linux_first, O_WRONLY);if (wfdo -1){perror(open error);return NULL;}//发送数据char wbufo[128] ;while (1){sem_wait(sem);printf(对话);fgets(wbufo, sizeof(wbufo), stdin);wbufo[strlen(wbufo) - 1] 0;write(wfdo, wbufo, strlen(wbufo));if (strcmp(wbufo, quit) 0){break;}}//关闭文件描述符close(wfdo);
}
int main(int argc, char const *argv[])
{//初始化无名信号量sem_init(sem, 0, 1);//定义变量存储线程号pthread_t tido -1;if (pthread_create(tido, NULL, task1, NULL) ! 0){printf(pthread_create error\n);return -1;}//定义变量存储线程号pthread_t tido2 -1;if (pthread_create(tido2, NULL, task2, NULL) ! 0){printf(pthread_create error\n);return -1;}//阻塞回收线程的资源pthread_join(tido, NULL);pthread_join(tido2, NULL);//销毁无名信号量sem_destroy(sem);return 0;
}/*******************************************/
文件名another.c
/*******************************************/
#include myhead.h
sem_t sem;
void *task1(void *arg)
{//只写的形式打开管道文件2int wfda open(./linux_next, O_WRONLY);if (wfda -1){perror(open error);return NULL;}//发送数据char wbufa[128] ;while (1){sem_wait(sem);printf(对话);fgets(wbufa, sizeof(wbufa), stdin);wbufa[strlen(wbufa) - 1] 0;write(wfda, wbufa, strlen(wbufa));if (strcmp(wbufa, quit) 0){break;}}//关闭文件描述符close(wfda);pthread_exit(NULL); //退出线程
}
void *task2(void *arg)
{//只读的形式打开管道文件1int rfda open(./linux_first, O_RDONLY);if (rfda -1){perror(open error);return NULL;}//定义接收容器char rbufa[128] ;while (1){bzero(rbufa, sizeof(rbufa));//读取数据read(rfda, rbufa, sizeof(rbufa));if (strcmp(rbufa, quit) 0){sem_post(sem);break;}printf(对方的消息%s\n, rbufa);sem_post(sem);}//关闭文件描述符close(rfda);pthread_exit(NULL); //退出线程
}
int main(int argc, char const *argv[])
{//初始化无名信号量sem_init(sem, 0, 0);//定义变量存储线程号pthread_t tida -1;if (pthread_create(tida, NULL, task1, NULL) ! 0){printf(pthread_create error\n);return -1;}//定义变量存储线程号pthread_t tida2 -1;if (pthread_create(tida2, NULL, task2, NULL) ! 0){printf(pthread_create error\n);return -1;}pthread_join(tida, NULL);pthread_join(tida2, NULL);//销毁无名信号量sem_destroy(sem);return 0;
}结果 思维导图