温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

进程通信 (命名管道 单向通信)

发布时间:2020-07-13 04:06:45 来源:网络 阅读:333 作者:秋笙夏笛 栏目:编程语言
 client.c:
 1 #include<stdio.h>
  2#include<stdlib.h>
  3#include<string.h>
  4#include<sys/stat.h>
  5#include<sys/types.h>
  6#include<unistd.h>
  7#include<fcntl.h>
  8#define _PATH_ "./tmp"
  9#define SIZE 100
 10int main()
 11 {
 12 if(mkfifo(_PATH_,0666|S_IFIFO)<0)
 13   {
 14    perror("mkfifo");
 15    return -1;
 16   }
 17   intfd=open(_PATH_,O_WRONLY);
 18  if(fd<0)
 19   {
 20   perror("open");
 21   return -1;
 22   }
 23  char buf[SIZE];
 24  memset(buf,'\0',SIZE);
 25  while(1)
 26  {
 27   scanf("%s",buf);
 28   write(fd,buf,strlen(buf));
 29  }
 30 close(fd);
 31 return 0;
 32 }
 
 
 server.c:
 
 1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 #include<sys/stat.h>
5 #include<sys/types.h>
6 #include<unistd.h>
  7#include<fcntl.h>
  8#define _PATH_ "./tmp"
  9#define SIZE 100
 10int main()
 11 {
 12   intfd=open(_PATH_,O_RDONLY);
 13  if(fd<0)
 14   {
 15   perror("open");
 16   return -1;
 17   }
 18  char buf[SIZE];
 19  while(1)
 20  {
 21
 22  memset(buf,'\0',SIZE);
 23   read(fd,buf,sizeof(buf));
 24   printf("%s\n",buf);
 25  }
 26 close(fd);
 27 return 0;
 28 }
 
 
 
 Makefile:
 
  1.PHONY:all
  2 all:client server
  3 server:server.c
  4         gcc -o $@ $^
  5client:client.c
  6         gcc -o $@ $^
  7.PHONY:clean
  8clean:
  9         rm -f client server tmp

进程通信 (命名管道  单向通信)

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI