Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compile errors using signal.h in Linux [duplicate]

I'm writing a shell program that must handle signals. My relevant signal handling related code is as follows:

#include <signal.h>
...
#include <sys/types.h>
...
void installSigactions( int, struct sigaction* );

void handler_function( int signal_id );
...
/*define signal table*/
struct sigaction signal_action;

/*insert handler function*/
signal_action.sa_handler = handler_function;

/*init the flags field*/
 signal_action.sa_flags = 0;

/*are no masked interrupts*/
sigemptyset( &signal_action.sa_mask );

/*install the signal_actions*/
sigaction( SIGINT, &signal_action, NULL );

Compiling gives me the following warnings and errors:

gcc -Wall -ggdb -ansi -static -pedantic -o os1shell2 os1shell2.c
os1shell2.c:35: warning: 'struct sigaction' declared inside parameter list
os1shell2.c:35: warning: its scope is only this definition or declaration, 
which is probably not what you want
os1shell2.c: In function 'main':
os1shell2.c:66: error: storage size of 'signal_action' isn't known
os1shell2.c:75: warning: implicit declaration of function 'sigemptyset'
os1shell2.c:78: warning: implicit declaration of function 'sigaction'

Can anyone tell me why I'm getting these warnings and errors?

like image 243
rurouniwallace Avatar asked Jan 13 '12 04:01

rurouniwallace


People also ask

What is sigaction in Linux?

The sigaction() system call is used to change the action taken by a process on receipt of a specific signal. (See signal(7) for an overview of signals.) signum specifies the signal and can be any valid signal except SIGKILL and SIGSTOP. If act is non-NULL, the new action for signal signum is installed from act.

How does sigaction work?

The sigaction() function examines, changes, or both examines and changes the action associated with a specific signal. The sig argument must be one of the macros defined in the <signal. h> header file. If sigaction() fails, the action for the signal sig is not changed.

What is sa_ mask?

sa_mask. A signal mask of type sigset_t that specifies an additional set of signals to be blocked during processing by the signal handling function specified by the sa_handler field. sa_flags. Flags that affect the behavior of the signal.

What are signal handlers in Linux?

A signal can be generated by calling raise() or kill() system calls. raise() sends a signal to the current process, kill() sends a signal to a specific process. A signal handler is a function which is called by the target environment when the corresponding signal occurs.


1 Answers

It will work if you remove -ansi from your compile line. I suspect the problem is that the posix parts of the signal library aren't included when you specify -ansi.

If you really don't want to disable -ansi, you can also add -D_POSIX_C_SOURCE to the compiler options.

Here is a short discussion of ANSI and the gcc feature test macros.

like image 187
Timothy Jones Avatar answered Sep 16 '22 12:09

Timothy Jones