///tell the task scheduler we are done with the SPU tasks void b3PosixThreadSupport::stopThreads() { for(size_t t=0; t < size_t(m_activeThreadStatus.size()); ++t) { b3ThreadStatus& spuStatus = m_activeThreadStatus[t]; // printf("%s: Thread %i used: %ld\n", __FUNCTION__, int(t), spuStatus.threadUsed); spuStatus.m_userPtr = 0; checkPThreadFunction(sem_post(spuStatus.startSemaphore)); checkPThreadFunction(sem_wait(m_mainSemaphore)); printf("destroy semaphore\n"); destroySem(spuStatus.startSemaphore); printf("semaphore destroyed\n"); checkPThreadFunction(pthread_join(spuStatus.thread,0)); if (spuStatus.m_lsMemoryReleaseFunc) { spuStatus.m_lsMemoryReleaseFunc( spuStatus.m_lsMemory); } } printf("destroy main semaphore\n"); destroySem(m_mainSemaphore); printf("main semaphore destroyed\n"); m_activeThreadStatus.clear(); }
///tell the task scheduler we are done with the SPU tasks void PosixThreadSupport::stopSPU() { for(size_t t=0; t < size_t(m_activeSpuStatus.size()); ++t) { btSpuStatus& spuStatus = m_activeSpuStatus[t]; printf("%s: Thread %i used: %ld\n", __FUNCTION__, int(t), spuStatus.threadUsed); destroySem(spuStatus.startSemaphore); checkPThreadFunction(pthread_cancel(spuStatus.thread)); } destroySem(mainSemaphore); m_activeSpuStatus.clear(); }
/* * Clean up the mess */ void cleanUp(void) { printf("Cleaning up...\n"); fflush(stdout); /* detach all shmem segments here */ if(ipc_packet_data.ipc_method) ipc_packet_data.resource_id = status.shm_id; if(finishIPC(&ipc_packet_data) < 0) { ipcerror("mtimesrv::cleanIp():finishIPC() - failed"); printf("Terminating abnormally..."); } if(ipc_packet_data.ipc_method == SHMEM) destroySem(status.sem_id); ipc_packet_data.reqresp = NULL; ipc_packet_data.shmem = NULL; printf("Done...\n"); fflush(stdout); }
static void catchterm(int sig) { if (disconnectFromIPC(MAIN_ID) == ERROR) { destroyIPC(MAIN_ID); } destroySem(sem_id); freeAll(); exit(0); }
int destroyIPC(int id) { close(fd); char *path = malloc(sizeof(char) * MAX_PATH_STR); sprintf(path, "/tmp/socket_id%d", id); unlink(path); free(path); destroySem(sem_id); return EXIT_SUCCESS; }
///tell the task scheduler we are done with the SPU tasks void PosixThreadSupport::stopSPU() { for(size_t t=0; t < size_t(m_activeSpuStatus.size()); ++t) { btSpuStatus& spuStatus = m_activeSpuStatus[t]; printf("%s: Thread %i used: %ld\n", __FUNCTION__, int(t), spuStatus.threadUsed); spuStatus.m_userPtr = 0; checkPThreadFunction(sem_post(spuStatus.startSemaphore)); checkPThreadFunction(sem_wait(mainSemaphore)); printf("destroy semaphore\n"); destroySem(spuStatus.startSemaphore); printf("semaphore destroyed\n"); checkPThreadFunction(pthread_join(spuStatus.thread,0)); } printf("destroy main semaphore\n"); destroySem(mainSemaphore); printf("main semaphore destroyed\n"); m_activeSpuStatus.clear(); }
/* * Disconnect from the server. */ void disconnect(void) { request_t die; static volatile int retries; struct sigaction newact, oldact; sigset_t newmask; requestPrepare(&die); die.command = TERMINATE; /* Setup ACK timout handler */ newact.sa_handler = onAckTimeout; /* Set all signals to blocked in the mask */ sigemptyset(&newact.sa_mask); /* No flags */ newact.sa_flags = 0; /* Install SIGALRM handler */ sigaction(SIGALRM, &newact, &oldact); sigemptyset(&newmask); sigaddset(&newmask, SIGALRM); /* Here we wait for ACK */ /* for(retries = 0; retries < TIMEOUT_RETRIES; retries++)*/ { /* sigsetjmp(jmpbuf, SIGALRM);*/ alarm(TIMEOUT); srvSendReceive(); /* if(semctl(status.sem_id, READER, GETVAL, 0) == -1) continue; else break; */ } if(retries == TIMEOUT_RETRIES) printf("Sever is not responding. Looks like it's down or tremendously busy. I'm giving up...\n"); if(finishIPC(&ipc_packet_data) < 0) ipcerror("Failed to finish properly IPC"); if(ipc_packet_data.ipc_method == SHMEM) destroySem(status.sem_id); }
static void catchint(int signal) { int i; for (i = 0; i < processID->cantPids; i++) { wait(0); // Waits for child processes to abort } printf("Aborting Simulation\n"); freeAll(); if (disconnectFromIPC(MAIN_ID) == ERROR) { destroyIPC(MAIN_ID); } destroySem(sem_id); exit(1); }
int main(int argc, char *argv[]) { initializeSigHandler(); setpgid(0, 0); int i = 0; int ret = 0; pid_t pid; cmpQty = argc - 2; cmp = malloc(sizeof(company*) * cmpQty); map = malloc(sizeof(graph)); sem_id = createSem(SEM_CREAT_KEY, cmpQty + SEM_QTY); setAllSem(sem_id, cmpQty + SEM_QTY, 0); if (initPIDS() == MALLOC_ERROR ) return MALLOC_ERROR; /***PARSER***/ if (cmp == NULL || map == NULL ) return parserError(NULL, NULL, MALLOC_ERROR, NULL); if ((ret = parseFiles(argc, argv, map, cmp)) != EXIT_SUCCESS ) return ret; /***INITIALIZES ARRAYS FOR THE EXECL***/ initializeArray(&semMsg, sem_id); initializeArray(&procQty, ID_QTY + cmpQty); /***MAP AND COMPANIES CREATION***/ if (createIPC(MAIN_ID, cmpQty + ID_QTY) == ERROR ) return ERROR; int map_pid = 0; int io_pid = 0; int mult_pid = 0; if ((pid = fork()) == 0) { execl("io", semMsg, procQty, NULL); exit(1); } else { io_pid = pid; addPid(io_pid); if ((pid = fork()) == 0) { execl("map", semMsg, procQty, NULL); exit(1); } else { map_pid = pid; addPid(map_pid); if ((pid = fork()) == 0) { execl("multitasker", semMsg, procQty, NULL); exit(1); } else { mult_pid = pid; addPid(mult_pid); for (i = 0; i < cmpQty; i++) { if ((pid = fork()) == 0) { char *buf = NULL; int id = CMP_ID + i; initializeArray(&buf, id); execl("company", buf, procQty, semMsg, NULL); } else { addPid(pid); } } } } } upSem(sem_id, MAIN_ID); downSem(sem_id, IO_ID); downSem(sem_id, MULTITASKER_ID); for (i = 0; i < cmpQty; i++) { downSem(sem_id, CMP_ID + i); if (initializeCmp(cmp[i], MAIN_ID, CMP_ID + i) == ERROR ) return ERROR; } downSem(sem_id, MAP_ID); if (initializeMap(map, MAIN_ID, MAP_ID) == ERROR) { return ERROR; } downSem(sem_id, MAP_ID); for (i = 0; i < cmpQty; i++) { wait(0); //Waits for all the companies to finish } kill(mult_pid, SIGTERM); waitpid(mult_pid, 0, 0); kill(map_pid, SIGTERM); waitpid(map_pid, 0, 0); kill(io_pid, SIGTERM); waitpid(io_pid, 0, 0); if (disconnectFromIPC(MAIN_ID) == ERROR) { destroyIPC(MAIN_ID); } destroySem(sem_id); /***FREES***/ freeAll(); return EXIT_SUCCESS; }