【发布时间】:2016-02-09 04:58:00
【问题描述】:
我是 linux 新手,我尝试制作服务器(读取器)和客户端(写入器);
因此客户端可以使用命名管道向服务器发送“Hi”。
我已经编写了这两个程序。当我在 Makefile 中构建它们时,如何使它们与命名管道通信?
//server programm:
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include "fun.h"
#define MAX_BUF 1024
int main()
{
int pid,fd,status;
char * myfifo = "/home/pipe";
char buf[MAX_BUF];
pid=fork();
wait(&status);
if (pid<0){
exit(1);
}
if (pid==0){
mkfifo(myfifo, 0666);
fd = open(myfifo, O_RDONLY);
main1();
read(fd, buf, MAX_BUF);
printf("%s\n", buf);
}
else{
printf("i am the father and i wait my child\n");
}
close(fd);
return 0;
}
//client program:
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "fun.h"
int main1()
{
int fd;
char * myfifo = "/home/pipe";
fd = open(myfifo, O_WRONLY);
write(fd, "Hi", sizeof("Hi"));
close(fd);
unlink(myfifo);
return 0;
}
//fun.h:
int main1()
//Makefile:
all: client.o server.o
gcc client.o server.o -o all
client.o: client.c
gcc -c client.c
server.o: server.c
gcc -c server.c
clean:
rm server.o client.o
以上是我目前写的代码。这是其他问题教程和视频中的简单代码。
【问题讨论】:
-
首先让您的客户端/服务器在 cmd 行上工作。然后制作一个shell脚本来管理进程,即创建命名管道,在后台运行服务器,运行客户端并通过命名管道发送数据,然后关闭/清理。然后,您可以寻找如何将对 shell 脚本的调用嵌入到 makefile 中。祝你好运。
-
谢谢!!我相信这就是我要找的!
标签: c linux makefile named-pipes