6

What's your idea about simulating thread with "fork() function" and a "shared memory" block ...

Is it possible ?

How much is it reasonable to do this for a program ? ( I mean , Will it work well..?)

Emadpres
  • 3,466
  • 2
  • 29
  • 44

4 Answers4

4

For starters, don't mix a thread and fork().

A fork gives you a brand new process, which is a copy of the current process, with the same code segments. As the memory image changes (typically this is due to different behavior of the two processes) you get a separation of the memory images, however the executable code remains the same. Tasks do not share memory unless they use some Inter Process Communication (IPC) primitive.

In contrast a thread is another execution thread of the same task. One task can have multiple threads, and the task memory object are shared among threads, therefore shared data must be accessed through some primitive and synchronization objects that allow you to avoid data corruption.

  • I read it before ! I mean making a myThread() that simulate a process which give similar result to a real thread . – Emadpres Jul 28 '11 at 15:53
  • 1
    @EmAdpres: I'm sorry, could you please clarify. Also, no need to get excited my friend. –  Jul 28 '11 at 15:53
  • @EmAdpres: there are things you will not be able to do with different processes like accessing the same global variable for example... but again, that depends what you mean by "simulating" or "similar result" – Matthieu Jul 28 '11 at 15:59
  • "maybe , we could store result of child-processes somewhere and use it , in other process .. I mean a handmade-thread" Is should be possible.. but tricky ! – Emadpres Jul 28 '11 at 15:59
  • 1
    @EmAdpres: You can always use pipes or file I/O to pass data between processes. However, simulating thread with a fork() is like apples and oranges. They don't mix. That's why I explained the two concepts so you could understand. Maybe if you explained more clearly what exactly you want, we can answer better. –  Jul 28 '11 at 16:00
  • 1
    @EmAdpres: You can use Shared Memory if that is what you are getting at? http://stackoverflow.com/questions/1963642/shared-memory-for-fork –  Jul 28 '11 at 16:02
  • yeah , I mean some tricky stuff like that ! It seems your idea about of that, is close to what i thought , And I got the answer ! ThX !! :D – Emadpres Jul 28 '11 at 16:05
2

Yes, it is possible, but I cannot imagine it being a good idea, and it would be a real pain to test.

If you have a shared heap, and you make sure all semaphores etc. are allocated in the heap, and not the stack, then there's no inherent reason you couldn't do something like it. There would be some tricky differences though.

For example, anything you do in an interrupt handler in a multi-threaded program can change data used by all the threads, while in a forked program, you would have to send multiple interrupts, which would be caught at different times, and might lead to unintended effects.

If you want threading behavior, just use a thread.

John Doucette
  • 4,370
  • 5
  • 37
  • 61
1

AFAIK, fork will create a separate process with its own context, stack and so on. Depends what you mean by "simulating"...

You might want to check this out : http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them

Matthieu
  • 16,103
  • 10
  • 59
  • 86
  • I knew them. maybe , we could store result of child-processes somewhere and use it , in other process .. I mean a handmade-thread :D – Emadpres Jul 28 '11 at 15:57
1

A few of the answers here focus on "don't mix fork and threads". But the way I read your question is: "can you use two different processes, and still communicate quickly and conveniently with shared memory between them, just like how threads have access to each others' memory?"

And the answer is, yes you can, but you have to remember to explicitly mark which memory areas you want shared. You can not just share your variables between the processes. Also, you can communicate this way between processes not related to each other at all. It is not limited to processes forked from each other.

Have a look at shared memory or "shm".

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173