Thursday, July 30, 2009

Question related to C++ threads...?

I'm trying to create a simple C++ program that uses fork() to create three processes which work together to calculate the square roots of all integers from 1 - 100. Each thread is supposed to read the int, print out its root, and increment it. My problem is I don't know how to get all three processes to share this integer! I tried a pointer, but that didn't help. What can I do?

Question related to C++ threads...?
When you call fork(), the child process gets a *copy* of the address space, which is separate from the parent's address space. The data stored at an address in one process won't necessarily be the same as the data stored at the same address in another process. So in this case, each process is working on their own copy of value.





What it sounds like you want to do is use threads, which will run in the same process, and have access to the same memory space (and global variables). You can try using pthread_create() if this is what you're trying to do.





If you really want to use fork() and create separate processes, you can use shared memory to pass data back and forth between the processes. Try using shmget() if this is the case.





In either case, don't forget to use some kind of mutual exclusion on your shared resource! (For example, pthread_mutex_lock() in the thread case, or semget() in the process case). Good luck! ^_^
Reply:Well: according to Editing there is Ctrl in which it does work


with your Computer 's Programming %26amp;Design of course


Refer below


Ctrl+X (cut)


Ctrl+C(copy)


Ctrl +V (paste)


also these others can be found too


please refer again from file


Ctrl +N ( new Window Browser)


Ctrl +T (new Tab)


Ctrl +O (open)
Reply:try codeproject.com


they have good thread articles
Reply:I think if you made the shared variable into a buffer you should be able to synchronize the data. Unfortunately there are no good examples of this on the web so I scanned a section of one of my books for you.


http://www.mediafire.com/?ajxgoy5meet


hope it helps
Reply:was a c++ freak once.. dnt knw how i forgot all of it..





gud luck..
Reply:I tried reading your program but it seems incomplete and missing stuff.... i'm guess u need fork() so ok....





but then whats pid_t ? a typedef/?





getpid(); ? some function?


No comments:

Post a Comment