Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > A problem with linux threads

Reply
Thread Tools

A problem with linux threads

 
 
Sourav
Guest
Posts: n/a
 
      09-22-2006
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?

 
Reply With Quote
 
 
 
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      09-22-2006

"Sourav" <> wrote in message
news: ups.com...
> 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?
>



 
Reply With Quote
 
 
 
 
Kenneth Brody
Guest
Posts: n/a
 
      09-22-2006
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: <private.php?do=newpm&u=>

 
Reply With Quote
 
Sourav
Guest
Posts: n/a
 
      09-23-2006

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: <private.php?do=newpm&u=>


 
Reply With Quote
 
Gregorovic Peter
Guest
Posts: n/a
 
      01-03-2007
Try printf("\n") before printf("%s") it is posible tha the bash colon is
overwriting yor output to stdout
 
Reply With Quote
 
Gregorovic Peter
Guest
Posts: n/a
 
      01-03-2007
#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);
}
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      01-03-2007
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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-03-2007
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) kst- <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Is Netscape Leaving Firefox Behind on GNU/Linux? THE LINUX PROPAGANDA MACHINE CONTINUES. FIREFOX IGNORING LINUX............. traci.manicotti@gmail.com Computer Support 2 10-20-2007 02:12 PM
Linux... yeah linux.. Linux Have a nice cup of pee NZ Computing 19 04-17-2006 10:16 AM
TB View, Threads, Threads with unread The Invisible Man Firefox 1 03-20-2006 02:09 AM
Standard Threads vs Weightless Threads yoda Python 2 08-01-2005 09:12 PM
threads without threads sindica@gmail.com C Programming 4 08-27-2004 09:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57