2

I've got a problem with the following code:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>


int main(int argc, char*argv[]){

    FILE * tube;
    char chaine[10];
    mkfifo("glue", 0666);
    tube = fopen("glue", "r");


    while(1){
        fgets(chaine, 10, tube);
        printf("%s\n", chaine);

    }

}

It's a program that mimics a server behavior, however when I run it on my Ubuntu Machine, either Windows 10 Ubuntu subsystem or the normal OS, the mkfifo line returns this error:

Segmentation Fault(core dumped)

Pls Help!

EDIT:

Forgot to send the version with the mkfifo test:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>


int main(int argc, char*argv[]){

    FILE * tube;
    char chaine[10];
    int errValue = mkfifo("glue", 0666);
    perror("mkfifo");
    if( errValue < 0){
        printf("Error: %d\n", errValue);
        exit(EXIT_FAILURE);
    }
    tube = fopen("glue", "r");


    while(1){
        fgets(chaine, 10, tube);
        printf("%s\n", chaine);

    }

}

The output of the program is:

mkfifo: Operation not permitted
Error: -1

And the one of umask is:

0000

And thank you immensely everyone for participating in this post!!:)

EDIT, PROBLEM SOLVED THANKS TO achal and everyone, thank you sooo much:

-One problem, the core one, was in using a 0022 umask instead of an 0002, thanks achal for this solution.

-The second problem was me using the Ubuntu subsystem for windows and try to run the program from CLI while it was located on the Windows desktop, windows doesn't give pipe permissions for the desktop apparently.

-The solution was to switch to my Ubuntu boot and change the umask and then it worked perfectly:)

Thanks to achal and everyone who participated in this post.

PROBLEM SOLVED.

Abdelghani Bekka
  • 576
  • 9
  • 18
  • Generate a core dump and then analyze it. https://stackoverflow.com/questions/8305866/how-to-analyze-a-programs-core-dump-file-with-gdb – Fausto Carvalho Marques Silva Nov 16 '17 at 18:09
  • 2
    I'm not sure it is `mkfifo` that causes the segmentation fault, more likely the lines after. However, you don't check the return value of `mkfifo` (which is a big mistake when programming in C, since that's the only way you can handle errors) so there's no guarantee if the function call itself is successful. The same with `fopen`, there's no guarantee it won't be `NULL`. – LeleDumbo Nov 16 '17 at 18:17
  • 1
    Most likely `mkfifo` is failing, and then you're getting an error when you try to open the nonexistent fifo. – Barmar Nov 16 '17 at 18:19
  • 1
    You're probably running the program in a directory you don't have write permission to, so `mkfifo` fails. – Barmar Nov 16 '17 at 18:20

1 Answers1

1

mkfifo() is a IPC mechanism useful for create a FIFO for communicating same or different process.

man pages of mkfifo() says "The mkfifo() system call creates a new fifo file with name path. The access permissions are specified by mode and restricted by the umask(2) of the calling process"

As you mentioned in comments in your terminal umask value was 0000 means no permission that's why mkfifo() fails, so modify umask value using CLI according your requirement.

xyz@xyz-PC:~$ umask 0002
xyz@xyz-PC:~/s_flow$ umask
0002

and modify your code as

int main(int argc, char*argv[]){

    FILE * tube;
    char chaine[10];
    int errValue = mkfifo("glue", 0666);
    perror("mkfifo");
    if( errValue < 0){
        printf("Error: %d\n", errValue);
        exit(EXIT_FAILURE);
    }
    tube = fopen("glue", "r");
    if(tube == NULL)
    {
     printf("error in opening file :\n");
     return 0;
    }
    while(1){
        fgets(chaine, 10, tube);
        printf("%s\n", chaine);

    }

}

Once umask value is set, execute your program.

I hope it helped lot

Achal
  • 11,821
  • 2
  • 15
  • 37