Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 3rd, 2005, 7:02 PM   #1
zyd
Newbie
 
Join Date: Oct 2005
Posts: 11
Rep Power: 0 zyd is on a distinguished road
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.
zyd is offline   Reply With Quote
Old Nov 3rd, 2005, 7:03 PM   #2
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Fork creates a new process, not a new thread.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Nov 3rd, 2005, 7:35 PM   #3
zyd
Newbie
 
Join Date: Oct 2005
Posts: 11
Rep Power: 0 zyd is on a distinguished road
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.
zyd is offline   Reply With Quote
Old Nov 3rd, 2005, 7:52 PM   #4
zyd
Newbie
 
Join Date: Oct 2005
Posts: 11
Rep Power: 0 zyd is on a distinguished road
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?
zyd is offline   Reply With Quote
Old Nov 3rd, 2005, 7:56 PM   #5
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Nov 3rd, 2005, 8:13 PM   #6
zyd
Newbie
 
Join Date: Oct 2005
Posts: 11
Rep Power: 0 zyd is on a distinguished road
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
zyd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:29 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC