//./a.out filename int main(int argc, const char *argv[]) { int shm_id; int sem_id; char *pmsg; int create_flag = 0; if(argc < 2){ fprintf(stderr,"Usage : %s <filename>\n",argv[0]); exit(EXIT_FAILURE); } shm_id = shm_init(argv[1],(char **)&pmsg); sem_id = init_sems(argv[1],2,&create_flag); while(1){ P(sem_id,0); printf("%s\n",pmsg); V(sem_id,1); if(strncmp(pmsg,"quit",4) == 0){ break; } } if(create_flag){ usleep(500); delete_sems(sem_id); } return 0; }
int main(int argc, char ** argv) { key_t key = 99; int shmid; data *setSemaphores; if((shmid = shmget(key, SHM_SIZE, 0644 | IPC_CREAT)) < 0) { printf("SHMGET IS WRONG!!!\n"); return -1; } init_sems(shmid); int pid = fork(); if(pid == 0) { //Child child_attach(shmid); } else { parent_attach(shmid, argc, argv); parent_print(shmid); } /* delete the segment */ if( shmctl(shmid, IPC_RMID, NULL) == -1) { perror("shmctl"); exit(1); } return 0; }
void init_server(){ signal(SIGINT, clean); MSG_RECEIVER = msgget(IPC_PRIVATE, 0666); init_sems(); init_repo(); init_local_repo(); register_new_server(MSG_RECEIVER); create_room(""); printf("SERVER READY, LISTENING ON QUEUE %d\n", MSG_RECEIVER); }
/** Carries out simulation setup and management. @param argc The number of arguments @param argv The array of arguments */ int main(int argc, char *argv[]) { int *results, *shm_states; int i, op_count, proc_id; char *tmp_operator, *cmd; list *commands; operation *shm_operations; if(signal(SIGTERM, &stop_execution) == SIG_ERR) { write_to_fd(2, "Failed to register signal\n"); exit(1); } if(argc != 3) { write_to_fd(2, "Usage: main.x <source file> <results file>\n"); exit(1); } commands = parse_file(argv[1]); processors = atoi(list_extract(commands)); if (processors <= 0) { write_to_fd(2, "Invalid number of processors\n"); exit(1); } write_with_int(1, "Number of processors: ", processors); op_count = list_count(commands); if (op_count == 0) { write_to_fd(2, "No operations provided\n"); exit(1); } write_with_int(1, "Number of operations: ", op_count); results = (int *) malloc(op_count * sizeof(int)); if (results == NULL) { write_to_fd(2, "Failed to allocate results array\n"); exit(1); } init_ipc(2 * processors + 2, processors * sizeof(operation), processors * sizeof(int), 0666 | IPC_CREAT | IPC_EXCL); write_with_int(1, "Created semaphore set with ID ", ipc_id[0]); write_with_int(1, "Created shm for operations with ID ", ipc_id[1]); write_with_int(1, "Created shm for states with ID ", ipc_id[2]); init_sems(processors); shm_operations = (operation *) shm_attach(ipc_id[1]); shm_states = (int *) shm_attach(ipc_id[2]); for (i = 0; i < processors; ++i) shm_states[i] = 0; start_processors(); for (i = 1; list_count(commands) > 0; ++i) { cmd = list_extract(commands); write_with_int(1, "\nOperation #", i); proc_id = atoi(strtok(cmd, " ")); sem_p(2 * processors + 1); if (proc_id-- == 0) { proc_id = find_proc(shm_states); } write_with_int(1, "Waiting for processor ", proc_id + 1); sem_p(2 * proc_id); write_with_int(1, "Delivering operation to processor ", proc_id + 1); if (shm_states[proc_id] != 0) { results[(shm_states[proc_id] + 1) * -1] = shm_operations[proc_id].num1; write_with_int(1, "Previous result: ", shm_operations[proc_id].num1); } shm_operations[proc_id].num1 = atoi(strtok(NULL, " ")); tmp_operator = strtok(NULL, " "); shm_operations[proc_id].op = *tmp_operator; shm_operations[proc_id].num2 = atoi(strtok(NULL, " ")); shm_states[proc_id] = i; write_with_int(1, "Operation delivered. Unblocking processor ", proc_id + 1); sem_v((2 * proc_id) + 1); free(cmd); } list_destruct(commands); for (i = 0; i < processors; ++i) { sem_p(2 * i); write_with_int(1, "\nPassing termination command to processor #", i + 1); if (shm_states[i] != 0) { results[(shm_states[i] + 1) * -1] = shm_operations[i].num1; write_with_int(1, "Last result: ", shm_operations[i].num1); } shm_operations[i].op = 'K'; sem_v((2 * i) + 1); } for (i = 0; i < processors; ++i) if(wait(NULL) == -1) write_to_fd(2, "Wait failed\n"); write_to_fd(1, "\nAll processors exited. Writing output file\n"); write_results(argv[2], results, op_count); free(results); write_to_fd(1, "Closing IPCs\n"); shm_detach((void *) shm_operations); shm_detach((void *) shm_states); close_ipc(); exit(0); }