Using mknod in Ubuntu program in c

I am trying to create a c program where I use the mknod command like

#include<stdio.h>
#include<fcntl.h>
#include<string.h>

char info[50];

main() {
    int fdr;
    int rc = mknod("testfile",'b',0);
    if(rc<0) {
        perror("Error in mnod");
    }
    fdr=open("testfile",O_RDONLY);
    read(fdr,info,50);
    printf("\n Received message=%s",info);
    printf("\n");
} 

And do something. It works well on a Red Hat system, but does not work on ubuntu, giving an erroneous error argument.

+3
source share
3 answers

mknodDeprecated you should not use it. If you want to create a FIFO, use the standard one mkfifo. If you want to create a regular file, use creator openwith O_CREAT. Yes, it mknodcan create device nodes, but on some systems there may still be a way to do this, but on a modern Linux system, you rely on the kernel and / or udevdto handle this.

+4
source

MKNOD ( "TestFile", '', 0);

'b' mknod . mknod ( umask) S_IFREG ( ) S_IFIFO ( FIFO). :

mknod ( "textfile", S_IFREG | 0666, 0);

+2

You can create a named PIPI using the mknode function, but also the user to create the dev file, so you need to specify which file you want to create with user rights, and the dev type is

Syntax:

mknode (const char* fileName, mode_t mode | S_IFIFO, (dev_t) 0)        

For instance:

  mknode("pipe1",0777 | S_IFIFO, (dev_t) 0)

You also use the mkfifo API to create the file that it defines PIPE, since you do not need to specify what type of file you want to create:

mkfifo()
0
source

Source: https://habr.com/ru/post/1795073/


All Articles