Home > Archive > Unix Programming > December 2006 > Select in child thread
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Select in child thread
|
|
|
| i have 2 threads 'main' and 'thread_fun'.
i block at 'select' in both the threads.
SIGINT is caught in 'thread_fun'.
which 'select' should unblock when i raise SIGINT for the first time?
when i run the program, i see 'select' in 'main' being unblocked when
i raise SIGINT for the first time.
OR should both 'select's be unblocked?
please explain why it so happens?
soure code is pasted at the end.
thanks in advance.
prashant.
~~~~~~
source code
void hello(int sig);
void *thread_fun(void *input);
int thread_select;
int main(void)
{
int fd, fd_w;
// struct sigaction sa1;
fd_set rd;
int max_fd, nfds;
pthread_t thread_id;
/*
sa1.sa_handler= hello;
sa1.sa_flags=0;
sa1.sa_restorer = NULL;
sigaction(SIGINT,&sa1,NULL);
*/
pthread_create(&thread_id, NULL,thread_fun, 0);
fd = open("/root/try/try", O_RDONLY | O_NONBLOCK);
if(fd<0)
{
printf("\nmain: Error in getting fd\n");
exit(0);
}
fd_w = open("/root/try/try", O_WRONLY);
if(fd_w <0)
{
printf("\nmain: Error in getting fd\n");
exit(0);
}
FD_ZERO(&rd);
FD_SET(fd, &rd);
max_fd = 0;
if(max_fd < fd)
max_fd = fd;
while(0 == thread_select)
;
if(1 == thread_select)
{
printf("\nmain : @select\n");
nfds = select(max_fd + 1, &rd, NULL, NULL, NULL);
if(nfds < 0)
{
printf("\nmain: nfds = %d, errno = %d\n", nfds,
errno);
}
else
printf("\nmain: select success...\n");
}
printf("\nmain exit...\n");
pthread_exit(NULL);
}
//signal handler function
void hello(int sig)
{
printf("\nhello : SIGINT received !!!!!!!!!! \n");
}
void *thread_fun(void *input)
{
struct sigaction sa2;
int fd;
int r, nfd;
fd_set rd, wr;
sa2.sa_handler = hello;
sa2.sa_flags = 0;
sa2.sa_restorer = NULL;
sigaction(SIGINT,&sa2,NULL);
printf("\nthread... \n");
fd=open("/root/try/try",O_RDONLY | O_NONBLOCK);
if(fd<0)
{
printf("\nthread: Error in getting fd\n");
exit(0);
}
FD_ZERO (&rd);
FD_SET(fd, &rd);
nfd=0;
if(fd>nfd)
nfd=fd;
printf("\nthread: @ select\r");
thread_select = 1;
r = select(nfd+1, &rd, NULL, NULL, NULL);
// printf("r=%d\n",r);
if((r < 0))
{
printf("\nthread: r = %d, errrno= %d\n", r,
errno);
}
else
printf("\nthread: select success...\n");
pthread_exit(NULL);
}
| |
| Maxim Yegorushkin 2006-12-11, 7:12 pm |
|
Subbu wrote:
> i have 2 threads 'main' and 'thread_fun'.
> i block at 'select' in both the threads.
> SIGINT is caught in 'thread_fun'.
> which 'select' should unblock when i raise SIGINT for the first time?
any for SIGINT
> when i run the program, i see 'select' in 'main' being unblocked when
> i raise SIGINT for the first time.
> OR should both 'select's be unblocked?
>
> please explain why it so happens?
http://www.opengroup.org/onlinepubs....html#tag_02_04
| |
| Brian C 2006-12-14, 10:05 pm |
| Subbu wrote:
> i have 2 threads 'main' and 'thread_fun'.
> i block at 'select' in both the threads.
> SIGINT is caught in 'thread_fun'.
> which 'select' should unblock when i raise SIGINT for the first time?
> when i run the program, i see 'select' in 'main' being unblocked when
> i raise SIGINT for the first time.
> OR should both 'select's be unblocked?
>
> please explain why it so happens?
> soure code is pasted at the end.
>
>
> thanks in advance.
>
>
> prashant.
Signals are not thread safe. This means that ANY thread can receive a "SIG".
For further reference, if you need to pass "SIG"s to specific threads,
use pthread_kill().
| |
| Maxim Yegorushkin 2006-12-15, 4:11 am |
|
Brian C wrote:
> Subbu wrote:
> Signals are not thread safe.
If they were not thread safe, there would be no way to use them in a
multithreaded application. Don't you think so?
> This means that ANY thread can receive a "SIG".
This is not accurate.
Please see
http://www.opengroup.org/onlinepubs....html#tag_02_04
<q>
At the time of generation, a determination shall be made whether the
signal has been generated for the process or for a specific thread
within the process. Signals which are generated by some action
attributable to a particular thread, such as a hardware fault, shall be
generated for the thread that caused the signal to be generated.
Signals that are generated in association with a process ID or process
group ID or an asynchronous event, such as terminal activity, shall be
generated for the process.
</q>
| |
| Brian C 2006-12-16, 4:08 am |
| Maxim Yegorushkin wrote:
>
> If they were not thread safe, there would be no way to use them in a
> multithreaded application. Don't you think so?
I did not say they were unusable, I said they were not thread safe. By
that, I mean that by using signal(), you cannot guarantee which thread
will receive the signal.
| |
| loic-dev@gmx.net 2006-12-16, 7:06 pm |
| Hello,
> i have 2 threads 'main' and 'thread_fun'.
> i block at 'select' in both the threads.
> SIGINT is caught in 'thread_fun'.
> which 'select' should unblock when i raise SIGINT for the first time?
> when i run the program, i see 'select' in 'main' being unblocked when
> i raise SIGINT for the first time.
> OR should both 'select's be unblocked?
>
> please explain why it so happens?
> soure code is pasted at the end.
The POSIX threads standard specifies that the implementation is free to
choose the "most suited" thread[1] for delivering the signal. Hence
this can be either the 'main' or the 'thread_fun' thread, depending on
the OS/pthreads implementation.
The rule is: either you are prepared to receive the signal in any of
the threads. Or if only a specific thread should receive the signal,
then make sure that the other threads have blocked the signal.
The standard framework to implement "signal handler" in a
multi-threaded program consists to:
(*) block the signals with /pthread_sigmask()/ in the 'main' thread
prior to creating any thread.
(*) create a special thread that will deal with the signal.
(*) in that "signal handler" thread, wait synchronously the signal
reception using /sigwait()/. Once the signal has been received, do
whatever is necessary.
HTH,
Loic.
[1] which usually turn to be the most annoying one for the programmer.
|
|
|
|
|