After the first execution, the fopen() function cannot complete for some reason.
I am practicing on named pipe using C. This program will create a new pipe and send a message to the pipe. The program always runs successfully after changing the name of the pipe. However, the program will not continue when it reaches the fopen() function on the second run.
my running result As you can see my program cannot continue when it reaches fopen()
int chk_file_exists(const char *filename)
{
/* try to open file to read */
FILE *file;
printf("....\n");
if ((file = fopen(filename, "r")))
{
printf("....\n");
fclose(file);
return 1;
}
printf("....\n");
return 0;
}
int main(int argc, char **argv)
{
int fd;
char str[128] = "Hello";
char myfifo[128] = "my_pipe_one";
if (!chk_file_exists(myfifo))
{
printf("file does not exist\n");
if (mkfifo(myfifo, 0660) < 0)
{
printf("Error opening creating fifo\n");
return (-1);
}
}
else
{
printf("file does exist\n");
}
fd = open(myfifo, O_WRONLY);
write(fd, str, strlen(str) + 1);
close(fd);
fd = open(myfifo, O_RDONLY);
read(fd, str, 128);
printf("%s \n", str);
close(fd);
return 0;
}