Sharing the FD ( get from Open)


Can I share the FD across the thread of the same process. Is there any specific option which I need to give while opening the device.

I am having the following problem,
I have one main process, in which I spawn two thread. Now I open a file in the main process, and I want to read from one thread and write from other thread. By doing so, I was getting the error, as bad file descriptors in the write thread.

Any idea in this regards??


Hello Dheeraj,

as far as I’m aware, file descriptors are owned by processes, not by threads, so you can use one and the same file descriptor in multiple threads within the same process. I think the resource manager that handles the I/O requests (read, write, devctl etc) is aware of that, i.e. it can determine which thread sent the request (using tid member in resmgr_context_t structure - correct me if I’m wrong), and might choose to deny it if it came from a different thread than the one that did the original open() .

Some more info would be helpful: What type of file are you writing to? What prevents you from using dup() to get around it? Have you tried to do a separate open() of the same file in the second thrad to see if that succeeds? Can you write to the file in the main thread that did the open(), i.e. is the file open for reading and writing (O_RDWR)?

Best regards,

Even I thought the same. But surprisingly to my observation, if I use O_CREAT flag in the OPEN option, it was not working if I open the device in the thread. Means O_CREAT was restricting the access permission of the device among the threads.

I mught be wrong, but I have asked clarification over this to QNX.