Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8668721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:17:05+00:00 2026-06-12T18:17:05+00:00

Doing homework with signals and fork and have a problem with the signal. I’ve

  • 0

Doing homework with signals and fork and have a problem with the signal.

I’ve created the function:

void trata_sinal_int() {
    char op[2];

    printf("\nTerminate? (y/n)\n");

    scanf("%s", op);

    if (op[0] == 'y') {
        printf("Bye Bye\n");
        exit(0);
    }

}

And in main I have:

signal(SIGINT, trata_sinal_int);

When I run this, and press CTRL ^C the function void trata_sinal_int() is called and I got the message.

If I press y program ends as expected but if I press n program still ends.
It is not returning to were he was before pressing CTRL ^C.

Is this supposed to happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:17:06+00:00Added an answer on June 12, 2026 at 6:17 pm

    It depends on which standard you are adhering to, but Standard C doesn’t allow you to do much more than modify a variable of type volatile sig_atomic_t or call _Exit (or abort() or signal()) from a signal handler. POSIX is a lot more lenient. Your code in your signal handler, replete with user interaction, is pushing beyond the limits of what even POSIX allows. Normally, you want your signal handler function to be small and svelte.

    Note that the signal handler function should be:

    void trata_sinal_int(int signum)
    {
    

    This allows you to compile without casts or compiler warnings about type mismatches.
    The signal() function may reset the signal handler back to default behaviour when it is invoked; classically, it is necessary to reinstate the signal handler inside the signal handler:

        signal(signum, trata_sinal_int);
    

    So far, that’s all pretty generic and semi-trivial.

    When you type the Control-C, the system does go back to roughly where it was when the signal was originally received. However, what happens next depends on where it was (one of the reasons you have to be so very careful inside the handler). For example, if it was in the middle of manipulating the free list pointers inside malloc(), it would return there, but if you’d reinvoked malloc() inside the handler, all hell might be breaking loose. If you were inside a system call, then your call may be interrupted (return with an error indication and errno == EINTR), or it may resume where it left off. Otherwise, it should go back to where the calculation was running.


    Here’s (a fixed up version of) your code built into a test rig. The pause() function waits for a signal before returning.

    #include <stdio.h>
    #include <signal.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    static void trata_sinal_int(int signum)
    {
        char op[2];
    
        signal(signum, trata_sinal_int);
    
        printf("\nTerminate? (y/n)\n");
        scanf("%s", op);
    
        if (op[0] == 'y')
        {
            printf("Bye Bye\n");
            exit(0);
        }
    }
    
    int main(void)
    {
        signal(SIGINT, trata_sinal_int);
        for (int i = 0; i < 3; i++)
        {
            printf("Pausing\n");
            pause();
            printf("Continuing\n");
        }
        printf("Exiting\n");
        return(0);
    }
    

    I should really point out that the scanf() is not very safe at all; a buffer of size 2 is an open invitation to buffer overflow. I’m also not error checking system calls.

    I tested on Mac OS X 10.7.5, a BSD derivative. The chance are good that the resetting of signal() would be unnecessary on this platform, because BSD introduced ‘reliable signals’ a long time ago (pre-POSIX).


    ISO/IEC 9899:2011 §7.14.1.1 The signal function

    ¶5 If the signal occurs other than as the result of calling the abort or raise function, the
    behavior is undefined if the signal handler refers to any object with static or thread
    storage duration that is not a lock-free atomic object other than by assigning a value to an
    object declared as volatile sig_atomic_t, or the signal handler calls any function
    in the standard library other than the abort function, the _Exit function, the
    quick_exit function, or the signal function with the first argument equal to the
    signal number corresponding to the signal that caused the invocation of the handler.
    Furthermore, if such a call to the signal function results in a SIG_ERR return, the
    value of errno is indeterminate.252)

    252) If any signal is generated by an asynchronous signal handler, the behavior is undefined.

    The references to quick_exit() are new in C2011; they were not present in C1999.

    POSIX 2008

    The section on Signal Concepts goes through what is and is not allowed inside a signal handler under POSIX in considerable detail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing homework but I have problem with non-RestFul routes. My spec is:
Me and my friend having a problem while doing a homework assignment. We have
Im doing a homework problem to make a function sumOdd to computer the sum
I'm doing some homework and while I have some experience with SML, Haskell has
As question states, I am doing homework which have 2 variables BOOK, and MAGAZINE
newbie doing Java homework here. I have one class named Album which contains the
So I am doing homework and I am stuck on one spot. I have
Upon doing my homework, I stumbled across a problem concerning Python and image manipulation.
Im doing a homework problem for computer architecture and im stumped on using a
I'm doing my homework on java I/O data, the problem is i'm not allowed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.