【问题标题】:Fork() with FIFO带有 FIFO 的 fork()
【发布时间】: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


【解决方案1】:

这是一个经过广泛修正的编写器进程版本

#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[])
{
    if(argc !=2)
    {
        printf("USAGE: NamedPipeClient[string]\n");
        exit(1);
    }

    FILE *fpoint;
    if((fpoint = fopen (FIFO_FILE, "w")) == NULL)
    {
        perror("fopen");
        exit(1);
    }

    char strIn[80];
    strcpy(strIn, argv[1]);


    int again =1;
    while(again == 1)
    {
        fputs(strIn, fpoint);

        printf("Enter message to send: ");
        scanf("%79s", strIn);
        again = strcmp(strIn, "Stop");
    }

    fputs(strIn,fpoint);
    fclose(fpoint);
    return(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);
}

请注意,该函数仅在循环一次后返回。

建议:

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;

注意:return 不是函数,(类似于sizeof 不是函数),所以不需要括号。

注意:FIFO 的不断打开和关闭不是一个好主意。

建议在任何一个进程中只打开一次。

建议在任一进程中只关闭一次。

调用fopen()时,一定要检查返回值,确保操作成功。

【讨论】:

    猜你喜欢
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 2018-04-03
    • 1970-01-01
    • 2015-06-13
    相关资源
    最近更新 更多