So, I'm using signals for a project, and i'm getting something that is not exactly an error i think.
I'll explain, I got this code, where sr2 is a global variable triggered by a signal SIGUSR2 , the signal handling is working fine. But then I compile and run, and when I trigger SIGUSR2, it prints "User defined signal 2" and stops.
I have this code in a while(1) loop and it's not supposed to stop. I can fix the problem by printting something, like the printf that is commented, but then it keeps printing it forever, and I can't make new inputs, to trigger other signals.
int teste( const char* path , const char* cadeia, const char* erro )
{
int i, x, stat;
int fd = open( path, O_WRONLY | O_CREAT , S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH );
x = flock(fd, LOCK_EX);
if ( sr1 )
{
printf("sr1\n");
//sr1 = !sr1 ;
}
if ( !sr2 )
{
for ( i=0 ; i<CADEIAS ; i++ )
{
stat = write( fd, cadeia, strlen(cadeia));
}
}
else
{
printf("\n");
for ( i=0 ; i<CADEIAS/2 ; i++ )
{
stat = write( fd, cadeia, strlen(cadeia));
stat = write( fd, erro, strlen(erro));
}
}
if ( stp )
{
printf("gtfo\n");
}
x = flock(fd, LOCK_UN);
close(fd);
return 0 ;
}
This is called in the "thread maker":
void * threadEscritor()
{
int a ;
char *f, *c, *e ;
f = (char *)malloc(sizeof(SIZEFICHEIRO));
c = (char *)malloc(sizeof(SIZECADEIA));
e = (char *)malloc(sizeof(SIZECADEIA));
while(1)
{
a = randomnum(4);
f = pickfile(a, f);
a = randomnum(9);
c = pickchar(a, c);
e = pickerror(a, e);
//escreve1x(f, c, e);
teste(f, c, e);
if(stp)
{
break;
}
}
pthread_exit(0);
}
Which is called in main:
int main ()
{
pthread_t tids[NUM_THREADS];
int i, safe ;
void* resultado;
sr1 = false ;
sr2 = false ;
stp = false ;
signal( SIGUSR1, sigHandler );
signal( SIGUSR2, sigHandler );
signal( SIGTSTP, sigHandler );
for ( i=0; i<NUM_THREADS ; i++ )
{
safe = pthread_create( &(tids[i]) , NULL , threadEscritor ) ;
if ( safe != 0 )
perror ("Error creating threads");
}
for ( i=0; i<NUM_THREADS ; i++ )
{
safe = pthread_join( tids[i], &resultado ) ;
if ( safe != 0 )
{
perror ("Errors waiting");
}
}
}
So I was wondering why that message is showing up (since I'm not printing it anywhere) and why it stops my while(1) loop, and why if I make a printf() it "solves" the problem.