select() is staying blocked after close() is called, surely this isn't normal? The program connects to my local webserver and waits for input, about 1 second after waiting, threadfunction calls close(), select never returns. Well that's what I get when the program is run from a shell, if it's run in gdb, select returns -1 and errno is 9 (bad file descriptor).
Is this normal behaviour? ...I can't see how it is. Can somebody please compile my code and report the behaviour. I'm feeling my system is screwed up
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <unistd.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <errno.h>
void * threadfunction(void *arg)
{
printf("threadfunction started\n");
int fd=*(int *)arg;
sleep(1);
printf("closing socket\n");
close(fd);
}
int main()
{
int fd=socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr("127.0.0.1");
addr.sin_port=htons(80);
connect(fd,(struct sockaddr *)&addr,sizeof(struct sockaddr_in));
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(fd,&readfds);
pthread_t thread;
pthread_create(&thread,NULL,threadfunction,&fd);
printf("calling select()\n");
int ret=select(fd+1,&readfds,NULL,NULL,NULL);
printf("select returned %i\n",ret);
printf("errno is %i\n",errno);
return 0;
}
gcc test.c -o test -lpthread
If it needs to be known, i'm running:
Slackware 10.2
Linux kernel 2.4.31
libpthread-0.10.so
libc-2.3.5.so
Thanx