Solved

Signal handling in Unix

Posted on 2000-05-10
17
683 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

 Database Backup and Recovery Best Practices

Join Percona’s, Architect, Manjot Singh as he presents Database Backup and Recovery Best Practices (with a Focus on MySQL) on Thursday, July 27, 2017 at 11:00 am PDT / 2:00 pm EDT (UTC-7). In the case of a failure, do you know how long it will take to restore your database?

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

634 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