3

I am trying to make a c program where i am using 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 some stuff. It works well on Red Hat system, but fails on ubuntu giving error invalid argument.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
asb
  • 910
  • 3
  • 10
  • 19
  • The code you posted does not compile. Please copy and paste the actual code you are using. – bdonlan Feb 27 '11 at 15:57
  • #include #include #include 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"); } – asb Feb 27 '11 at 15:58
  • 2
    You should probably edit this into your question, it's hard to read when it's squashed in a single line like that... – bdonlan Feb 27 '11 at 15:59

3 Answers3

3

mknod is deprecated; you should not be using it. If you want to create a FIFO, use the standard mkfifo. If you want to create an ordinary file, use creat or open with O_CREAT. Yes mknod can create device nodes, and on some systems might still be the way to do it, but on a modern Linux system you rely on the kernel and/or udevd to handle this.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 1
    hmm mkfifo("testfile",010666) ==> Fails saying Invalid Argument – asb Feb 27 '11 at 16:52
  • 2
    `010666` is not a valid mode. The mode is `0666`. – R.. GitHub STOP HELPING ICE Feb 28 '11 at 04:26
  • The claim that `mknod` is deprecated is unsubstantiated. No signs of deprecation on the documentation page https://pubs.opengroup.org/onlinepubs/9699919799/ . Also it does not make sense since `udevd` runs in user space and needs a way to create special files. – beroal Apr 18 '21 at 12:08
2

mknod("testfile",'b',0);

'b' is not a very sensible argument for mknod here. mknod's argument should be a bitwise OR of a permissions mask (modified by umask) and S_IFREG (for a regular file) or S_IFIFO (for a FIFO). For example:

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

bdonlan
  • 224,562
  • 31
  • 268
  • 324
0

You can create named PIPI using mknode function but it also user to create dev file so you have to specify which file you want to create with user permission and dev type is zero

Syntax:

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

For example:

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

You also use mkfifo API for create file it's PIPE specify, In that no need to specify which kind of file you want to create:

mkfifo()
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459