I am pretty new to the concepts of inter-process communication, therefore, need some help, I need to implement a multiple producer-consumer problem using message queues in which there is a manager process that calls 5 producers and 5 consumers (call to the respective files i.e producer.c and consumer.c) and along with that, I need to check for deadlocks.
There are two queues represented in the form of 2D-matrix and producer and consumer insert and delete respectively using respective files from the matrice.
The matrix will be in following format :
P1 P2 P3 P4 P5 C1 C2 C3 C4 C5
Q0 : - - - - - - - - - -
Q1 : - - - - - - - - - -
What I have tried:
I have understood most of the concept involved but I'm lacking in the part where I need to call producer.c and consumer.c from the manager process (i.e manager.c). I have implemented my method using fork but it resulted in segmentation fault, any help will be very welcome.
Here's my code.
int main(int argc, char* argv[]){
int dpp, i;
float prob=0.2;
if(argc!=3){
printf("Please run the program with 2 arguments : <DPP_toggle (0 or 1)> <probability> \n"); DPP --> Deadlock Prevention Protocol
exit(1);
}
sscanf(argv[1], "%d", &dpp);
sscanf(argv[2], "%f", &prob);
semid = semget((key_t)SEMAPHORE_KEY, 7, IPC_CREAT|0666);
for(i=0; i<=6; i++){
if(semctl(semid, i, SETVAL, 1) < 0){
printf("Error in initializing semaphore");
exit(EXIT_FAILURE);
}
}
int msgid0 = msgget((key_t)MSGQ0KEY, IPC_CREAT|0666);
int msgid1 = msgget((key_t)MSGQ1KEY, IPC_CREAT|0666);
pid_t pid = fork();
if(pid < 0){
perror("fork()");
return -1;
}
for(int index=0; index<NUM_PRODUCERS; index++){
if(pid > 0){
execlp("./producer", "./producer", index, MSGQ0KEY, MSGQ1KEY, (char *)0);
exit(0);
}
else{
execlp("./consumer", "./consumer", NUM_PRODUCERS+index, MSGQ0KEY, MSGQ1KEY, dpp, prob, (char*)0);
exit(0);
}
}
}