繁体   English   中英

命名管道将无法在C程序中打开

[英]named pipe won't open in C program

我对管道具有用户读/写权限。 小组已阅读。 其他已读。 但是程序在我运行时被“卡住”。 程序1是“父级”。 程序2是“子级”。

程序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()直到写入后才发生。

同样,自从您使用fork()之后,似乎两个进程实际上都将尝试写入fifo,然后立即开始写入。

您应该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()创建的匿名管道对,但这可以说明流程,包括创建命名管道。

暂无
暂无

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

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