473,794 Members | 2,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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",sig ns[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,NUL L);
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",s igns[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 2047

"Sourav" <so*********@gm ail.comwrote in message
news:11******** *************@h 48g2000cwc.goog legroups.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",sig ns[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,NUL L);
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",s igns[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",sig ns[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",sig ns[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*)'|',(c har*)'/',(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,NUL L);
sleep(10);
running=0;
exit(0);
}
Jan 3 '07 #6
Gregorovic Peter <gr************ **@gmail.comwro te:
#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.progr ammer, 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)gma il.com | don't, I need to know. Flames welcome.
Jan 3 '07 #7
Gregorovic Peter <gr************ **@gmail.comwri tes:
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_Keit h) 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
2117
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 8080, if I do this on the browser's address bar: https://my_ip:8080 --> The https is intentional mozilla on linux and IE on windows, take a long time till a timeout
15
1649
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 : I wish to create two threads in my application, one thread (the subject) will increment a variable, and another thread (the controller) will print a message saying "i am waiting..." or someting like that, until the counter of the first thread...
1
1230
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 with threads in Python? The other part of this question is, if there is something wrong with the threads in Python has anyone wrapped the Posix threads C api using swig to fix this problem? I work on Solaris/Linux systems and this seems like a...
1
2590
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 when I set the per-thread stack size to be fairly small. Am I bumping into a hard limit? Or, am I missing a configuration somewhere that would allow me to get up to 10's of thousands of threads?
8
2405
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 <iostream> #include <stdio.h> #include <stdlib.h> #include <pthread.h> using namespace std;
2
24283
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 the following: #include <stdlib.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <netdb.h>
2
1254
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 Linux (red hat 3 ou 4) and I read that differences exist between the implementation of threads : on HPUX 11.11 there is CMA (ou DCE) threads and on Linux POSIX thread. Do you
1
3122
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 the I/O system call returns. To be honest, on a uniprocessor system you're not likely to get much help from threading in the situation you describe, even if you do complicated things to get asynchronous I/O.
3
3953
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 am fairly happy using IDLE, the debugger is unintuitive to me and I wanted a project manager and a better variable/ class browser and also the potential to edit/run other languages such as R and Tex/Latex. Windows and LINUX compatibility is...
0
10433
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10161
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.