Problem with threads

Discussion in 'C Programming' started by Rodney Leger, Jun 25, 2003.

  1. Rodney Leger

    Rodney Leger Guest

    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
    Rodney Leger, Jun 25, 2003
    #1
    1. Advertising

  2. In article <>,
    Rodney Leger <> 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
    Patrick TJ McPhee, Jun 25, 2003
    #2
    1. Advertising

  3. Rodney Leger

    Rodney Leger Guest

    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 <>,
    > Rodney Leger <> 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.
    Rodney Leger, Jun 25, 2003
    #3
  4. Rodney Leger <> 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 () ---------------------------\
    | 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
    Joona I Palaste, Jun 25, 2003
    #4
  5. Rodney Leger

    CBFalconer Guest

    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 <> 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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Jun 26, 2003
    #5
  6. Rodney Leger

    Rodney Leger Guest

    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 <> wrote in message news:<3ef9f2a3$0$72684$>...
    > 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 <>,
    > > Rodney Leger <> 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.
    Rodney Leger, Jun 26, 2003
    #6
  7. Rodney Leger <> 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 () ---------------------------\
    | 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
    Joona I Palaste, Jun 26, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yoda
    Replies:
    2
    Views:
    425
    =?utf-8?Q?Bj=C3=B6rn_Lindstr=C3=B6m?=
    Aug 1, 2005
  2. threads without threads

    , Aug 27, 2004, in forum: C Programming
    Replies:
    4
    Views:
    389
    William Ahern
    Aug 27, 2004
  3. Pedro Pinto

    Java Threads - Get running threads

    Pedro Pinto, Apr 8, 2008, in forum: Java
    Replies:
    2
    Views:
    1,416
    Arne Vajhøj
    Apr 9, 2008
  4. Une bévue
    Replies:
    0
    Views:
    137
    Une bévue
    Jun 14, 2006
  5. prameela

    Is it a gtk2 problem or Threads problem

    prameela, Mar 19, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    78
    prameela
    Mar 19, 2006
Loading...

Share This Page