The first problem: calling external programs in C + +
When writing a program, you will inevitably encounter the need to call an external program or someone else to write the executable file, when the programming tool for C + +, by using the system () function, or the Exec family function can be implemented to the external executable file calls. If you use the EXECLP () function, there is a problem, that is, when the EXECLP () function call ends, the subsequent code is no longer executed. The problem can be resolved by creating a child process with the fork function. and the system () function itself is the way to make calls to external programs by fork the child process.
Second question: Calculate the elapsed time of the calling program
In order to test the external program for different data processing time, you need to calculate the running time of the program, but if the above mentioned in the C language of the function to create a sub-process, the statistical time is always incorrect, and debugging the afternoon did not succeed. Then saw the fork process related information, I think it should be the child process and the parent process at the same time to execute the reason. But again, if the time node program is placed in the child process, then the EXECLP function will still not execute the time node after the program, if the time node program to the parent process, then the parent process and the child process is at the same time, still cannot capture the child process run time. Later attempts to block the parent process, execute the child process alone, and so on, are still unsuccessful.
Because you just need to calculate the time it takes to call an external program to process the data, think of the ways that you can make use of the bash script call. The bash script is as follows:
1#!/bin/Bash2Begin_time= 'Date+%h:%m:%S '3 #设置输入点集4Data_name=. /.. /test_data/input/dragon-1.5M. ASC5./src/cocone-Linux $DATA _name6 Echo7 Echo8End_time= 'Date+%h:%m:%S '9 Echo "program start time is"Ten Echo "$BEGIN _time" One Echo "program End time is" A Echo "$END _time" - Echo "start off to STL data conversion" -Python./src/trans_off_to_stl.py $DATA _name.jv the RM-RF $DATA _name.jv - RM-RF $DATA _name.dl
Under Linux, the date command gets the system time, followed by the set time format.
Where the Begin_time variable stores the system time when the program starts running. The End_time variable stores the system time at the end of the program.
A simple script implements the function of calling an external program to calculate the external program running.
Some problems with external program (executable file) calls