Solved

Signal handling in Unix

Posted on 2000-05-10
17
681 Views
Last Modified: 2013-12-26
I have a test.cgi executable which has a link and then i have an infinite while loop.Now if i click on the link provided, the cgi receives a SIGTERM and then SIGPIPE.I have written handlers for these signals and i have not exited in these handlers.But still the test.cgi is terminating.Whether it received SIGKILL after this.If it receives SIGKILL how do I handle it? I dont want my test.cgi to be terminated by a signal.I want a normal termination for it.

Here is the code

#include<stdio.h>
#include<signal.h>
int flag=0;
void handlerterm() {
    system("echo \"SIGTERM\n\"  >> /home/prashantk/sigpipe.txt");
}
void handlerpipe() {
    system("echo \"SIGPIPE\n\"  >> /home/prashantk/sigpipe.txt");
    flag=1;
}
main() {
    long count = 0,pid =1;
    char counttemp[512];
    system("echo \" \"  > /home/prashantk/sigpipe.txt");
    signal(SIGTERM,handlerterm);
    signal(SIGPIPE,handlerpipe);
    printf("Content-type:text/html\n\n");
    printf("<HTML><HEAD><BODY>\n");
    printf("<a href=\"/cgi-bin/test1.cgi\">testlink</a>\n");
    while(1) {
        if(!flag)
            printf(" ");
    }
    printf("</BODY></HTML>\n");
}

Here First SIGTERM is received,then SIGPIPE is received.I handle both.But still the process get killed.
The program given above creates my test.cgi
0
Comment
Question by:prashanthks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 4
  • +1
17 Comments
 
LVL 3

Expert Comment

by:ufolk123
ID: 2796085
Can you provide some code here.There is a race condition in old Unix signal usage in which rapid delivery of signals results in to resetting of signal handler.also write What is your enviornment.
Also you can not handle SIGKILL.There seems to be no source of SIGKILL unless you send it yourself ( unix will never send it to your program internally )so problem is somewhere else
0
 

Author Comment

by:prashanthks
ID: 2796395
Edited text of question.
0
 
LVL 1

Expert Comment

by:meerak
ID: 2797129
I think once the signal handler is invoked, the signal behaviour
becomes the default.

Try adding the code:
signal(SIGTERM,handlerterm);

to the end of handlerterm() and
  signal(SIGPIPE,handlerpipe);
to end of handlerpipe.

Also if you have truss in your machine. Run the program under
truss and find out what caused the program to exit.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:ufolk123
ID: 2806541
OOPS I overlooked the code part.Yes as doubted by me earlier it is signal behaviour which is resetting the signal handler to default.Try using the
sigaction
if available on your computer.
To Meerak:
Adding the signal handler install code
signal(SIGTERM,handlerterm);

to the end of handlerterm() and
  signal(SIGPIPE,handlerpipe);
to end of handlerpipe.

will not work as there can be delivery of a signal between start and end of handlerterm().
Let us see if it helps prsanthks.
0
 
LVL 3

Expert Comment

by:ufolk123
ID: 2824383
hi prashanthks ... where are you ?
0
 

Author Comment

by:prashanthks
ID: 2826263
I am sorry. I am busy with my work.I have not been able to check your suggestion .I will check it sooner and reply.
0
 

Author Comment

by:prashanthks
ID: 2867442
Hi ufolk,
      I am sorry for the delayed response.I tried out sigaction as told by you. But the behaviour is same.Ths signal is handle once. After that the process gets killed. In the struct sigaction I set the sa_handler to the function name i.e,
pipeact.sa_handler = handlerpipe; and
termact.sa_handler = handlerterm;
Then I made
pipeact.sa_flags = 0; and
termact.sa_flags = 0;
Then I called
sigaction(SIGPIPE,&pipeact,NULL);
sigaction(SIGTERM,&termact,NULL);

But still the thing seems not to be working.
0
 
LVL 3

Expert Comment

by:ufolk123
ID: 2869693
On some systems ( specifically QNX etc) the system() function call also modifies the signal state of a program.You try to remove system from your handlers and instead keep the file desc open of sigpipe.txt and try to write the text using file i/o calls.


void handlerterm() {
    system("echo \"SIGTERM\n\"  >> /home/prashantk/sigpipe.txt");
}

as

void handlerterm() {
    fputs(fd,"SIGTERM\n\" );
}

where fd=fopen of /home/prashantk/sigpipe.txt in main code.



0
 

Author Comment

by:prashanthks
ID: 2872866
It is not working out ufolk. I tried commenting out the "system" system call in both the handlers.
0
 

Expert Comment

by:fredos
ID: 2909449
Hello,

The problem is that you dont let signals comming in.

you need to use pause() somewhere.

I modified your version and i use pause() and it worked well.

If you want to do processing in the same time then use pause() and alarm() with an action for alarm.

I hope this helps.
0
 

Expert Comment

by:fredos
ID: 2909456
Hi again, watch-out, on some systems (unix),
the system function can block signals also.
This can create loops ! and loops and loops and ...
0
 

Expert Comment

by:fredos
ID: 2909471
Hi again, again,

your:

int flag=0;

watch out, you should use and atomic version of it.

ex:

sig_atomic_t flag=0; this will ensure that only one guy (child) will update at one time.
you dont have any childs in your example but this is a good practice.

;-)
0
 

Accepted Solution

by:
fredos earned 100 total points
ID: 2909495
Hello,

The problem is that you dont let signals comming in.

you need to use pause() somewhere.

I modified your version and i use pause() and it worked well.

 If you want to do processing in the same time then use pause() and alarm() with an action for alarm.

                   
0
 

Author Comment

by:prashanthks
ID: 2912450
Hi fredos could you please send your version of code so that I can know where to put the pause.
0
 

Author Comment

by:prashanthks
ID: 2918966
I tried with pause and alarm.But it did not work.Could you please send me the code.
0
 

Expert Comment

by:fredos
ID: 2922926
yes sure tonight !
0
 

Expert Comment

by:fredos
ID: 2923544
OK this is the basic of it, once you get a signal
it will be executed (the handler), but if you want to do something in the same time, use threads/fork or same task with alarm (in seconds) or there is a fine one in milliseconds (see ginfo libc there is also an example)

hope this helps

#include<stdio.h>
#include<signal.h>

int flag=0;

void handlerterm() {
    printf("in the term handler\n");
    system("echo \"SIGTERM\n\"  >> ./sigpipe.txt");
}

void handlerpipe() {
    printf("in the pipe handler\n");
    system("echo \"SIGPIPE\n\"  >> ./sigpipe.txt");
    flag=1;
}

main()
{
    long count = 0,pid =1;
    char counttemp[512];
    system("echo \" \"  > ./sigpipe.txt");

    signal(SIGTERM,handlerterm);
    signal(SIGPIPE,handlerpipe);

    printf("Content-type:text/html\n\n");
    printf("<HTML><HEAD><BODY>\n");
    printf("<a href=\"/cgi-bin/test1.cgi\">testlink</a>\n");

    pause();

    printf("</BODY></HTML>\n");
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expand macro to ask for filename column 8 40
post4 challenge 28 123
Dell Alienware Graphics Amplifier Driver for non-Alienware systems 18 680
firstChar challenge 13 149
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question