C++ Signal Handling | Find output programs | Set 1

This section contains the C++ find output programs with their explanations on C++ Signal Handling (set 1).
Submitted by Nidhi, on September 14, 2020

Program 1:

#include <iostream>
#include <csignal>

using namespace std;

void my_signal_handler(int sig)
{
    cout << "Signal is raised";
}

int main()
{
    signal(SIGINT, my_signal_handler);
    cout << "In the main() function" << endl;

    return 0;
}

Output:

In the main() function

Explanation:

In the above program, we created a signal handler my_signal_handler and bind with signal SIGINT, and in the main() function we print the message "In the main() function" using cout. Here the signal will not raise automatically. Either we need to press "CTRL+C" or need to write code to raise signal explicitly.

Program 2:

#include <iostream>
#include <csignal>

using namespace std;

void my_signal_handler(int sig)
{
    cout << "Signal is raised";
}

int main()
{
    signal(SIGINT, my_signal_handler);

    raise(SIGINT);

    return 0;
}

Output:

Signal is raised

Explanation:

The above program will print "Signal is raised" on the console screen. In the above program, we created a signal handler my_signal_handler and bind with signal SIGINT, and in the main() function we raised signal explicitly. That's why the "Signal is raised" message will print on the console screen.

Program 3:

#include <iostream>
#include <csignal>

using namespace std;

void my_signal_handler(int sig, char* msg)
{
    cout << msg;
}

int main()
{
    signal(SIGINT, my_signal_handler, "Signal is raised");

    raise(SIGINT);

    return 0;
}

Output:

[Error] invalid conversion from 'void (*)(int, char*)' to '__p_sig_fn_t {aka void (*)(int)}' [-fpermissive]
[Error] too many arguments to function 'void (* signal(int, __p_sig_fn_t))(int)'

Explanation:

The above program will generate a compilation error. Because here we passed three arguments in the signal function, but we can pass only two arguments into the signal() function, then the 3rd argument is invalid that's why the program will generate a compile-time error.

Program 4:

#include <iostream>
#include <csignal>

using namespace std;

void my_signal_handler(int sig)
{
    cout << "Signal is raised" << endl;
}

int main()
{
    int ret = 0;

    signal(SIGINT, my_signal_handler);

    ret = raise(SIGINT);

    if (ret == 0) {
        cout << "############";
    }
    else {
        cout << "@@@@@@@@@@@@";
    }

    return 0;
}

Output:

Signal is raised
############

Explanation:

In the above program, we created a signal handler my_signal_handler and bind with signal SIGINT, and in the main() function we raised signal explicitly using raise() function that calls signal handler and returns 0.

The raise() function return 0 on success and return non-zero value on failure. In our case the raise() function will return 0 then the "############" will print on the console screen.

Program 5:

#include <iostream>
#include <csignal>

using namespace std;

int SigVal = 0;

void my_signal_handler(int sig)
{
    SigVal = sig;
}

int main()
{
    signal(SIGTERM, my_signal_handler);
    cout << "Before Signal raise() Value is: " << SigVal << endl;

    raise(SIGTERM);
    cout << "After Signal raise() Value is: " << SigVal << endl;

    return 0;
}

Output:

Before Signal raise() Value is: 0
After Signal raise() Value is: 15

Explanation:

In the above program, we created a signal handler my_signal_handler and bind with signal SIGTERM, and in the main() function we raised signal explicitly using raise() function that calls signal handle.

Here we declared global variable SigVal which is initialized with 0, and we assign the value in the signal handler. As we know that the value of SIGTERM is 15, then the value of SigVal becomes 15 after calling raise() function.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.