LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-16-2004, 01:17 AM   #1
shilpig
LQ Newbie
 
Registered: May 2004
Posts: 8

Rep: Reputation: 0
Threads in signal handling


Pls help me in the program attached.
I am not able to figure out why with thread, signal handler doesnot return.

Hoping to get the solution.

My Program:
#include<unistd.h>
#include<signal.h>
#include<time.h>
#include<stdio.h>

#define OUR_CLOCK CLOCK_REALTIME
// General Hash Defines
#define FALSE 0
#define TRUE 1

int c;
struct itimerspec i;
struct timespec resolution;
struct sigaction sa;
sigset_t allsigs;
struct sigevent timer_event;
timer_t mytimer;
pthread_t th;


void timer_intr(int sig, siginfo_t *extra, void *cruft)
{
int noverflow;
printf("in timer_intr\n");
if(noverflow = timer_getoverrun(*(timer_t *) extra->si_value.sival_ptr))
{
printf("timer has overflowed -- error !\n");
}
return;

}

void th_proc( )
{
sigemptyset(&sa.sa_mask);
sa.sa_flags=SA_SIGINFO; //real-time signal
sa.sa_sigaction=timer_intr; //pointer to action

if(sigaction(SIGRTMIN, &sa, NULL) < 0)
{
perror("sigaction error \n");
exit(1);
}

//
// first determine whether the desired clock exists
//

if (clock_getres(OUR_CLOCK, &resolution) < 0)
{
perror("clock_getres error\n");
exit(1);
}

printf("clock resolution %d sec %d nsec \n", resolution.tv_sec, resolution.tv_nsec);

//
// create a timer based upon the CLOCK_REALTIME clock
//

i.it_interval.tv_sec = 1;//0;

// set resolution to one-tenth of the maximum allowed

i.it_interval.tv_nsec = resolution.tv_nsec*10;
i.it_value = i.it_interval;

//
// this describes the asynchronous notification to be posted
// upon this timer's expiration:
//
// - use signals
// - send SIGRTMIN
// - send extra data consisting of a pointer back to the timer ID
// cannot pass the timer ID itself because we haven't created it
// yet.
//

timer_event.sigev_notify = SIGEV_SIGNAL;
timer_event.sigev_signo = SIGRTMIN;
timer_event.sigev_value.sival_ptr = (void *)&mytimer;

if (timer_create(OUR_CLOCK, &timer_event, &mytimer) < 0)
{
perror("timer create error\n");
exit(1);
}

// relative timer, go off at the end of the interval

if(timer_settime(mytimer, 0 , &i, NULL) < 0)
{
perror("settimer\n");
exit(3);
}

printf("timer_settime\n");

sigemptyset(&allsigs);
while(1)
{
printf("before\n");
sigsuspend(&allsigs);
printf("after\n");
}
exit(4);
}


static int launchThread()
{
int iRet = 0;
int iResult = TRUE;

iRet = pthread_create( &th, NULL, (void *)(&th_proc), // thread Proc
NULL);
if ( iRet != 0 )
{
printf("Failed to create thread : Err %d.\n", iRet);
iResult = FALSE;
}

return iResult;
}


int main()
{

// Signal Handler doesnot return to the calling i.e "after" is not printed. WHY?
if( launchThread() == FALSE)
{
printf("Failed to launch thread.\n");
return 1;
}

// If I call thread_proc (dont create thread) and comment launchThread, I get the correct output
//th_proc();

printf(" in main\n");

while(1)
{
sleep(100);
}
}



My Makefile:
all:
cc -g -pthread -lrt -o try4.o try4.c
clean:
rm -f try4.o
 
Old 07-16-2004, 12:39 PM   #2
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
printf doesn't automatically print to the console. Printf buffers its output, and when the buffer is full, or a certain amount of time has elapsed then it outputs its buffer to the screen. Use fprintf(stderr, "Mystring %d", i); instead.

