We help IT Professionals succeed at work.
Get Started

Signal handling in Linux with C

paddy8788 asked
Last Modified: 2013-11-13
I am doing my project to develop a simple shell. I have a problem with handling signals that I hope you guys can help me.

I am using signal() function to catch the signal sending from the forked children to the shell, which is:
            signal(SIGCHLD, handler);
However, this is really confusing. When the user input a command into my shell, normally the shell parse it and call the corresponding programs. However, when I have a pipe like:
ls | cat | cat | cat | cat > a.txt
Then there are multiple files running at once, and the signal handler does not seem to be able to catch multiple signal at one time...

I tried to use signal mask in my handler (in the code snippet).      

But this does not seem help... I also applied the race running idea with signal suspend function to delay the moment the child return the signal but also, it does not work because I think at some moment, two child sends two SIGCHLDs at once, and that is really hard to control...
Right now, what I am doing is ask for the children to wait 1 second before sending any signals to the parent, and this works actually. But I don't think this is a smart way to handle signal...

I hope you can help me. Thank you!

Thank you!
sigset_t nmask, omask;	
	sigaddset(&nmask, SIGCHLD);
	signal(SIGCHLD, handler);
	sigprocmask(SIG_BLOCK, &nmask, &omask);			//Block all SIGCHLD signals
	/* My main code goes here */
	sigprocmask(SIG_SETMASK, &omask, NULL);			//Unblock SIGCHLD

Open in new window

Watch Question
Software Developer
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE