Hi C++/Unix/Linux guys,
I have a question on getting some part of the code in background (not sure correct word).
I am not a very experienced C/C++ coder
I have the below code which worked well but with a list of about 1000 entries in file DeviceList, UNIX (SunSolaris) server get really CPU intensive & not very responsive to prompt.
Code does below steps;
Binary/program is run with the parameter email address.
1st while loop read the file DeviceList & each line is then build with parameters to run with the same compiled program as the main one (one program)
The new command is then called via system with "nohup" alone with "-f" followed by "&" for background. This "-f" option code in turn calls a function - "main_comn(Device) which does some complex analysis on log files" Please note that there is ONE complied binary program - used differently depending on command line parameters.
2nd while loop wait till/loop wait till 1st file with "Devicename+Resultlog.log" is generated.
3rd while loop wait till number of "Resultlog.log" (that is generated per device - each line in file DeviceList)
Now all these code works as expected when run with few number of entries (say 50) in file but when the entries are about 200-300, UNIX box CPU utilisation becomes closer to 100% due to many processes created with "nohup "+tool_name+" -f "+Devicename+" 2>/dev/null 1>/dev/null &".
is there a better way to get a multiple child run at the same time to speed up checking all log files in parallel & improved method for main code to wait till all child process are done without utilizing CPU cycles?
I head about fork() & not used before.
Any suggestion/links to improve this code & learn more on child processes etc. are much appriciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include <fstream>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <sstream>
#include <time.h>
#include <dirent.h>
#include <ctype.h>
#include <vector>
#include <unistd.h>
using namespace std;
int main(int argc, char** argv) {
int nohupCounter=1;
if (argc==2)
{
if (validateEmailAdd(argv[1]))
{
if (FileExistCheck("DeviceList"))
{
string DeviceList1="DeviceList";
string Devicename;
ifstream fileToSearch12;
fileToSearch12.open (DeviceList1.c_str());
while(getline(fileToSearch12, Devicename))
{
if (nohupCounter<=nohupCounterMAX)
{
string run_Device_test_bg="nohup "+tool_name+" -f "+Devicename+" 2>/dev/null 1>/dev/null &";
system(run_Device_test_bg.c_str());
nohupCounter++;
}
else
{
string run_Device_test_bg="nohup "+tool_name+" -f "+Devicename+" 2>/dev/null 1>/dev/null &";
system(run_Device_test_bg.c_str());
nohupCounter=1;
sleep(30);
}
}
NoDevices=noLineinAFile("DeviceList");
int NoFilesChked=0;
cout <<" "<<endl;
cout <<" ---- In progress"<<endl;
cout <<" "<<endl;
FileList();
while(!searchAStringinFile("File_list", "Resultlog.log")==1)
{
sleep(1);
remove("File_list");
FileList();
}
while(NoDevices!=NoFilesChked)
{
string CountNoDeviceFiles="ls *.log|wc -l > NuFilesChecked";
system(CountNoDeviceFiles.c_str());
string wcNumber;
ifstream fileToCheck1;
fileToCheck1.open("NuFilesChecked");
getline(fileToCheck1,wcNumber);
istringstream buffer(wcNumber);
buffer >> NoFilesChked;
cout<<" --- Checked "<<NoFilesChked<<" Devices so far --- ";
cout << '\xd';
}
}
else
{
cout <<" "<<endl;
cout <<"Device list file is not generated, internal error & existing the tool"<<endl;
cout <<" "<<endl;
return 0;
}
cout<< " Done" << endl;
}
}
else if (argc==3 && (strcmp(argv[1],"-f")==0 || strcmp(argv[1],"-f")==0))
{
string Device=argv[2];
// Call main module
main_comn(Device);
}
else
{
// Display usage
usage(argv[0]);
}
return 0;
// end of main function
}
|
Regards,
Mathew