mardi 30 août 2016

file read write with custom buffer

I'm currently fiddling out with raspberry pi gpio, the access files for which are located in /sys/class/gpio/ directory. Once correct pins are exported, we've to read and write onto the respective gpio files.

Now since those files will only contain either 0 or 1 at a moment, would providing a custom buffer via pubsetbuf call a good choice here? The call looks like -

file.rdbuf()->pubsetbuf(buf, sizeof buf);

I've read buffers are good for big read/writes since it provides less system calls and less number of read/writes if buffer size matches the required read/writes. But since here there is 0 possibility of big read/writes, only 0/1 would be written onto respective files, wouldn't having a large(default) buffer a bad choice here?

Should I set buffer and size as nullptr and 0 for direct read/write or some respective size of 0/1 chars so that buffering occurs but only for single characters, which is confusing since dosen't it means the same as the first choice? Or should I just leave the buffer size as it is and not even touch this function?

Also if I try to represent 0 and 1 as integers, what type size should my buffer be of? Currently I was doing it with string data type without pubsetbuf call, so never thought of that.

PS - I'm just trying to learn inner workings of c++ and linux so I'm not experimenting on some actual application where I might worsen things :p.

Aucun commentaire:

Enregistrer un commentaire