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

时间:2017-04-15 14:00:47

标签: c fifo

我在管道上拥有用户读/写权限。集团已阅读。其他已阅读。但是当我运行它时程序会“卡住”。计划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);

1 个答案:

答案 0 :(得分:2)

程序将阻止写入fifo,直到正在读取它的内容。由于execl()直到之后才发生,因此不会发生子进程中的阅读。

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

你应该fork(),然后测试返回的PID。然后,在孩子应该呼叫execl()时,父母应该写入fifo。在fork()调用之前,应由父母创建fifo。

您还应该考虑使用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()创建的匿名管道对,但这说明了流程,包括创建命名管道。 / p>