raise() function in C++


csignal header file declared the function raise() to handle a particular signal. Signal learns some unusual behavior in a program, and calls the signal handler. It is implemented to check if the default handler will get called or it will be ignored.

Syntax:

int raise ( int signal_ )

Parameter: The function accepts a single parameter sig which specifies the signal which is artificially raised. It can take any of the 6 C standard signals.
Defined Signal Types

  • SIGILL
  • SIGINT
  • SIGSEGV
  • SIGTERM
  • SIGABRT
  • SIGFPE

Return Value: It return a nonzero value with no error in signal else it returns zero. The function returns different nonzero elements with different defined signals.

Below programs illustrate the above method:
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// raise() function when SIGABRT is passed
#include <csignal>
#include <iostream>
using namespace std;
  
sig_atomic_t s_value = 0;
void handle(int signal_)
{
    s_value = signal_;
}
  
int main()
{
    signal(SIGABRT, handle);
    cout << "Before called Signal = " << s_value << endl;
    raise(SIGABRT);
    cout << "After called Signal = " << s_value << endl;
    return 0;
}

chevron_right


Output:

Before called Signal = 0
After called Signal = 6

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// raise() function when SIGINT is passed
#include <csignal>
#include <iostream>
using namespace std;
  
sig_atomic_t s_value = 0;
void handle(int signal_)
{
    s_value = signal_;
}
  
int main()
{
    signal(SIGINT, handle);
    cout << "Before called Signal = " << s_value << endl;
    raise(SIGINT);
    cout << "After called Signal = " << s_value << endl;
    return 0;
}

chevron_right


Output:

Before called Signal = 0
After called Signal = 2

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// raise() function when SIGTERM is passed
#include <csignal>
#include <iostream>
using namespace std;
  
sig_atomic_t s_value = 0;
void handle(int signal_)
{
    s_value = signal_;
}
  
int main()
{
    signal(SIGTERM, handle);
    cout << "Before called Signal = " << s_value << endl;
    raise(SIGTERM);
    cout << "After called Signal = " << s_value << endl;
    return 0;
}

chevron_right


Output:

Before called Signal = 0
After called Signal = 15

Program 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// raise() function when SIGSEGV is passed
#include <csignal>
#include <iostream>
using namespace std;
  
sig_atomic_t s_value = 0;
void handle(int signal_)
{
    s_value = signal_;
}
  
int main()
{
    signal(SIGSEGV, handle);
    cout << "Before called Signal = " << s_value << endl;
    raise(SIGSEGV);
    cout << "After called Signal = " << s_value << endl;
    return 0;
}

chevron_right


Output:

Before called Signal = 0
After called Signal = 11

Program 5:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// raise() function when SIGFPE is passed
#include <csignal>
#include <iostream>
using namespace std;
  
sig_atomic_t s_value = 0;
void handle(int signal_)
{
    s_value = signal_;
}
  
int main()
{
    signal(SIGFPE, handle);
    cout << "Before called Signal = " << s_value << endl;
    raise(SIGFPE);
    cout << "After called Signal = " << s_value << endl;
    return 0;
}

chevron_right


Output:

Before called Signal = 0
After called Signal = 8


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.