Open In App

raise() function in C++

Last Updated : 18 Aug, 2018
Improve
Improve
Like Article
Like
Save
Share
Report


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:




// 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;
}


Output:

Before called Signal = 0
After called Signal = 6

Program 2:




// 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;
}


Output:

Before called Signal = 0
After called Signal = 2

Program 3:




// 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;
}


Output:

Before called Signal = 0
After called Signal = 15

Program 4:




// 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;
}


Output:

Before called Signal = 0
After called Signal = 11

Program 5:




// 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;
}


Output:

Before called Signal = 0
After called Signal = 8


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads