Problem with threads

Problem with threads

Post by Rodney Leg » Thu, 26 Jun 2003 16:44:46



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

 
 
 

Problem with threads

Post by Ulrich Eckhard » Thu, 26 Jun 2003 18:56:45



> 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.

What exactly do you mean by above statements ? What should happen, what
does happen ? Maybe show some code.

Quote:> Should the main while loop which
> is (currently part of  main) be setup in its own thread?

Not necessary.

Quote:> What else can I try to resolve this problem.

What data are shared between the threads ? It should be evident that those
need to be protected against corruption. Also consider 'hidden' data like
the filedescriptor that printf writes to.

Else, try replacing stuff with no-ops until the error goes away. Especially
consider replacing the body of the worker-thread with a call to sleep().

hth
Uli

 
 
 

Problem with threads

Post by Patrick TJ McPh » Thu, 26 Jun 2003 23:38:12



% 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.
--

Patrick TJ McPhee
East York  Canada

 
 
 

Problem with threads

Post by Rodney Lege » Fri, 27 Jun 2003 04:07:53


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);

        } ......

Quote:}

int socket_server(int client_socket)
{
        while(1)
        {
                ...

                read(client_socket, &length, sizeof(length));

                ... process request

        }

Quote:}

void *receive_loop(void)
{

        while(1)
        {
                receive_data();
        }

Quote:}

void receive_data(void)
{
        .....

        n = read(fd, &buf, 255);

        ... process data

Quote:}

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.

 
 
 

Problem with threads

Post by Joona I Palast » Fri, 27 Jun 2003 04:19:12



on comp.lang.c:

Quote:> 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.

--

| 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

 
 
 

Problem with threads

Post by CBFalcone » Fri, 27 Jun 2003 13:57:13


 *** and topposted much off topic stuff on c.l.c ***

Quote:> 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 ...



> > % 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.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!

 
 
 

Problem with threads

Post by Rodney Leg » Fri, 27 Jun 2003 14:55:32


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


> 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.

 
 
 

Problem with threads

Post by Joona I Palast » Fri, 27 Jun 2003 16:06:44



on comp.lang.c:

Quote:> 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)

--

| 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