JNIEXPORT jstring JNICALL Java_devmap_DevMap_getDevMaps(JNIEnv *env, jclass cls) { jstring content; char *res = ""; char buf[4096]; /* Step 1: open the shm */ int fd = open_shm(); if (fd < 0) { goto out; } /* Step 2: read in the content */ memset(buf, 0, sizeof(buf)); read(fd, buf, 4096); printf("buf %s\n", buf); close_shm(fd); res = buf; out: content = (*env)->NewStringUTF(env, res); return content; }
int main(int argc, char **argv) { int i, rc, status, np, nr_prog_arg, size_structure_shm, size_np_shm; int fd_structure_shm, fd_np_shm; char *prog, *command; char **arguments; void *structure_mem, *np_mem; mqd_t local_queue; char local_queue_name[MAX_NAME_LEN]; if(argc < 4) { printf("Wrong parameters!\n"); return -1; } np = atoi(argv[2]); prog = argv[3]; nr_prog_arg = argc - 4; arguments = calloc(nr_prog_arg + 2, sizeof(char*)); arguments[0] = prog; for(i = 0; i < nr_prog_arg; i++) { arguments[i + 1] = argv[i + 4]; } arguments[i + 1] = NULL; //alloc memory for the mpi_comm_world structure mpi_comm_world = calloc(np, sizeof(struct mpi_comm)); //create the first shared memory for the processes number size_structure_shm = np * sizeof(struct mpi_comm); fd_structure_shm = open_shm(SHM_STRUCTURE_NAME, size_structure_shm, O_CREAT | O_RDWR, &structure_mem); memcpy(structure_mem, mpi_comm_world, size_structure_shm); //create the shared memory for the mpi_comm_world structure size_np_shm = sizeof(int); fd_np_shm = open_shm(SHM_NP_NAME, size_np_shm, O_CREAT | O_RDWR, &np_mem); ((int*)np_mem)[0] = np; //create queues for all the processes for(i = 0; i < np; i ++) { sprintf(local_queue_name, "/%i", i); local_queue = mq_open(local_queue_name, O_CREAT | O_RDWR, 0666, NULL); DIE(local_queue == (mqd_t)-1, "mq_open"); mpi_comm_world[i].local_queue = local_queue; } //create the processes for(i = 0; i < np; i++) { pid_t pid = fork(); switch(pid) { case -1: return EXIT_FAILURE; case 0: ((struct mpi_comm*)structure_mem)[i].pid = getpid(); ((struct mpi_comm*)structure_mem)[i].rank = i; command = calloc(strlen(prog) + 2, sizeof(char)); sprintf(command, "./%s", prog); execvp(command, (char*const*) arguments); free(command); free(arguments); exit(EXIT_FAILURE); default: break; } } free(arguments); for(i = 0; i < np; i++) waitpid(mpi_comm_world[i].pid, &status, 0); //close the shared memories and the queues close_shm(SHM_STRUCTURE_NAME, structure_mem, fd_structure_shm, size_structure_shm); close_shm(SHM_NP_NAME, np_mem, fd_np_shm, size_np_shm); for(i = 0; i < np; i++) { rc = mq_close(mpi_comm_world[i].local_queue); DIE(rc == -1, "mq_close"); sprintf(local_queue_name, "/%i", i); rc = mq_unlink(local_queue_name); DIE(rc == -1, "mq_unlink"); } free(mpi_comm_world); return 0; }