Optimizing SIGCHLD Handler for Prime Number Generation

  • Thread starter TylerH
  • Start date
In summary, the program is generating a complete, ordered list of primes and checking if a number is prime by using the map procs to map pids to numbers. If the number is prime, the child processes exit with true. If the number is not prime, the child processes exit with false.
  • #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:
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.
 
Technology news on Phys.org
  • #2
Are you using a debugger? Since you're seeing "Entered handler" and "Entered loop", but not seeing "1", or "Error <pid> exited abnormally", it would seem that something untoward is happening in this code:
Code:
auto n = procs.find(pid)->second;
procs.erase(pid);
 
  • #3
I still don't really know what was going wrong, but I suppose it probably had to do with my false assumption that signals are queued.

I fixed it by doing the waiting in the function in which the results of the child processes are needed. It works now, so I'd like to start optimizing it. Is there any way to analyze how many/which COW pages are being copied? Are COW pages copied when the parent writes or only when the child does it? If they're copied when the parent writes to them, it there any way to disable this or get a chuck of memory which doesn't do this? For context, I only want 1 copy of the list of all primes, to keep memory waste to a minimum. I know children will never write to the list, because they are guaranteed to have all the primes they need to complete their check. (From 2 to sqrt(n), where n is the number being checked.)
 

Related to Optimizing SIGCHLD Handler for Prime Number Generation

1. What is a SIGCHLD handler?

A SIGCHLD handler is a signal handler in a computer program that is responsible for handling the SIGCHLD signal, which is sent to a process when one of its child processes terminates.

2. Why does a SIGCHLD handler sometimes hang?

A SIGCHLD handler may hang if it is not properly coded or if there are issues with the program's execution. This can occur if the handler fails to handle the SIGCHLD signal, causing the process to wait indefinitely for the child process to terminate.

3. How can I troubleshoot a hanging SIGCHLD handler?

To troubleshoot a hanging SIGCHLD handler, you can use debugging tools such as gdb to analyze the program's execution and identify any errors in the handler code. You can also add print statements or logging to track the flow of the program and identify where the handler is hanging.

4. What are some common causes of a SIGCHLD handler hanging?

Some common causes of a SIGCHLD handler hanging include coding errors in the handler, issues with the program's execution flow, or issues with the child process itself. Other factors such as system resources or external dependencies can also contribute to the hanging of a SIGCHLD handler.

5. How can I prevent a SIGCHLD handler from hanging?

To prevent a SIGCHLD handler from hanging, it is important to ensure that the handler code is properly written and handles the SIGCHLD signal correctly. Additionally, proper error handling and debugging can help identify and resolve any issues that may cause the handler to hang. It is also important to monitor system resources and ensure that the child processes are functioning properly.

Similar threads

  • Programming and Computer Science
Replies
8
Views
4K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
3K
  • Programming and Computer Science
Replies
6
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top