Replace system and pop-up calls with calls that do not clone process memory

I am writing a multi-threaded server with memory width and performance. So I need an alternative for the standard system() and popen() calls that don't use fork() , which clone all the process memory, which usually takes too much time.

It seems to need to use vfork() , then execve() to do this.

Can someone help me with two functions:

  • Replacement for calling system() . Behavior example: one thread calls a function to execute, for example touch filename , and calls a thread that is waiting for completion to complete. (all other threads should continue to work)
  • Replacing popen() call Example of behavior: the same behavior, but you need to get the output of the command, for example ls -flags (alternative for this code: The correct code is Non-blocking pipe with popen )

thanks

+4
source share
1 answer

This is just a suggestion on how to approach the proposed solution. For two specific actions you are asking about:

  • touch : you can achieve a similar effect by opening and closing the file you want to touch
    • be careful not to use the O_TRUNC flag on open
  • ls : this is a bit more painful because you will need to use dirent.h on POSIX systems and go dirent.h results
    • opendir
    • readdir
    • Be sure to call closedir when you are done.

If you want to replace system and popen calls with something equivalent using vfork , there is some caution. Calling vfork little more difficult to use, because if the child does anything other than exec right after the call, you can corrupt the state of the parent's memory.

To replace the system :

  • create a helper program to execute the string provided in the arguments
    • a helper program can either call system on the supplied argument, or parse the command line and call exec
  • in your system replacement function, create an arg vector to call the helper program and pass it to the program line that you really want to execute as an argument to that program.
  • after calling vfork , you immediately exec helper program in child
  • parent is waiting for completion for the child

In your popen replacement:

  • create a helper program that takes stdout and stdin file descriptors as arguments and the command line you want to execute
    • the helper program will duplicate the characters 0 or 1 (or both) passed in the descriptors, as indicated by the arguments
    • a helper program can execute a line with popen and proxy data between the parent and the child, or call exec after parsing the command line
  • in your popen replacement function, use pipe to create a stdout or stdin communication channel (according to the second parameter of the popen function) and create an arg vector to invoke the helper program, passing in the corresponding file descriptor number and command line as arguments
  • after calling vfork , you immediately exec helper program in child
  • you'll need pclose replacement to use child process
+3
source

All Articles