Reputation: 33
How to rewrite this code using processes instead of threads?How can I replace threads with processes, in this file without starting other files manually?Code is not complete but header files are not necessary here.
#include <pthread.h>
#include <semaphore.h>
sem_t p1_p2;
sem_t p2_p3p5;
sem_t p3_p4;
sem_t p4p5_p6;
void* P1(void *args)
{
OS_WORK();
OS_SEM_UP(p1_p2);
}
void* P2(void *args)
{
OS_SEM_DOWN(p1_p2);
OS_WORK();
OS_SEM_UP(p2_p3p5);
OS_SEM_UP(p2_p3p5);
}
void* P3(void *args)
{
OS_SEM_DOWN(p2_p3p5);
OS_CS_WORK();
OS_SEM_UP(p3_p4);
}
void* P4(void *args)
{
OS_SEM_DOWN(p3_p4);
OS_CS_WORK();
OS_SEM_UP(p4p5_p6);
}
void* P5(void *args)
{
OS_SEM_DOWN(p2_p3p5);
OS_CS_WORK();
OS_SEM_UP(p4p5_p6);
}
void* P6(void *args)
{
OS_SEM_DOWN(p4p5_p6);
OS_SEM_DOWN(p4p5_p6);
OS_WORK();
}
int main (int argc, char * argv[])
{
pthread_t p1, p2, p3, p4, p5, p6;
os_base_init(TRUE, TRUE);
OS_SEM_INIT(p1_p2, 0);
OS_SEM_INIT(p2_p3p5, 0);
OS_SEM_INIT(p3_p4, 0);
OS_SEM_INIT(p4p5_p6, 0);
pthread_create(&p6, NULL, P6, NULL);
pthread_create(&p5, NULL, P5, NULL);
pthread_create(&p4, NULL, P4, NULL);
pthread_create(&p3, NULL, P3, NULL);
pthread_create(&p2, NULL, P2, NULL);
pthread_create(&p1, NULL, P1, NULL);
pthread_join(p1, NULL);
pthread_join(p2, NULL);
pthread_join(p3, NULL);
pthread_join(p4, NULL);
pthread_join(p5, NULL);
pthread_join(p6, NULL);
OS_SEM_DESTROY(p1_p2);
OS_SEM_DESTROY(p2_p3p5);
OS_SEM_DESTROY(p3_p4);
OS_SEM_DESTROY(p4p5_p6);
os_base_destroy();
return 0;
}
How to rewrite this code using processes instead of threads?How can I replace threads with processes, in this file without starting other files manually?Code is not complete but header files are not necessary here.
Upvotes: -1
Views: 104
Reputation: 27115
How to rewrite this code using processes instead of threads?
ShadowRanger said, "multiprocess designs are less portable, and generally more expensive."
The thing that distinguishes threads is, they all run in the same virtual address space. It is trivially easy to let several threads access the same global variables or the same heap objects. It is significantly more work to create memory regions that are shared between processes and, to create the means of constructing data structures within those shared regions.
In addition, your program will need to use various inter-process communication (IPC) means to synchronize access to the shared memory. Many multi-process applications forego the shared memory altogether and rely entirely on IPC for communication between different processes. But then, re-structuring a program that previously used shared memory to use IPC instead potentially is a huge task.
On top of all that, the library calls you'll need to make in order use IPC, and optionally, to create a shared memory region, will be different on different operating systems. So, if you need your program to be portable across platforms, that adds a whole 'nother level of complexity.
OS_WORK()
, ...OS_CS_WORK()
There is no general answer to your question. The answer to how you can port your program is going to depend very much on what those functions do and, on how they communicate with each other.
Upvotes: 0