简体   繁体   English

带有FIFO的Fork()

[英]Fork() with FIFO

I am having the toughest time with this assignment. 我在这项工作中度过了最艰难的时光。 So this assignment I have two children(two separate programs) and they have to write to the parent (main). 所以这个作业我有两个孩子(两个独立的程序),他们必须写给父母(主要)。 The parent has to read both data from the kids and then print it out. 父母必须从孩子那里读取两个数据,然后将其打印出来。 I have to use named pipes. 我必须使用命名管道。 Well so my FIFO keeps giving me "USAGE: NAMEPIPECLIENT[String]" message and I don't know why. 好吧,所以我的FIFO不断给我“用法:NAMEPIPECLIENT [String]”消息,我不知道为什么。 The message is on the client side by the way. 该消息是在客户端。 Also if someone can point me in a good direction on how to use fork with multiply children on separate files that would be really appreciated. 另外,如果有人可以为我指出如何在单独的文件上使用带有多个子项的fork,那将是不胜感激的。 Thanks in advance!Using GNU C 在此先感谢您!使用GNU C

My Reader 我的读者

#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
}

My Writer 我的作家

#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);
}

Here is an extensively corrected version of the writer process 这是编写器过程的经过广泛校正的版本

#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);
}

This is the main reason that only one string is read: 这是仅读取一个字符串的主要原因:

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);
}

Note that the function returns after only one pass through the loop. 请注意,该函数仅在循环执行一次后返回。

Suggest: 建议:

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;

Note: return is not a function, (similar to sizeof is not a function), so no parens needed. 注意: return不是函数,(类似于sizeof不是函数),因此不需要括号。

Note: the continually opening and closing of the FIFO is not a good idea. 注意:连续打开和关闭FIFO不是一个好主意。

Suggest only open once in any one process. 建议在任何一个过程中仅打开一次。

Suggest only close once in any one process. 建议在任何一个过程中仅关闭一次。

When calling fopen() , always check the returned value to assure the operation was successful. 调用fopen() ,请始终检查返回的值以确保操作成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM