Using mknod on Ubuntu in c program

11,404

Solution 1

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.

Solution 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);

Share:
11,404
asb
Author by

asb

Updated on June 28, 2022

Comments

  • asb
    asb almost 2 years

    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.