c - 命名管道不会在 C 程序中打开

标签 c fifo

我拥有管道的用户读/写权限。群已阅。其他看过了。但是程序在我运行时会“卡住”。程序 1 是“父级”。方案2是“ child ”。

程序 1:

int main(int argc, char * argv[])
{
FILE *fptr; //for opening and closing input file
 int fdw;// write to pipe;
 int fdr; //read to pipe;
pid_t pid;
int inputarray[500]; 
int arraylength = 0; int j =0;
char *mypipe = "mypipe";


if (argc < 2)
{
  printf("Need to provide the file's name. \n");
  return EXIT_FAILURE;
}
//open input file
fptr = fopen(argv[1], "r");
if (fptr==NULL)
{
  printf("fopen fail.\n");
  return EXIT_FAILURE;
}

//read input file and fill array with integers
while (!feof(fptr))
{
  fscanf(fptr,"%d",&inputarray[arraylength]);
  arraylength = arraylength + 1;

}

fclose(fptr); //close input file



pid = fork();

mkfifo(mypipe, 0666);
fdw = open("mypipe",O_WRONLY);
if (fdw < 0)
{
  perror("File can't open to write.");
  return;
}
int b;
b=3;
write(fdw,&b,sizeof(b));
close(fdw);




if ( pid ==-1)
{
 perror("fork");
 exit(1);
}
int status; //exit status of child

if(pid==0)//if child process
{
   execl("program2", (char*) NULL);
}

else //if parent process
{
wait(&status);}
if((WIFEXITED(status)))
{
 printf("Child's exit code %d", WEXITSTATUS(status));
}
else{
printf("Child did not terminate with exit");}



}

程序 2:

int fdl;
int data;
fdl = open("mypipe",O_RDONLY);
if ( fdl < 0)
{
  perror("File can't open to read.");
  return;
}
read(fdl,&data,sizeof(data));
close(fdl);

最佳答案

程序将阻塞写入 fifo,直到它写入的内容被读取。子进程中的读取不会发生,因为 execl() 直到写入之后才会发生。

此外,看起来这两个进程实际上都会尝试写入 fifo,因为您 fork() 然后立即开始写入。

您应该fork(),然后测试返回的PID。然后,父项应写入 fifo,而子项应调用 execl()。 fifo 应该在 fork() 调用之前由父级创建。

您还应该考虑使用 indentclang-format 来正确格式化您的代码,这样可以简化代码阅读并可能会暴露错误(忘记花括号等)。


一个简单完整的示例程序。父进程写一个字符串给子进程,子进程逐个字符读取并输出到标准输出:

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>

void parent(void);
void child(void);

int main(void) {
  pid_t pid;

  mkfifo("myfifo", 0666); /* fails if exists, but we don't care here */

  if ((pid = fork()) < 0)
    abort();

  if (pid == 0)
    child(); /* will not return */
  else
    parent();

  return EXIT_SUCCESS;
}

void parent(void) {
  int fd;
  int len;
  int ret;
  int stat;

  char *ptr;
  char *msg = "Hello World!";

  if ((fd = open("myfifo", O_WRONLY)) < 0)
    abort();

  len = strlen(msg) + 1;
  ptr = msg;

  puts("Parent: About to write to child");

  while ((ret = write(fd, ptr, len)) != 0) {
    if (ret > 0) {
      len -= ret;
      ptr += ret;
    } else
      abort();
  }

  close(fd);

  puts("Parent: Waiting for child to exit");
  wait(&stat);

  printf("Parent: Child exited with status %d\n", stat);
}

void child(void) {
  int fd;
  int ret;

  char ch;

  if ((fd = open("myfifo", O_RDONLY)) < 0)
    abort();

  puts("Child: About to read from parent");

  while ((ret = read(fd, &ch, 1)) != 0) {
    if (ret > 0)
      putchar(ch);
    else
      abort();
  }
  putchar('\n');

  close(fd);

  puts("Child: I'm done here");
  exit(EXIT_SUCCESS);
}

在这种情况下,由于子进程和父进程都在同一个上下文中,我可以使用通过 pipe() 创建的匿名管道对,但这说明了流程,包括创建命名管道。

关于c - 命名管道不会在 C 程序中打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43431707/

相关文章:

c - 为什么带变量的数组分配有效?

c - 找出套接字的传输类型

php - 如何确定一个列表是否是另一个列表的子集?

java - 如何将此功能调整为 FIFO 和 LRU?

c++ - 在 OSX 上使用 PortAudio 写入 fifo 并使用 ffmpeg 读取

c - 当格式字符串末尾有换行符时,为什么 scanf 会要求输入两次?

c - 检测 C 头文件中的 undefined symbol

shell - 路径中有FIFO时如何递归grep

java - 可靠的 FIFO 消息传递 Java

java - 队列元素乱序排列,不按顺序排列