void netcmd_semaphore_release (void) { semaphore_release (&netcmd_semaphore); /* netcmd_semaphore has max count of 1 - safe to close after release */ semaphore_close (&netcmd_semaphore); }
int main(int argc, const char** argv) { semaphore_t *semap; semap = semaphore_open("/tmp/semaphore"); if (semap == NULL) return 1; semaphore_post(semap); semaphore_close(semap); return 0; }
void netcmd_semaphore_close (void) { semaphore_close (&netcmd_semaphore); }
~named_semaphore_wrapper() { if(mp_sem != BOOST_INTERPROCESS_POSIX_SEM_FAILED) semaphore_close(mp_sem); }
~posix_named_semaphore() { if(mp_sem != BOOST_INTERPROCESS_POSIX_SEM_FAILED) semaphore_close(mp_sem); }
static int basic_test(void) { pid_t pid; int fd, sem_id, status, ret = 0; unsigned long i, j, chunk_no = 0, num_chunks = 0; struct write_unit wu; sem_id = semaphore_init(1); if (sem_id < 0) return sem_id; num_chunks = file_size / CHUNK_SIZE; open_rw_flags |= O_DIRECT; open_ro_flags |= O_DIRECT; if (test_flags & BASC_TEST) { fprintf(stdout, "# Prepare file in %lu length.\n", file_size); ret = prep_orig_file_in_chunks(workfile, file_size); if (ret) return ret; } fflush(stderr); fflush(stdout); signal(SIGCHLD, sigchld_handler); fd = open_file(workfile, open_rw_flags); if (fd < 0) return fd; if (test_flags & FIHL_TEST) { fprintf(stdout, "# Reserve a hole by truncating file to %lu.\n", file_size); ret = ftruncate(fd, file_size); if (ret) { ret = errno; fprintf(stderr, "ftruncate faile:%d,%s\n", ret, strerror(ret)); return ret; } } fprintf(stdout, "# Fork %lu processes performing writes.\n", num_children); for (i = 0; i < num_children; i++) { pid = fork(); if (pid < 0) { fprintf(stderr, "Fork process error!\n"); return pid; } if (pid == 0) { srand(getpid()); for (j = 0; j < num_chunks; j++) { if (verbose) fprintf(stdout, " #%d process writes " "#%lu chunk\n", getpid(), chunk_no); if (semaphore_p(sem_id) < 0) { ret = -1; goto child_bail; } if (test_flags & APPD_TEST) chunk_no = j; else chunk_no = get_rand_ul(0, num_chunks - 1); prep_rand_dest_write_unit(&wu, chunk_no); ret = do_write_chunk(fd, wu); if (ret < 0) goto child_bail; ret = log_write(&wu, log); if (ret < 0) goto child_bail; if (semaphore_v(sem_id) < 0) { ret = -1; goto child_bail; } usleep(10000); if (!(test_flags & DSCV_TEST)) continue; /* * Are you ready to crash the machine? */ if ((j > 1) && (j < num_chunks - 1)) { if (get_rand_ul(1, num_chunks) == num_chunks / 2) { if (semaphore_p(sem_id) < 0) { ret = -1; goto child_bail; } fprintf(stdout, "#%d process " "tries to crash the " "box.\n", getpid()); if (system("echo b>/proc/sysrq-trigger") < 0) { fprintf(stderr, "#%d process " "tries to enable sysrq-trigger " "but failed.\n", getpid()); goto child_bail; } } } else if (j == num_chunks - 1) { if (semaphore_p(sem_id) < 0) { ret = -1; goto child_bail; } fprintf(stdout, "#%d process " "tries to crash the " "box.\n", getpid()); if (system("echo b>/proc/sysrq-trigger") < 0) { fprintf(stderr, "#%d process " "tries to enable sysrq-trigger " "but failed.\n", getpid()); goto child_bail; } } } child_bail: if (fd) close(fd); if (ret > 0) ret = 0; exit(ret); } if (pid > 0) child_pid_list[i] = pid; } signal(SIGINT, sigint_handler); signal(SIGTERM, sigterm_handler); for (i = 0; i < num_children; i++) { waitpid(child_pid_list[i], &status, 0); ret = WEXITSTATUS(status); if (ret) { fprintf(stderr, "Child %d exits abnormally with " "RC=%d\n", child_pid_list[i], ret); } } if (fd) close(fd); if (sem_id) semaphore_close(sem_id); return ret; }