Abdelghani Bekka
Abdelghani Bekka

Reputation: 684

Segmentation Fault with mkfifo

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.

Upvotes: 2

Views: 902

Answers (1)

Achal
Achal

Reputation: 11931

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

Upvotes: 1

Related Questions