Note that there are some explanatory texts on larger screens.

plurals
  1. POSIGINT signal()/sigaction in C++
    primarykey
    data
    text
    <p>So here is my code:</p> <pre><code>void sigHandle(int sig) { signal(SIGINT, sigHandle); //Is this line necessairy? cout&lt;&lt;"Signal: "&lt;&lt;sig&lt;&lt;endl; } int main(){ signal(SIGINT, sigHandle); while(true){ //Supposed to loop until user exits. //rest of my code } } </code></pre> <p>Now it is my understanding of signal() that when the SIGINT command (Ctrl+C right?) is received my function sigHandle should be called with an integer value of 2 (the SIGINT number), the method should run and the program should <strong>NOT</strong> exit. </p> <p>All I would like to do is just print the signal number and move on, however after printing out "Signal: 2" it exits. </p> <p>(Eventually I'm supposed to handle the first 32 interrupts but I figured Ctrl+C would be the most difficult so I'm starting here.) </p> <p>In main if I do <strong>signal(SIGINT, SIG_IGN);</strong> it ignores the signal correctly and doesn't exit but I now have no way of knowing if I recieved the SIGINT interrupt. </p> <p>Earlier I was playing around with the sigaction struct but I could not find any real comprehensive documentation on it so I decided to go with just "raw" signal handling. </p> <p>This was my sigaction code (same problem as above):</p> <pre><code>struct sigaction action; action.sa_handler = sigHandle; sigemptyset(&amp;action.sa_mask); action.sa_flags = 0; sigaction(SIGINT, &amp;action, 0); </code></pre> <p>Thanks for your help!</p> <h1>EDIT</h1> <p>OK SO After many many many hours of scowering through man pages and the internet I have happened across a (very) ghetto solution involving saving the stack pre-infinite loop then when the interrupt comes, doing what I need to do, then re-setting the stack back to where it was and calling the sigrelse() command to re-set any states that might have been changed and not re-loaded.</p> <p>I understand that this is not the most elegant/efficient/or even socially acceptable solution to this problem but it works and as far as I can tell I am not leaking any memory anywhere so it's all good...</p> <p>I am still looking for a solution to this problem and I view my stack re-setting shenanigins as only a temporary fix...</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload