- #1
TylerH
- 729
- 0
My program is generating a complete, ordered list of primes and children are the worker processes deciding the primality of a single number and exiting with true iff the number they are assigned it prime. The map procs maps pids to numbers being checked.
Here is where I register the handler:
Here is my handler:
When the program is ran, the only output I get is:
Entered handler.
Entered loop.
1
[hang, as in, nothing is printed and it doesn't exit]
ps -a on another terminal prints:
10304 pts/0 00:00:00 a.out
10306 pts/0 00:00:00 a.out <defunct>
10307 pts/0 00:00:00 a.out <defunct>
10308 pts/0 00:00:00 a.out <defunct>
10310 pts/1 00:00:00 ps
I'd be glad to post the rest of the code for anyone who thinks it could be the culprit, I just didn't want to bombard you with code that probably (I assume) isn't helpful.
Here is where I register the handler:
Code:
struct sigaction sa;
sa.sa_handler = sigchld_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_NOCLDSTOP;
if (sigaction(SIGCHLD, &sa, NULL) < 0) {
perror("sigaction");
exit(1);
}
Here is my handler:
Code:
void sigchld_handler(int signal){
int status;
pid_t pid;
cout << "Entered handler." << endl;
while((pid = wait(&status)) > 0){
cout << "Entered loop." << endl;
if(!WIFEXITED(status)){
cerr << "Error: " << pid << " exited abnormally." << endl;
exit(1);
}
auto n = procs.find(pid)->second;
procs.erase(pid);
cout << "1" << endl;
if(status){
cout << "Tested true: " << n << endl;
buffer.insert(n);
}
else
cout << "Tested false: " << n << endl;
cout << "2" << endl;
bool min = true;
while(min && !buffer.empty()){
mpz_class n = *buffer.begin();
for(auto i = procs.begin();(min = mpz_class(i->second) > n) && i != procs.end();i++);
if(min){
primes.push_back(n);
cout << "Added: " << n << endl;
buffer.erase(n);
} else
cout << "Buffered: " << n << endl;
}
}
/*if(pid != ECHILD){
cerr << "Error: wait() returned negative other than ECHILD" << endl;
exit(1);
}*/
}
When the program is ran, the only output I get is:
Entered handler.
Entered loop.
1
[hang, as in, nothing is printed and it doesn't exit]
ps -a on another terminal prints:
10304 pts/0 00:00:00 a.out
10306 pts/0 00:00:00 a.out <defunct>
10307 pts/0 00:00:00 a.out <defunct>
10308 pts/0 00:00:00 a.out <defunct>
10310 pts/1 00:00:00 ps
I'd be glad to post the rest of the code for anyone who thinks it could be the culprit, I just didn't want to bombard you with code that probably (I assume) isn't helpful.