![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 11
Rep Power: 0
![]() |
Share Variables Between Threads
Hi. I want to work with threads in linux using fork(), but i don't know how to share variables between diferent threads (or instances) of the same program. How do i need to declare my variables for them to work in that way?.
Thanks in advance. |
|
|
|
|
|
#2 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Fork creates a new process, not a new thread.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2005
Posts: 11
Rep Power: 0
![]() |
ok, i misused terms, but that's what i meant, it is possible for a child process to inherit or to use the same variables than his parent process?, something like a global value for all process of the same program.
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Oct 2005
Posts: 11
Rep Power: 0
![]() |
ok. i guess i also hurried in thinking the solution to my problem. what i need is the clone system call. According to what i read, with fork you can only share the text segment of the process. Does anybody know a way to share data too with fork?
|
|
|
|
|
|
#5 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Perhaps you should reconsider your design. A threaded application shares a single address space, the result being that any change made by one thread is apparent to all the other threads because it was made to the same physical address. In the case of fork(), memory and file descriptors are copied. The address of the variables doesn't change from the program's perspective, but the physical address maps to somewhere else. If you want several points of execution that affect each other, use threads. If you want to allow two copies of the app to go two seperate and independent ways, use fork (hence the name).
If you seriously need to communicate between processes, this is fairly thorough as far as the major ways of doing things on *nix. http://www.tldp.org/LDP/lpg/node1.html Threading is often a good solution, but watch out for deadlocks and sychronization. http://www.cs.nmsu.edu/~jcook/Tools/pthreads/pthreads.html http://www.yolinux.com/TUTORIALS/Lin...ixThreads.html
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Oct 2005
Posts: 11
Rep Power: 0
![]() |
thanks for the advice. the thing is that that is precisaly what i want. i want to show all the problems that can arise with threads and the diferent ways to solve them, i made a little scheduler that creates threads using interruptions (though it was for M$-DO$). But what i want to emphasize are the problems and the methods to solve them, more than the way of creating threads, so my natural choice is a *nix system since it has system calls to efficiently create threads
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|