6

This is a sequel to my previous question. I am using fork to create child process. Inside child, I am giving command to run a process as follows:

if((childpid=fork())==0)
{
system("./runBinary ");
exit(1)
}

My runBinary has the functionality of measuring how much time it takes from start to finish.

What amazes me is that when I run runBinary directly on command-line, it takes ~60 seconds. However, when I run it as a child process, it takes more, like ~75 or more. Is there something which I can do or am currently doing wrong, which is leading to this?

Thanks for the help in advance. MORE DETAILS: I am running on linux RHEL server, with 24 cores. I am measuring CPU time. At a time, I only fork 8 child (sequentially), each of which is bound to different core, using taskset (not shown in code). The system is not loaded except for my own program.

Community
  • 1
  • 1
user984260
  • 3,401
  • 5
  • 25
  • 38
  • 1
    Maybe it takes 15 seconds for your sleeping parent process to get woken by the o/s after the child exits? – Jonathan Leffler Apr 10 '12 at 01:45
  • @JonathanLeffler can you tell how to confirm that? Thanks – user984260 Apr 10 '12 at 01:47
  • @sarnold actually it is runBinary which is itself measuring the time. – user984260 Apr 10 '12 at 01:50
  • 1
    If runBinary is doing the timing, then it is not clear that the time taken to wake the sleeping parent is the issue. Given the update about a 24 core machine and using `taskset()`, then try running it without using `taskset()` or CPU binding. Is the `runBinary` program itself multi-threaded? Could it be running into more contention when you run 8 from your own binary than when you run them one at a time from the command line? (You do run 8 from the command line, don't you?) – Jonathan Leffler Apr 10 '12 at 01:52
  • 1
    Does performance depend upon the [`/proc/sys/kernel/sched_autogroup_enabled`](http://lwn.net/Articles/415742/) variable? – sarnold Apr 10 '12 at 01:59
  • @sarnold Thanks. However, on my server and even local computer, this variable/file is not there. only: /proc/sys/kernel/sched_interactive is there. – user984260 Apr 10 '12 at 02:02
  • @JonathanLeffler runBinary is not multi-threaded. Each of its instance is independent of each other. Its memory usage is 2% (seeing from top). I don't think it causes contention. I will try to run it w/o taskset soon. – user984260 Apr 10 '12 at 02:03

2 Answers2

2

The system() function is to invoke the shell. You can do anything inside it, including running a script. This gives you a lot of flexibility, but it comes with a price: you're loading a shell, and then runBinary inside it. Although I don't think loading the shell would be responsible to so much time difference (15 seconds is a lot, after all), since it doesn't seem you need that - just to run the app - try using something from the exec() family instead.

Fabio Ceconello
  • 15,819
  • 5
  • 38
  • 51
  • Thanks. Your answer is very convincing. Do you think exec family is faster? The reason I did not use execvp was that in system I could do system("./Binary argument1 argument2 ");, whereas, I could not do so in execvp. Can you tell. – user984260 Apr 12 '12 at 16:41
  • 2
    Well, just not having to load the shell and interpret the command line given should provide a speed increase. About the command line, I didn't understand, what exectly you said you can't do? You can pass arguments to it, the only additional work is that you have to put the arguments in an array - if using the execv variants - or at least have the binary name in a separated string - if using the execl variants. Those things can be done, for instance, with strtok. – Fabio Ceconello Apr 12 '12 at 16:54
0

Without profiling the application, if the parent process which forks has a large memory space, you might find that there is time spent attempting to fork the process itself, and attempts to duplicate the memory space.

This isn't a problem in Red Hat Enterprise Linux 6, but was in earlier versions of Red Hat Enterprise Linux 5.