Question:
Question related to C++ threads...?
2007-10-13 12:43:35 UTC
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?
Seven answers:
2007-10-15 15:02:04 UTC
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! ^_^
Reveille
2007-10-13 14:22:00 UTC
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
levey
2016-10-22 11:00:18 UTC
The Birds and the Bees by potential of Breathe Carolina, additionally i like the album Moths by potential of decrease Definition. they have fowl and moth of their titles i don't probably know any songs approximately those without examine Your favourite band from Arizona?
mi deng
2007-10-13 12:53:53 UTC
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?
toddk57@sbcglobal.net
2007-10-13 13:20:24 UTC
Well: according to Editing there is Ctrl in which it does work

with your Computer 's Programming &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)
i_am_the_next_best_one
2007-10-13 23:04:13 UTC
try codeproject.com

they have good thread articles
2007-10-13 13:12:31 UTC
was a c++ freak once.. dnt knw how i forgot all of it..



gud luck..


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...