Question

I would like to use mknod in my code to create a file, but man says, that

The only portable use of mknod() is to create a FIFO-special file. If mode is not S_IFIFO or dev is not 0, the behavior of mknod() is unspecified.

Does that mean, that mknod is not really portable and I should use some other way to create a function? How about calling open and the instantly close? Which way is safer?

Was it helpful?

Solution

mknod is not for creating files. It's for creating device nodes. A portable application will never need to create device nodes because whether they exist, what they are, and how they're implemented/numbered is an implementation detail.

mknod historically allowed you to create fifos too (and perhaps ordinary files?), but there are instead standard interfaces for this: mkfifo for fifos, and creat (or open with O_CREAT) for ordinary files.

OTHER TIPS

If you read the rest of that paragraph, you will see:

However, nowadays one should never use mknod() for this purpose;
one should use mkfifo(3), a function especially defined for this purpose.

so what POSIX.1-2001says here is already outdated. I would just ignore it. mknodis still being used by the init scripts in the Linux systems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top