473,394 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

A problem with linux threads

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

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

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
Try printf("\n") before printf("%s") it is posible tha the bash colon is
overwriting yor output to stdout
Jan 3 '07 #5
#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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Josef Meile | last post by:
Hi, I'm trying to do a simple link checking for python 2.1.3 (compiled from source on linux), but it fails when I use an invalid ssl url. For example: I have zope without ssl running on port...
15
by: chahnaz.ourzikene | last post by:
Hi all, This is the first i post in this newsgroup, i hope my english is not too bad... Let's get straight to the point ! I have a little probleme using threads in my little training example :...
1
by: Leonard J. Reder | last post by:
Hello Mark, I took your three day course here at JPL and recall that you said something was wrong with the implementation of threads within Python but I cannot recall what. So what is wrong...
1
by: Don | last post by:
Hi, I'm trying to test out creating large numbers of threads under the linux 2.6 kernel. I've written some code .. see below. For some reason, I can't get to beyond about 4090 threads, even...
8
by: pavvka | last post by:
I'm making multithread server on Linux. Everything works fine, but server reaches threads limint and cannot create any one more. I've wrote simple test code to test this problem. #include...
2
by: dariophoenix | last post by:
Hi, I am trying to encapsulate Linux sockets and POSIX threads in C++ classes (I work in Knoppix, using KDevelop). Since sockets and threads are new to me, I searched for example code and found...
2
by: awalter1 | last post by:
Hi, I have a Python application that runs under HPUX 11.11 (then unix). It uses threads : from threading import Thread # Class Main class RunComponent(Thread): My application should run under...
1
by: Antoninus Twink | last post by:
On 11 Jun 2008 at 19:26, jurij wrote: If you're using user-level threads (e.g. pthreads) and doing synchronous I/O then it's likely that the whole process is being blocked by the kernel until...
3
by: mmm | last post by:
I am looking for advice on Python Editors and IDEs I have read other posts and threads on the subject and my two questions at this time are mainly about the IDLE-like F5-run facilities. While I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.