0

Suppose I have three C modules:

  • The first one's function is to generate random output from /dev/urandom and it can work fine independently without other two modules.
  • The second one's function is to take some forms of input and output some kinds of useful information and it can work fine alone, too.
  • The last one's function is to manage the former two modules, and connect them, control them through stdout and stdin. Modules need to communicate through streams under the control of the third module. Is that a possible scenario? I found that many examples on the Internet just define a function outside a C source file and call it in the main function. That's too weak a relationship. I just wonder if my above scenario is possible. If possible, what kinds of knowledge does it need?
Tamier
  • 1
  • 3

1 Answers1

0

Is that a possible scenario?

Yes. Consider the fact that you can type commands like: cat /dev/urandom | hexdump, which does exactly what you describe where cat is one of your 'modules' and hexdump is the other. In this scenario the shell itself is your third module, starting the other two executables and tying their io together.

I just wonder if my above scenario is possible. If possible, what kinds of knowledge does it need?

You could to read up on the APIs for starting up processes and in particular how to configure their standard IO streams.

On Linux for example the usual way of doing this is using fork() to create a process, execv() to load up your 'modules', pipe() to create the connections between processes, and dup2() to put those connections into the right (stdio) locations.

Here's an example.

I found that many examples on the Internet is just defining a function outside a C source file and call it in the main function. That's too weak relationship.

Not that this is relevant, but I'd actually call that a stronger relationship than what you're talking about. This uses the system linker to tie the two bits of code together. About the only tighter binding would be if you didn't use functions and just wrote the code all together.

Community
  • 1
  • 1
bames53
  • 86,085
  • 15
  • 179
  • 244