Eddie M
Eddie M

Reputation: 23

In C, does popen write its result in the hard drive as a file pointer?

I created and ran a C program that used popen to call a command from the terminal and check the corresponding output in the file pointer that popen returned. popen and pclose were in a while loop which ran millions of times. However, since popen returns a file pointer, I'm worried that it was creating and deleting this information from my SSD all these millions of times and potentially shortening the life span of the SSD by this.(Because files are stored in storage) I am pretty sure I am wrong on this and that is not how it works but I just want to be sure.

Upvotes: 2

Views: 204

Answers (1)

maxy
maxy

Reputation: 5457

You are correct, it a pipe between two processes doesn't take a detour via harddisk.

Some background: in Unix/Linux, file descriptors are also used for all possible things that are not actually files.

  • Network sockets
  • Timers
  • Device files (keyboard or mouse input, for example)
  • Pipes (as you noticed)
    • This includes console standard input/output/error, e.g. for printf (which can be redirected to a file sometimes, without the program noticing)
  • Any other message queue (for IPC)
  • etc.

Those things all have something in common: your application will need to call into the kernel (write() or read()) and may have to wait until input/output is possible.

If you are waiting for multiple of those things at once, there is a big advantage of having only file descriptors: you can give the kernel a list of file descriptors that you're waiting for, and it will wake up your process if any of them is ready. If you want to read more about this, look at the select or poll/epoll manpages/syscalls.

Without this concept, you'd have to use a new thread for everything you are waiting for, unless you are okay with blocking your whole program or wasting CPU time with polling (non-blocking I/O).

Upvotes: 2

Related Questions