#include <fcntl.h>
#include <unistd.h>
int mknod(const char * path, mode_t mode, dev_t dev);
To create a device file, you need root privileges; all users may create
named pipes. The first argument contains the name of the file to be
created. The second argument encodes the file type, which can be
S_IFIFO, S_IFBLK or S_IFCHR, and the file's access mode, e.g. 0666. The
final argument contains the major and minor numbers of the device being
created. The kernel detects which device driver is responsible for
handling the device file according to the file's type (i.e., character
or block) and its major number. The device driver to identify the
specific device uses the minor number internally.
Manipulating dev_t Variables
The <sys/sysmacros.h> header defines three macros that manipulate dev_t
values. The makedev() macro takes two arguments: a major number and a
minor number, and returns a dev_t value accordingly. The major() and
minor() macros take a dev_t variable and extract the device's major and
minor versions, respectively.
The mknod Program
Fortunately, Linux provides a mknod program that enables users to create
device and named pipes entries from a command shell interface. For more
information about this program, type
man 1 mknod
A Note On August 9th Newsletter
The 08/09 newsletter discussed the various prototypes of main() and
their usage. Several users asked me about the validity of
void main()
Although several compilers (mostly on Windows) support this prototype,
it's a non-standard form. Linux and Unix users in particular shouldn't
use it because main() must always return an exit status to the kernel.
Historically, this form was introduced as a DOS hack in order to
suppress the (non-compliant) compiler warning issued when the programmer
didn't provide an explicit return statement in main(). Note that the
ANSI standard requires that when no explicit return statement is
provided, main() should return 0 implicitly. Thus, not only is void
main() a non-standard prototype, it's totally redundant anyway.