简体   繁体   中英

named pipe won't open in C program

I have user read/write permissions on a pipe. Group has read. Other has read. But program gets "stuck" when I run it. Program 1 is the "parent". Program 2 is the "child".

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



}

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

The program will block on writing to the fifo until what it's writing is being read. The reading in the child process won't happen since the execl() doesn't happen until after the writing.

Also, it looks like both processes will actually attempt to write to the fifo since you fork() and then immediately start writing.

You should fork() , then test on the returned PID. The parent should then write to the fifo while the child should call execl() . The fifo should be created by the parent before the fork() call.

You should also consider using indent or clang-format to properly format your code, which eases reading it and may expose bugs (forgotten curly braces etc.).


A simple complete example program. The parent writes a string to the child and the child reads it character by character and outputs it to standard output:

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

In this case, since both child and parent processes are in the same context, I could have used an anonymous pipe pair created with pipe() , but this illustrates the flow, including the creation of the named pipe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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