#include #include #include #include /* For mode constants */ #include /* For O_* constants */ #include #include #include #include "stencil.h" double *cur_step; double *next_step; struct shared_memory_t *p_shm = NULL; /* initialize matrixes */ void init() { int shm_id = shm_open(KEY, O_RDWR, 0666); assert(shm_id>=0); /* set the size of the shared memory segment */ assert(ftruncate(shm_id, SHM_SIZE) >= 0); /* map the shared memory segment in the current address space */ p_shm = mmap(NULL, SHM_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, shm_id, 0); assert(p_shm>=0); cur_step = p_shm->cur_step; next_step = p_shm->next_step; } void do_print() { int i; for(i=0;istep_produced); print_matrix(cur_step); sem_post(&p_shm->step_consumed); sleep(1); double *tmp = cur_step; cur_step = next_step; next_step = tmp; } } int main(int argc, char** argv) { init(); do_print(); return 0; }