How to communicate between two threads or processes
Article Metadata
Contents |
Introduction
The following mechanisms are used to communicate between two threads within a process and also between unrelated processes(created either by a Symbian app or Open C application):
Pipes
Pipes can be used when a process creates child processes or threads and wants to communicate with them. The following steps are needed to do so:
- Declare a global variable with array [2] of data type int.
- Create a pipe.
- Create a thread.
- Read data from the pipe from the parent thread.
- Write data from the child thread.
- Close the read or write descriptor
Named pipes
Named pipes(mkfifo) are used to communicate between threads within a process or between two unrelated processes. The following steps are needed to do so:
- Create a named pipe using mkfifo.
- Create a process or thread and open this pipe in write mode.
- Another process or thread can open the same in read mode.
- Close the read or write descriptor.
Message queues
Message queues are used to communicate between threads within a process or two unrelated processes. The following steps are needed to do so:
- Create a message queue using msgget.
- Create a process or thread and send some message to this message queue.
- Read the message from the main thread.
- Delete the message queue.
Shared memory is used to communicate between threads within a process or two unrelated processes, allowing both to share a given region of memory. The following steps are needed to do so:
- Create a shared memory segment using shmget<tt>.
- Attach the segment to the address space of both processes using <tt style="font-family:monospace;">shmat.
- Synchronize processes to correct access the shared memory.
- Detach the shared memory segment from the address space of the processes using shmdt.


(no comments yet)