Oh, and you do know that when you call an exit(0); it will exit your program, not the thread. (Someone correct me if I am wrong about this, but I REALLY don't think I am). Use return instead.

Hope that helps.
 
Old 07-16-2004, 01:10 PM   #3
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
u need to post that code inside of CODE TAGS please.
 
Old 08-20-2009, 03:14 AM   #4
naveenisback
Member
 
Registered: Jun 2009
Posts: 80
Blog Entries: 1

Rep: Reputation: 16
put the code in code tag

Code:
#include<unistd.h>
#include<signal.h>
#include<time.h>
#include<stdio.h>

#define OUR_CLOCK CLOCK_REALTIME
// General Hash Defines
#define FALSE 0
#define TRUE 1

int c;
struct itimerspec i;
struct timespec resolution;
struct sigaction sa;
sigset_t allsigs;
struct sigevent timer_event;
timer_t mytimer;
pthread_t th;


void timer_intr(int sig, siginfo_t *extra, void *cruft)
{
int noverflow;
printf("in timer_intr\n");
if(noverflow = timer_getoverrun(*(timer_t *) extra->si_value.sival_ptr))
{
printf("timer has overflowed -- error !\n");
}
return;

}

void th_proc( )
{
sigemptyset(&sa.sa_mask);
sa.sa_flags=SA_SIGINFO; //real-time signal
sa.sa_sigaction=timer_intr; //pointer to action

if(sigaction(SIGRTMIN, &sa, NULL) < 0)
{
perror("sigaction error \n");
exit(1);
}

//
// first determine whether the desired clock exists
//

if (clock_getres(OUR_CLOCK, &resolution) < 0)
{
perror("clock_getres error\n");
exit(1);
}

printf("clock resolution %d sec %d nsec \n", resolution.tv_sec, resolution.tv_nsec);

//
// create a timer based upon the CLOCK_REALTIME clock
//

i.it_interval.tv_sec = 1;//0;

// set resolution to one-tenth of the maximum allowed

i.it_interval.tv_nsec = resolution.tv_nsec*10;
i.it_value = i.it_interval;

//
// this describes the asynchronous notification to be posted
// upon this timer's expiration:
//
// - use signals
// - send SIGRTMIN
// - send extra data consisting of a pointer back to the timer ID
// cannot pass the timer ID itself because we haven't created it
// yet.
//

timer_event.sigev_notify = SIGEV_SIGNAL;
timer_event.sigev_signo = SIGRTMIN;
timer_event.sigev_value.sival_ptr = (void *)&mytimer;

if (timer_create(OUR_CLOCK, &timer_event, &mytimer) < 0)
{
perror("timer create error\n");
exit(1);
}

// relative timer, go off at the end of the interval

if(timer_settime(mytimer, 0 , &i, NULL) < 0)
{
perror("settimer\n");
exit(3);
}

printf("timer_settime\n");

sigemptyset(&allsigs);
while(1)
{
printf("before\n");
sigsuspend(&allsigs);
printf("after\n");
}
exit(4);
}


static int launchThread()
{
int iRet = 0;
int iResult = TRUE;

iRet = pthread_create( &th, NULL, (void *)(&th_proc), // thread Proc
NULL);
if ( iRet != 0 )
{
printf("Failed to create thread : Err %d.\n", iRet);
iResult = FALSE;
}

return iResult;
}


int main()
{

// Signal Handler doesnot return to the calling i.e "after" is not printed. WHY?
if( launchThread() == FALSE)
{
printf("Failed to launch thread.\n");
return 1;
}

// If I call thread_proc (dont create thread) and comment launchThread, I get the correct output
//th_proc();

printf(" in main\n");

while(1)
{
sleep(100);
}
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Signal handling......... rajsun Programming 2 06-28-2005 08:10 AM
signal handling (SIGSEGV) in threads Sinner6 Programming 1 11-09-2004 04:52 PM
Signal Handling in Threads[POSIX] anoop_cv Programming 1 11-02-2004 02:00 AM
Signal handling in threads in 2.6 vs. 2.4 kernel dlocke Linux - Software 0 09-29-2004 06:11 PM
Signal handling Mohsen Programming 2 07-30-2003 06:55 AM


All times are GMT -5. The time now is 05:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration