Results 1 to 2 of 2
Hi everybody!
I have a thread that at some point receives SIGUSR1. When it receives it, it just prints out
"Ricevuto segnale 10, SIGUSR1, adesso Terminazione vale 1"
and then ...
- 07-20-2010 #1Just Joined!
- Join Date
- Jun 2010
- Posts
- 11
segmentation fault in handling SIGUSR
Hi everybody!
I have a thread that at some point receives SIGUSR1. When it receives it, it just prints out
"Ricevuto segnale 10, SIGUSR1, adesso Terminazione vale 1"
and then Segmentation fault. I cannot understand why!
here is the code:
PHP Code:volatile sig_atomic_t Terminazione=0;
static void gestoreSIGUSR (int signum)
{
printf("Ricevuto segnale %d, SIGUSR1, adesso Terminazione vale %d\n",signum,Terminazione);
return;}
static void* writer (void* arg)
{
int l,pfd;
FILE *ifp;
char * buf=malloc(DIMMEX*sizeof(char));
char* FileLog=copy_string((char*)arg);
int WorkerAttivi=0;
struct sigaction sigUSR,sigALL;
sigset_t* pset=malloc(sizeof(sigset_t));
FileLog[strlen(FileLog)]='\0';
if (sigemptyset(pset)==-1)
printf("Maschera non svuotata\0");
if (sigaddset(pset,SIGKILL)==-1)
printf("SIGKILL non inserito nella maschera\n");
if (sigaddset(pset,SIGTERM)==-1)
printf("SIGTERM non inserito nella maschera");
if (pthread_sigmask(SIG_SETMASK,pset,NULL)==-1)
printf("Maschera non impostata\n");
else printf("MASCHERA IMPOSTATA CORRETTAMENTE\n");
/* sigALL.sa_handler=SIG_IGN;
if (sigaction(SIGTERM,&sigALL,NULL)==-1)
printf("Errore assegnamento gestione segnali SIGALL nel writer\n");
if (sigaction(SIGKILL,&sigALL,NULL)==-1)
printf("Errore assegnamento gestione segnali SIGALL nel writer\n");*/
sigUSR.sa_handler=gestoreSIGUSR;
if (sigaction(SIGUSR1,&sigUSR,NULL)==-1)
printf("Errore assegnamento gestione segnali SIGUSR nel writer\n");
printf("Terminata assegnazione segnali nel writer\n");
if (chmod("serverreq",0664) == -1)
{
perror("Chmod pipe da parte del writer");
return(&errno);
}
if ((pfd=open("serverreq",O_RDONLY)) == -1)
{
perror("Apertura pipe da parte del writer");
return(&errno);
}
if ((ifp = fopen(FileLog,"w"))==NULL)
{
perror("Apertura file di log\n");
return(NULL);
}
while ((!Terminazione)||(WorkerAttivi!=0))
{
printf("Server, thread Writer: Terminazione vale %d e WorkerAttivi vale %d\n",Terminazione,WorkerAttivi);
if ( (l=read(pfd,buf,DIMMEX)) == -1)
{
perror("Lettura messaggio");
printf("Server writer: messaggio non ricevuto, sto per uscire\n");
fclose(ifp);
pthread_exit(&errno);
}
else
{
printf("Server writer: finita la read con esito %d\n",l);
if (!Terminazione)
{
printf("\n\nServer writer: messaggio ricevuto: %s\n\n\n ",buf);
if (!memcmp (buf,"JOIN",5))
WorkerAttivi++;
if (!memcmp (buf,"LEAVE",6))
WorkerAttivi--;
if (memcmp (buf,"JOIN",5)&&memcmp (buf,"LEAVE",6))
{
if (fprintf(ifp,"%s\n",buf)<0)
printf("Scrittura nel file di log non effettuata\n");
else printf("Scrittura nel file_log avvenuta\n");
fflush(ifp);
}
}
}printf ("Server writer, alla fine del ciclo while. Terminazione vale %d\n",Terminazione);
}
/* free(ifp);
free(buf);
free(FileLog);
fclose(ifp);*/
printf("THREAD WRITER: SONO STATO TERMINATO E CHIUDO CORRETTAMENTE\n");
pthread_exit(NULL);
}
when it receives the signal, it is probably blocked on the read call in the
loopPHP Code:while ((!Terminazione)||(WorkerAttivi!=0))
Any suggestions?
- 07-21-2010 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
You shouldn't run clib functions in signal handlers like printf(). From the signal manpage:
As you can see, printf() is NOT a "safe" function to call in a signal handler.Code:NOTES The effects of this call in a multi-threaded process are unspecified. The routine handler must be very careful, since processing elsewhere was inter- rupted at some arbitrary point. POSIX has the concept of "safe function". If a signal interrupts an unsafe function, and handler calls an unsafe function, then the behavior is undefined. Safe functions are listed explicitly in the various standards. The POSIX.1-2003 list is _Exit() _exit() abort() accept() access() aio_error() aio_return() aio_suspend() alarm() bind() cfgetispeed() cfgetospeed() cfsetispeed() cfsetospeed() chdir() chmod() chown() clock_gettime() close() connect() creat() dup() dup2() execle() execve() fchmod() fchown() fcntl() fdatasync() fork() fpathconf() fstat() fsync() ftruncate() getegid() geteuid() getgid() getgroups() getpeername() getpgrp() get- pid() getppid() getsockname() getsockopt() getuid() kill() link() listen() lseek() lstat() mkdir() mkfifo() open() pathconf() pause() pipe() poll() posix_trace_event() pselect() raise() read() readlink() recv() recvfrom() recvmsg() rename() rmdir() select() sem_post() send() sendmsg() sendto() setgid() setpgid() setsid() setsockopt() setuid() shutdown() sigaction() sigaddset() sigdelset() sigemptyset() sigfillset() sigismember() signal() sigpause() sigpend- ing() sigprocmask() sigqueue() sigset() sigsuspend() sleep() socket() socket- pair() stat() symlink() sysconf() tcdrain() tcflow() tcflush() tcgetattr() tcgetpgrp() tcsendbreak() tcsetattr() tcsetpgrp() time() timer_getoverrun() timer_gettime() timer_settime() times() umask() uname() unlink() utime() wait() waitpid() write(). According to POSIX, the behaviour of a process is undefined after it ignores a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by the kill(2) or the raise(3) functions. Integer division by zero has undefined result. On some architectures it will generate a SIGFPE signal. (Also dividing the most negative integer by -1 may generate SIGFPE.) Ignoring this signal might lead to an end- less loop.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote