How can I declare a 2D array in shared memory?
Posted on 2006-03-21
I want to know how can I declare a 2D integer array and then store it into shared memory and then how to pass it to child processes. I have done this for a regular integer but I do not know how to do it for a 2D array.
myshmid = shmget (IPC_PRIVATE, sizeof(int), IPC_CREAT | 0600);
if (myshmid == -1)
shm_addr = shmat(myshmid, 0, 0);
times_written = (int*) shm_addr;
for(i = 0; i < pno; i++)
if ((PID = fork()) == 0)
printf("Creating producer %d\n", i + 1);
producer(i + 1, times_written);
The producer method is as follows:
void producer(int thread_num, int * times_written)
printf("PRODUCER #%d\n", thread_num);
*times_written = *times_written + 1;
printf("times_written is: %d\n", *times_written);
The above works well for a signle integer.
High priority question. 500 points.