![]() |
Problem with threads
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. ie. while(1) { printf("1 - Process line 1\n"); ...... process data } 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. status = pthread_create(&thread_id, NULL, receive_loop, NULL); receive_loop is similar to the main while loop in that it is composed of a while loop that reads data from another source. 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. Obviously, I am missing something. Should the main while loop which is (currently part of main) be setup in its own thread? What else can I try to resolve this problem. Your help is appreciated. Thanks- Rodney |
Re: Problem with threads
In article <52b01014.0306242344.7b48f47c@posting.google.com >,
Rodney Leger <jediknight@iname.com> wrote: % 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 debugger 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. -- Patrick TJ McPhee East York Canada ptjm@interlog.com |
Re: Problem with threads
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 Patrick TJ McPhee wrote: > In article <52b01014.0306242344.7b48f47c@posting.google.com >, > Rodney Leger <jediknight@iname.com> wrote: > > % 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 debugger 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. |
Re: Problem with threads
Rodney Leger <jediknight@mindspring.com> scribbled the following
on comp.lang.c: > 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. This is off-topic for comp.lang.c as the C standard does not define threads at all. Please drop comp.lang.c from future replies. Thanks. -- /-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\ | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++| | http://www.helsinki.fi/~palaste W++ B OP+ | \----------------------------------------- Finland rules! ------------/ "Outside of a dog, a book is a man's best friend. Inside a dog, it's too dark to read anyway." - Groucho Marx |
Re: Problem with threads
Rodney Leger wrote:
> *** and topposted much off topic stuff on c.l.c *** > 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 > .... which has been snipped ... > > Patrick TJ McPhee wrote: > > Rodney Leger <jediknight@iname.com> wrote: > > > > % 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] > > > > [...] > > > > 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. Now Mr McPhee was kind enough to give you advice, and to remove the off topic cross-postings. Yet in answering him you have immediately put back the annoying off-topic cross postings. It appears your objective is to annoy, rather than get or give information. I have also set follow-ups, so that this should not appear here anymore. -- Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net) Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net> USE worldnet address! |
Re: Problem with threads
After doing a little more research I have discovered the select
mechanism and realize that my problem was not related to threads but to blocked IO. For the time being I have simply made one of the file descriptors non-blocking until I can figure how to properly incorporate the select system call into my code. Thanks- Rodney Rodney Leger <jediknight@mindspring.com> wrote in message news:<3ef9f2a3$0$72684$9a6e19ea@news.newshosting.c om>... > 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 > > Patrick TJ McPhee wrote: > > In article <52b01014.0306242344.7b48f47c@posting.google.com >, > > Rodney Leger <jediknight@iname.com> wrote: > > > > % 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 debugger 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. |
Re: Problem with threads
Rodney Leger <jediknight@iname.com> scribbled the following
on comp.lang.c: > After doing a little more research I have discovered the select > mechanism and realize that my problem was not related to threads but > to blocked IO. For the time being I have simply made one of the file > descriptors non-blocking until I can figure how to properly > incorporate the select system call into my code. What part of "stop cross-posting off-topic messages on comp.lang.c" was too difficult for you to understand? (snip over 100 lines of needlessly quoted text) -- /-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\ | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++| | http://www.helsinki.fi/~palaste W++ B OP+ | \----------------------------------------- Finland rules! ------------/ "I am not very happy acting pleased whenever prominent scientists overmagnify intellectual enlightenment." - Anon |
| All times are GMT. The time now is 03:42 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.