【发布时间】:2016-04-29 03:21:04
【问题描述】:
我在这个任务中度过了最艰难的时期。所以这个任务我有两个孩子(两个独立的程序),他们必须写信给父母(主要)。父母必须从孩子那里读取这两个数据,然后将其打印出来。我必须使用命名管道。好吧,所以我的 FIFO 一直给我“USAGE:NAMEPIPECLIENT [String]”消息,我不知道为什么。顺便说一下,消息在客户端。另外,如果有人可以为我指出如何在单独的文件上使用带有多个子项的 fork 的好方向,那将不胜感激。 提前致谢!使用 GNU C
我的读者
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<linux/stat.h>
#define FIFO_FILE "MYFIFO" //default is current directory
int main(void){
FILE *fpoint;
char readbuffer[80];
int again = 1;
mknod(FIFO_FILE, S_IFIFO | 0666, 0);
while(again){
fpoint = fopen(FIFO_FILE, "r");
fgets(readbuffer, 80, fpoint);
printf("recevived string: %s\n, readbuffer");
fclose(fpoint);
if(strcmp(readbuffer, "stop") == 0 ) again = 0;
return(0);
}//exit main
}
我的作家
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/stat.h>
#include<linux/stat.h>
#define FIFO_FILE "MYFIFO"
int main(int argc, char *argv[]){
FILE *fpoint;
int again =1;
char strIn[80] = "Use message from command line";
if(argc !=2){
printf("USAGE: NamedPipeClient[string]\n");
exit(1);
}
strcpy(strIn, argv[1]);
while(again == 1){
if((fpoint = fopen (FIFO_FILE, "w")) == NULL){
perror("fopen");
exit(1);
}
fputs(strIn, fpoint);
fclose(fpoint);
printf("Enter message to send: ");
scanf("%s", strIn);
again = strcmp(strIn, "Stop");
}
if((fpoint = fopen(FIFO_FILE, "w")) == NULL){
perror("fopen");
exit(1);
}
fputs(strIn,fpoint);
fclose(fpoint);
return(0);
}
【问题讨论】:
-
您的标题说“fork”,但您的代码在任何地方都没有调用 fork??
-
@JohnZwinck 我有叉子,但这不相关。当我说 fork 时,我指的是子进程和父进程。
-
我只是想先实现FIFO,所以我可以稍后输入fork
-
所以您要在没有任何参数的情况下启动
./writer?但它需要在运行时在命令行中输入字符串参数! -
你为什么不直接删除这个火车残骸并开始一个新问题? :)
标签: c pipe fork gnu named-pipes