A problem with linux threads

Discussion in 'C Programming' started by Sourav, Sep 22, 2006.

  1. Sourav

    Sourav Guest

    Hi, I am new to linux and was writing a program that uses linux POSIX
    Threads. I had to show some activity on the screen while some files
    will be copied in the background. I thought of implementing it using
    threads and to just test the algorithm I wrote the following,

    #include <stdio.h>
    #include <pthread.h>
    #include <unistd.h>

    int running=1;
    char *signs[]={"|","/","-","\\"};
    int cur_sign=0;

    void* run(void* args){
    while(running){
    printf("%s",signs[cur_sign]);
    fflush(stdin);
    (cur_sign==3)? cur_sign=0 : cur_sign++;
    sleep(1);
    printf("\b");
    }
    return NULL;
    }

    int main(void){
    pthread_t t;
    pthread_create(&t,NULL,run,NULL);
    sleep(10);
    running=0;
    return 0;
    }

    The problem with this is, it simply doesn't show the signs in the
    repeating printf statements in run function, it waits for 10 secs and
    then displays the last printf result and ends. While the same logic
    works fine with Java threads, what is going on here? Another thing,
    when I add a \n to the printf statement in the loop
    [printf("%s\n",signs[cur_sign]);], it starts printing them in the right
    way but in separate lines (that is expected), but what happens in the
    first case?
    Sourav, Sep 22, 2006
    #1
    1. Advertising

  2. "Sourav" <> wrote in message
    news:...
    > Hi, I am new to linux and was writing a program that uses linux POSIX
    > Threads. I had to show some activity on the screen while some files
    > will be copied in the background. I thought of implementing it using
    > threads and to just test the algorithm I wrote the following,
    >
    > #include <stdio.h>
    > #include <pthread.h>
    > #include <unistd.h>
    >
    > int running=1;
    > char *signs[]={"|","/","-","\\"};
    > int cur_sign=0;
    >
    > void* run(void* args){
    > while(running){
    > printf("%s",signs[cur_sign]);
    > fflush(stdin);


    fflush( stdout);

    > (cur_sign==3)? cur_sign=0 : cur_sign++;
    > sleep(1);
    > printf("\b");
    > }
    > return NULL;
    > }
    >
    > int main(void){
    > pthread_t t;
    > pthread_create(&t,NULL,run,NULL);
    > sleep(10);
    > running=0;
    > return 0;
    > }
    >
    > The problem with this is, it simply doesn't show the signs in the
    > repeating printf statements in run function, it waits for 10 secs and
    > then displays the last printf result and ends. While the same logic
    > works fine with Java threads, what is going on here? Another thing,
    > when I add a \n to the printf statement in the loop
    > [printf("%s\n",signs[cur_sign]);], it starts printing them in the right
    > way but in separate lines (that is expected), but what happens in the
    > first case?
    >
    Fred Kleinschmidt, Sep 22, 2006
    #2
    1. Advertising

  3. Sourav wrote:
    >
    > Hi, I am new to linux and was writing a program that uses linux POSIX
    > Threads. I had to show some activity on the screen while some files
    > will be copied in the background. I thought of implementing it using
    > threads and to just test the algorithm I wrote the following,


    Threads are OT here, but perhaps your problem isn't really related to
    threads.

    [...]
    > printf("%s",signs[cur_sign]);
    > fflush(stdin);

    [...]

    Flushing input streams is undefined. Did you perhaps mean to flush
    stdout, to force the output to be sent?

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Sep 22, 2006
    #3
  4. Sourav

    Sourav Guest

    Kenneth Brody wrote:
    > Sourav wrote:
    > >
    > > Hi, I am new to linux and was writing a program that uses linux POSIX
    > > Threads. I had to show some activity on the screen while some files
    > > will be copied in the background. I thought of implementing it using
    > > threads and to just test the algorithm I wrote the following,

    >
    > Threads are OT here, but perhaps your problem isn't really related to
    > threads.
    >
    > [...]
    > > printf("%s",signs[cur_sign]);
    > > fflush(stdin);

    > [...]
    >
    > Flushing input streams is undefined. Did you perhaps mean to flush
    > stdout, to force the output to be sent?
    >
    > --



    Yes, that was actually fflush(stdout), I typed it wrong here!
    > +-------------------------+--------------------+-----------------------+
    > | Kenneth J. Brody | www.hvcomputer.com | #include |
    > | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    > +-------------------------+--------------------+-----------------------+
    > Don't e-mail me at: <mailto:>
    Sourav, Sep 23, 2006
    #4
  5. Try printf("\n") before printf("%s") it is posible tha the bash colon is
    overwriting yor output to stdout
    Gregorovic Peter, Jan 3, 2007
    #5
  6. #include <stdio.h>
    #include <pthread.h>
    #include <unistd.h>
    #include <stdlib.h>
    int running=1;
    char *signs[]={(char*)'|',(char*)'/',(char*)'-',(char*)'\\'};
    int cur_sign=0;


    void* run(void* args){
    while(running){
    printf("%c\r",(int)*(signs+cur_sign));
    (cur_sign==3) ? cur_sign=0 : cur_sign++;
    fflush(stdout);
    sleep(1);
    }
    return NULL;
    }

    int main(void){
    pthread_t t;
    pthread_create(&t,NULL,run,NULL);
    sleep(10);
    running=0;
    exit(0);
    }
    Gregorovic Peter, Jan 3, 2007
    #6
  7. Gregorovic Peter <> wrote:

    > #include <stdio.h>
    > #include <pthread.h>
    > #include <unistd.h>


    (snip)

    Please take these non-C headers and show them unto the gurus of
    comp.unix.programmer, who will show unto thee forthwith their stores
    of wisdom.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Jan 3, 2007
    #7
  8. Gregorovic Peter <> writes:
    > Try printf("\n") before printf("%s") it is posible tha the bash colon
    > is overwriting yor output to stdout


    You're replying to an article that was posted several months ago.
    Please provide context when you post a followup; see
    <http://cfaj.freeshell.org/google/>.

    The article in question was off-topic when it was posted, and it still
    is.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jan 3, 2007
    #8
    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. Al Tobey

    perl 5.8.2 threads on Linux/NPTL

    Al Tobey, Dec 5, 2003, in forum: Perl
    Replies:
    0
    Views:
    514
    Al Tobey
    Dec 5, 2003
  2. yoda
    Replies:
    2
    Views:
    415
    =?utf-8?Q?Bj=C3=B6rn_Lindstr=C3=B6m?=
    Aug 1, 2005
  3. threads without threads

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

    Java Threads - Get running threads

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

Share This Page