to blocked IO. For the time being I have simply made one of the file
incorporate the select system call into my code.
> Thanks for the info. It seems that my problem is related to some sort
> of blocking IO problem. What has me confused is that each thread is
> operating on a different file descriptor, one on a local socket
> connection(the thread) and the other on a serial device connection(the
> main thread). I thought that this would let them act independently.
> However, I am not sure how to fix the problem.
> I hope this code example provides enough information to resolve my problem.
> int main(void)
> {
> fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
> socket_fd = socket(PF_LOCAL, SOCK_STREAM, 0);
> ... local socket code ...
> pthread_t thread_id;
> status = pthread_create(&thread_id, NULL, receive_loop, NULL);
> do
> {
> ... local socket stuff ..
> int client_socket_fd;
> ... more local socket stuff ...
> socket_server(client_socket_fd);
> } ......
> }
> int socket_server(int client_socket)
> {
> while(1)
> {
> ...
> read(client_socket, &length, sizeof(length));
> ... process request
> }
> }
> void *receive_loop(void)
> {
> while(1)
> {
> receive_data();
> }
> }
> void receive_data(void)
> {
> .....
> n = read(fd, &buf, 255);
> ... process data
> }
> The code seems to block in the read call of receive_data(Removing this
> read allows the socket_server process to work as expected.)
> As such, the socket_server code reads data once and then "stops"
> reading. I thought that since the reads in socket_server and
> receive_data are reading from different file descriptors then they
> wouldn't block each other, however, this does not seem to be the case.
> Any help is greatly appreciated.
> Thanks-
> Rodney
> > % I have written an app in C on Linux(Redhat 9) that produces a menu and
> > % gives the user a number of options to choose. The menu is in main and
> > % is part of of a while loop.
> > [example which doesn't actually show anything elided]
> > % Prior to executing this loop, I set a thread to accept data from
> > % another source.
> > % The data that is accepted in the main while loop and the thread are
> > % independent of each other and unrelated.
> > [...]
> > % The problem is that the first main while loop only works once and
> > % stops accepting data after it processes the first menu option.
> > % However, the thread continues to process. The receive_loop thread
> > % works flawlessly. When I remove the receive_loop thread call then the
> > % main while loop works flawlessly.
> > I suggest connecting in the de* to see where main() is blocked.
> > % Obviously, I am missing something. Should the main while loop which
> > % is (currently part of main) be setup in its own thread?
> > Unlikely. Try whittling down to a minimal example. The process of doing
> > that will often help you identify the bug yourself, but if not, you
> > can post the minimal example, which should allow someone to spot it
> > for you.
> > I've set follow-ups to comp.programming.threads, as the problem seems to
> > be thread-related.