I improved my signal handling function but now when I try to compile my program via gcc ./test2.c -Wall -Wextra
, I receive the following;
./test2.c: In function 'end_app':
./test2.c:21: warning: implicit declaration of function 'flock'
./test2.c: In function 'main':
./test2.c:46: warning: implicit declaration of function 'usleep'
./test2.c:47: warning: implicit declaration of function 'close'
This is the source code to test2.c
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <fcntl.h>
static int end=0;
static int f;
static void end_app(int sig){
printf("Ending from sig#%d",sig);
struct sigaction si;
si.sa_handler=SIG_DFL;
si.sa_flags=0;
sigaction(SIGCHLD,&si,NULL);
sigaction(SIGTSTP,&si,NULL);
sigaction(SIGTTOU,&si,NULL);
sigaction(SIGTTIN,&si,NULL);
sigaction(SIGSEGV,&si,NULL);
sigaction(SIGTERM,&si,NULL);
sigaction(SIGHUP,&si,NULL);
flock(f,LOCK_UN); //compiler gives implicit declaration warning
printf("Ended\n");end=1;
}
void newsig(){
struct sigaction s,o;
o.sa_handler=SIG_DFL;
o.sa_flags=0;
s.sa_handler=&end_app;
s.sa_flags=SA_RESTART;
sigaction(SIGCHLD,&s,&o);
sigaction(SIGTSTP,&s,&o);
sigaction(SIGTTOU,&s,&o);
sigaction(SIGTTIN,&s,&o);
sigaction(SIGSEGV,&s,&o);
sigaction(SIGTERM,&s,&o);
sigaction(SIGHUP,&s,&o);
}
int main(){
f=open("xx.pid",O_WRONLY|O_CREAT|0x700);
flock(f,LOCK_EX); //function works here
printf("Started\n");
newsig();
while(1){
usleep(1000);
if (end==1){close(f);return 0;}
}
return 0;
}
My question is why would the compiler complain about the use of flock in my signal handler function but it doesn't complain about it in the main function? and what can I do to rectify this? I need flock to work so that I can successfully lock and unlock the file.