Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of ioctl set of functions in linux?

In Linux/freeBSD kernel whenever we have to make a driver module for a device, we make a file in the /dev/ folder and use it to communicate with the other processes.

If that is so, what is the purpose of the ioctl set of functions ? Whatever information, we want to convey with the device driver can be written to/read from this file.

Can anyone please explain it ?

I have tried reading about it on tldp.org but couldn't really understand it.

like image 728
Pratik Singhal Avatar asked Nov 30 '25 16:11

Pratik Singhal


2 Answers

ioctl is used for I/O control, and as an example lets take a serial port. You open the serial port device, and can read from the serial port, and write to the serial port, but how do you set e.g. the baud-rate? Or other control options? It's not possible using read or write as those are for reading and writing data from/to the serial port, you need another function for this, which is where the ioctl function comes in.

like image 58
Some programmer dude Avatar answered Dec 03 '25 12:12

Some programmer dude


It would be possible to just create another device file for "control" tasks and use plain reads/writes on that, instead of ioctl. This way ioctl wouldn't be needed at all. Plan 9 operating system did stuff this way.

Thing is - Unix systems just do it in another way. They always did, and since there isn't anything really wrong with it, and lots of software uses ioctl, why bother changing it?

like image 42
Edward Tomasz Napierala Avatar answered Dec 03 '25 10:12

Edward Tomasz Napierala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!