By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,590 Members | 663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,590 IT Pros & Developers. It's quick & easy.

A problem with linux threads

P: n/a
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?

Sep 22 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"Sourav" <so*********@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro 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?

Sep 22 '06 #2

P: n/a
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:Th*************@gmail.com>

Sep 22 '06 #3

P: n/a

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:Th*************@gmail.com>
Sep 23 '06 #4

P: n/a
Try printf("\n") before printf("%s") it is posible tha the bash colon is
overwriting yor output to stdout
Jan 3 '07 #5

P: n/a
#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);
}
Jan 3 '07 #6

P: n/a
Gregorovic Peter <gr**************@gmail.comwrote:
#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.
Jan 3 '07 #7

P: n/a
Gregorovic Peter <gr**************@gmail.comwrites:
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) ks***@mib.org <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.
Jan 3 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.