sigaction in C displaying 2 messages |
![]() ![]() |
sigaction in C displaying 2 messages |
Apr 21 2008, 10:33 PM
Post
#1
|
||
|
Member ![]() ![]() Posts: 24 OS: XP Pro |
CODE #include <signal.h> #include <stdio.h> #include <string.h> #include <sys/types.h> #include <unistd.h> sig_atomic_t count = 0; void handler (int signal_number) { count++; } void handler2 (int signal_number) { if(count >= 10) { printf("You have counted too many digits.\n" "You wanted %d and you can only use 9.\n" "I can't just grow an extra finger or toe people. Start over.\n",count); count = 0; } else { printf("%d\n",count); count = 0; } } void handler3 (int signal_number) { printf("Liar! It is not dead (just wounded).\n") ; } int main(int argc, char ** argv) { struct sigaction sa1, sa2, sa3; int pid; memset (&sa1, 0, sizeof (sa1)); sa1.sa_handler = &handler; sigaction(SIGUSR1,&sa1,NULL); memset (&sa2,0,sizeof(sa2)); sa2.sa_handler = &handler2; sigaction(SIGUSR2,&sa2,NULL); memset (&sa3,0,sizeof(sa2)); sa3.sa_handler = &handler3; sigaction(SIGCHLD,&sa3,NULL); pid = fork(); if (pid != 0) { for(;;) { sleep(1000); //printf("I'm awake, where's the coffee\n"); } } else { for(;;); } wait(pid); } Thanks in advance! Chris |
|
|
|
Apr 23 2008, 01:25 PM
Post
#2
|
|
![]() TA Moderator Posts: 1,275 From: SET HOMEPATH OS: Windows 95/98/2000/XP/Vista |
A couple things I see here that may or may not be problematic:
I've not had much experience with C, much less signals in C, but I hope this information gives you some assistance. There is another thing I found that may be relevant, since I see you are suspending the parent process: QUOTE (http://www.delorie.com/gnu/docs/glibc/libc_445.html) ...sleep can return sooner if a signal arrives; if you want to wait for a given interval regardless of signals, use select (see section 13.8 Waiting for Input or Output) and don't specify any descriptors to wait for. This may be the underlying issue, but you may have to debug to figure out either way. Regards, Ax |
|
|
![]() ![]() |
Similar Topics
| Topic Title | Replies / Views | Topic Information | |||||
|---|---|---|---|---|---|---|---|
![]() |
17 / 719 | 20th October 2007 - 10:37 AM zorba the geek started - last by Tal |
|||||
![]() |
1 / 172 | 20th December 2007 - 11:20 AM woodsman started - last by njantman |
|||||
![]() |
2 / 508 | 16th January 2008 - 09:19 PM hawthorn started - last by hawthorn |
|||||
![]() |
2 / 2,187 | 9th July 2008 - 03:30 PM Mayor_McCheese started - last by duscjj |
|||||
![]() |
1 / 208 | 1st March 2008 - 04:01 PM deb2120 started - last by tallin |
|||||
|
Time is now: 29th August 2008 - 04:17 PM |
| Advertisements do not imply our endorsement of that product or service. The forum is run by volunteers who donate their time and expertise. We make every attempt to ensure that the help and advice posted is accurate and will not cause harm to your computer. However, we do not guarantee that they are accurate and they are to be used at your own risk. |