|
Home > Archive > Unix Programming > May 2006 > select, signal, 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, signal, thread
|
|
| prashantkhoje@gmail.com 2006-05-19, 1:17 pm |
| 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);
}
| |
| Nils O. Selåsdal 2006-05-19, 7:15 pm |
| prashantkhoje@gmail.com 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.
The thread that catches the signal - Consider the signal to handled
randomly by one of the threads that doesn't have the signal blocked.
|
|
|
|
|