A couple of possible solutions come to mind. They do not get processor time after the first output for sure, but they can avoid the problem you are dealing with.
First, get rid of bash scripts and just do the equivalent work in your program before running user code (for example, between fork() and exec() ). Thus, the child process "processor time from wait4() does not include your additional configuration.
Another possibility is to write a simple application that does nothing but launch the user application and transfer its processor time back to the main application. Then this runner application can be called from your scripts to run the user program, and not directly access the user program. A runner application can use fork() / exec() / wait4() to start a user program and can report information from wait4() to your main program using any of a variety of methods, such as a named pipe, queue message, socket, or even just writing information to a file that your main program can open. Thus, your bash scripts can work both before and after starting a user program that will not be included in the processor time specified by the runner application. You would probaby want the runner to accept an argument, such as a channel name or output file, in addition to the path and arguments of the user program so that you can control how the information is reported - so you could run more than one instance of the runner application and that's it still retain the information they report separately.
If you want to include the work done with the script, but not the bash boot time, then you can signal the main program by repeating something in the pipe from the bash script before and after the parts you want to find. Then the main program can measure the time between the start and stop signals, which at least will give you the wall time (although not the actual processor time). Otherwise, I'm not sure if there is a way to ideally measure processor time for only part of the script without using modified bash (which I would have avoided if possible).
source share