Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

time taken by forked child process

Tags:

c++

fork

system

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.

like image 580
user984260 Avatar asked Apr 10 '12 01:04

user984260


People also ask

What happens when a child process is forked?

Fork system call is used for creating a new process, which is called child process, which runs concurrently with the process that makes the fork() call (parent process). After a new child process is created, both processes will execute the next instruction following the fork() system call.

How many times is forked printed?

With n fork system calls, number of child processes created are 2n – 1. Printf statement will be executed 2n times with n system calls. Explanation: Here there are 2 fork system calls in the given program.

How many child process will be created with 2 fork () calls?

Fork #2 is executed by two processes, creating two processes, for a total of four.

What does the fork () return to the child process?

Upon successful completion, fork() returns 0 to the child process and returns the process ID of the child process to the parent process. Otherwise, -1 is returned to the parent process, no child process is created, and errno is set to indicate the error.


1 Answers

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.

like image 141
Fabio Ceconello Avatar answered Oct 15 '22 13:10

Fabio Ceconello