Multi thread data sharing problem in uclinux


 
Thread Tools Search this Thread
Top Forums Programming Multi thread data sharing problem in uclinux
# 1  
Old 02-21-2010
Question Multi thread data sharing problem in uclinux

hello,
I have wrote a multi thread application to run under uclinux.
the problem is that threads does not share data. using the ps command it shows a single process for each thread.

I test the application under Ubuntu 8.04 and Open Suse 10.3 with 2.6 kernel and there were no problems and also ps command does not shows a single process for each thread.

but testing the application on Open Suse 9 with 2.4 kernel cause the same problem.

uclinux kernel is also 2.6.

any idea?
# 2  
Old 02-21-2010
The Linux 2.4 kernel used linuxthreads, an odd threading implementation the new 2.6(and some 2.5) kernels made obsolete. Threads were effectively separate processes, but like you'd expect threads to do nearly all memory was shared including stack and heap space. This was managed from userpace through creative use(some say abuse) of the linux "clone" system call. Some things were synchronized with realtime signals which was inefficient, conflicted with some existing RT signals, and had incurable bugs, hence the push to write a better threading system from scratch: NPTL for "native POSIX threading library". The move from linuxthreads to NPTL involved huge changes in glibc and new extensions to the kernel.

Sounds like ulibc still uses the old threading model. They may have aschewed NPTL to reduce code size. The old linuxthreads system will probably work with most any kernel since the clone() system call still exists -- it's used for non-threading process cloning too so they shan't get rid of it. You're probably hitting quirks in linuxthreads that can be worked around, but without seeing the code it's hard to say.

Last edited by Corona688; 02-21-2010 at 01:50 PM..
# 3  
Old 02-22-2010
i have tried this simple code and didn't get answer:

Code:
int lock =0;

void* thread1(){
	while(lock);
	
	//some actions ....

	lock =1;
	return 0;
}

void* thread2(){
	while(!lock);
	
	//some actions ....

	lock =0;
	return 0;
}

in thread2(), lock is always 0!
# 4  
Old 02-22-2010
Try 'volatile int lock'. If you're going to modify a variable out from under something else in mysterious ways like threading or memory poking, you have to tell the compiler so. Otherwise it feels free to optimize it away on the assumption that it won't change until it changes it itself.

You should really be using mutexes, though.
# 5  
Old 02-23-2010
thanks,

Quote:
You should really be using mutexes, though.
yes, that was just an example that i used to test the threads behavior.

so the mutexes and any other data that i want to be shared must be defined as volatile, am i right?
This User Gave Thanks to mrhosseini For This Post:
# 6  
Old 02-23-2010
That 'lock' integer at the very least needs to be volatile.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 02-27-2010
Quote:
Originally Posted by Corona688
Try 'volatile int lock'. If you're going to modify a variable out from under something else in mysterious ways like threading or memory poking, you have to tell the compiler so. Otherwise it feels free to optimize it away on the assumption that it won't change until it changes it itself.

You should really be using mutexes, though.
Yes, you should use mutex. volatile alone (along with atomic operation) is not enough to guarantee correctness in multi-threaded program. The magic word here is: memory visibility. Interested readers might want to read: Mutex and Memory Visibility

Cheers,
Loïc.

---------- Post updated at 10:15 AM ---------- Previous update was at 10:07 AM ----------

Quote:
Originally Posted by mrhosseini
thanks,


yes, that was just an example that i used to test the threads behavior.

so the mutexes and any other data that i want to be shared must be defined as volatile, am i right?
No, absolutely not. As long as you use mutex correctly, there is no need to declare the variables (or the mutexes) volatile.

On a conforming POSIX platform, the compiler shall "know" how to deal with that kind of issue as long as you follow the memory visibility rules given in XBD 4.11 of IEEE Std 1003.1-2008.

Cheers,
Loïc.

This User Gave Thanks to Loic Domaigne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multi thread awk command for faster performance

Hi, I have a script below for extracting xml from a file. for i in *.txt do echo $i awk '/<.*/ , /.*<\/.*>/' "$i" | tr -d '\n' echo -ne '\n' done . I read about using multi threading to speed up the script. I do not know much about it but read it on this forum. Is it a... (21 Replies)
Discussion started by: chetan.c
21 Replies

2. UNIX for Dummies Questions & Answers

Cross-compiling libiconv for uclinux

Hi everyone, I want to cross-compile libiconv for uclinux to create a static library. I use the following command : ./configure --enable-static --disable-shared --build=i686-pc-linux-gnu --host=nios2-unknown-linux-gnu --prefix=/home/captain/Programs/nios2-linux/uClinux-dist/staging/usr... (2 Replies)
Discussion started by: moganesh
2 Replies

3. Shell Programming and Scripting

Multi thread shell programming

I have a unix directory where a million of small text files getting accumulated every week. As of now there is a shell batch program in place which merges all the files in this directory into a single file and ftp to other system. Previously the volume of the files would be around 1 lakh... (2 Replies)
Discussion started by: vk39221
2 Replies

4. Shell Programming and Scripting

data sharing between process

hi ! i want to make 2 c prog such tht if i give an input in 1st prog then i can use tht input in 2nd. so over all i want to do data sharing between process using files. plz give me suggestions how can i achieve this ? thanks ya! (2 Replies)
Discussion started by: Sgupta
2 Replies

5. Solaris

Windows/Solaris data sharing

Hi all, I have a request from Developer team in my compagny, they would like to be able to share data between unix and windows world. 1. We would like to be able to see Unix data from Windows : ?Samba ? 2 We would like to be able to see windows data from Solaris (Mount point) : ?NFS server... (4 Replies)
Discussion started by: unclefab
4 Replies

6. Programming

forking. sharing global data in childs

hi, i want to write a code for forking 3 4 child. n wants that every child process one of the account from global account list. i wrote a program for that, but problem is every child is processing every account in list. what can me done to avoid it. attaching code with it #include <stdio.h>... (2 Replies)
Discussion started by: anup13
2 Replies

7. UNIX for Advanced & Expert Users

mmap vs shared memory - which is best for sharing data between applications?

Between mmap and shared memory which is the best method of sharing data between multiple applications, interms of speed? (2 Replies)
Discussion started by: nmds
2 Replies

8. Programming

Multi threading using posix thread library

hi all, can anyone tell me some good site for the mutithreading tutorials, its application, and some code examples. -sushil (2 Replies)
Discussion started by: shushilmore
2 Replies
Login or Register to Ask a Question