static int shared_mux(int idx, int mode) /* obtain exclusive access to specified segment */ { flock_t flk; int r; if (0 == shared_init_called) /* delayed initialization */ { if (SHARED_OK != (r = shared_init(0))) return(r); } if (SHARED_INVALID == shared_fd) return(SHARED_NOTINIT); if ((idx < 0) || (idx >= shared_maxseg)) return(SHARED_BADARG); flk.l_type = ((mode & SHARED_RDWRITE) ? F_WRLCK : F_RDLCK); flk.l_whence = 0; flk.l_start = idx; flk.l_len = 1; if (shared_debug) printf(" [mux (%d): ", idx); if (-1 == fcntl(shared_fd, ((mode & SHARED_NOWAIT) ? F_SETLK : F_SETLKW), &flk)) { switch (errno) { case EAGAIN: ; case EACCES: if (shared_debug) printf("again]"); return(SHARED_AGAIN); default: if (shared_debug) printf("err]"); return(SHARED_IPCERR); } } if (shared_debug) printf("ok]"); return(SHARED_OK); }
/** * @brief writes data from the stream to the ring buffer-based shared memory * * @param shm_size the size of the shared memory * @param stream the stream to take as input * * @returns 0 if everything went well and -1 in case of error */ int shared_send(long shm_size, FILE *stream) { shared_t data; int input = EOF; int position = 0; if (shared_init(shm_size, &data) == -1) { /* error is printed by shared_init() */ shared_cleanup(&data); return -1; } do { /* decrement the free space and wait if 0 */ if (sem_wait(data.sem_w_id) == -1) { /* try again after a signal interrupt */ if (errno == EINTR) { continue; } else { warn("sem_wait"); shared_cleanup(&data); return -1; } } /* stop if other process triggered shared_cleanup() */ if (data.shm_buffer[data.shm_size] == EOF) { warnx("Receiver exited unexpectedly"); shared_close(&data); return -1; } input = fgetc(stream); if (input == EOF && ferror(stream) != 0) { warn("fgetc"); shared_cleanup(&data); return -1; } data.shm_buffer[position] = input; position++; /* stay within bounds of the ring buffer */ if (position == shm_size) { position = 0; } /* increment the number of characters */ if (sem_post(data.sem_r_id) == -1) { warn("sem_post"); shared_cleanup(&data); return -1; } } while (input != EOF); shared_close(&data); return 0; }
int main (int argc, char **argv) { int i; char *target = NULL; char *mxdir = NULL; char *propfile = NULL; FILE *file; shared_init(32*1024*1024); err_init(); log_init(LOG_LEVEL); mman_init(); while (1) { char c = getopt(argc, argv, "n:d:p:"); if (c == -1) { break; } switch (c) { case 'd': mxdir = optarg; break; case 'n': target = optarg; break; case 'p': propfile = optarg; break; } } if (argc < 2) { usage(); } else { c = mxc_create(); c->mxdir = mxdir; c->target = target; prop_addFromFile(c->props, propfile); for (i = optind; i < argc; i++) { c->filename = argv[i]; file = fopen(argv[i], "r"); err_assert(file != NULL, err_f("IOError")); yyin = file; yylineno = 1; yyparse(); } mxc_emitIncludes(c); mxc_emitExports(c); mxc_emitInterface(c); mxc_emitCode(c); mxc_emitBuildScript(c); //mxc_free(c); } shared_cleanup(); return 0; }
static void init_subsystems() { shared_init(); heap_init(); signal_init(); process_init(); tls_init(); vfs_init(); dbt_init(); }
vrpn_Tracker_OSVRHackerDevKit::vrpn_Tracker_OSVRHackerDevKit(const char *name, vrpn_Connection *c) : vrpn_Tracker(name, c) , vrpn_Analog(name, c) , vrpn_HidInterface(makeHDKHidAcceptor()) , _wasConnected(false) , _messageCount(0) , _reportVersion(0) , _knownVersion(true) { shared_init(); }
static int shared_check_locked_index(int idx) /* verify that given idx is valid */ { int r; if (0 == shared_init_called) /* delayed initialization */ { if (SHARED_OK != (r = shared_init(0))) return(r); } if ((idx < 0) || (idx >= shared_maxseg)) return(SHARED_BADARG); if (NULL == shared_lt[idx].p) return(SHARED_BADARG); /* NULL pointer, not attached ?? */ if (0 == shared_lt[idx].lkcnt) return(SHARED_BADARG); /* not locked ?? */ if ((SHARED_ID_0 != (shared_lt[idx].p)->s.ID[0]) || (SHARED_ID_1 != (shared_lt[idx].p)->s.ID[1]) || (BLOCK_SHARED != (shared_lt[idx].p)->s.tflag)) /* invalid data in segment */ return(SHARED_BADARG); return(SHARED_OK); }
/** * Initialize/terminate DLL at load/unload. */ unsigned long _System _DLL_InitTerm(unsigned long hModule, unsigned long ulFlag) { // Make sure ghModule is initialized, TRACE needs it. if (ghModule == NULLHANDLE) ghModule = hModule; TRACE("hModule %lx, ulFlag %lu\n", hModule, ulFlag); switch (ulFlag) { /* * InitInstance. Note that this one is NOT called in a forked child — it's * assumed that the DLLs are already initialized in the parent and the child * receives an already initialized copy of DLL data. However, in some cases * this is not actually true (examples are OS/2 file handles, semaphores and * other resources that require special work besides data segment duplication * to be available in the child process) and LIBCx is one of these cases. * A solution here is to call shared_init() from a fork completion callback * (see forkCompletion() below). */ case 0: { if (_CRT_init() != 0) return 0; __ctordtorInit(); shared_init(); break; } /* * TermInstance. Called for everybody including forked children. We don't * call shared_term() from here at all and prefer a process exit hook * instead (see its description below). */ case 1: { __ctordtorTerm(); _CRT_term(); break; } default: return 0; } /* Success */ return 1; }
static void forkCompletion(void *pvArg, int rc, __LIBC_FORKCTX enmCtx) { pid_t pidParent = (pid_t)pvArg; /* * It's safe to use LIBC again. */ gInFork = FALSE; if (enmCtx != __LIBC_FORK_CTX_CHILD) return; /* * Reset the log instance in the child process to cause a reinit. Note that we * only do it when logging to a file. For console logging it's accidentally * fine to just leave it as is because the console handles in the child * process are identical to the parent (even the one we create in the dirty * hack in get_log_instance() with DosDupHandle as it's inherited by default). */ if (!gLogToConsole) { /* Free the instance we inherit from the parent */ if (gLogInstance) free(gLogInstance); gLogInstanceState = 0; gLogInstance = NULL; } gSeenAssertion = FALSE; /* * Initialize LIBCx in the forked child (note that for normal children this is * done in _DLL_InitTerm()). */ shared_init(); }
/** * @brief prints data from the ring buffer-based shared memory * * @param shm_size the size of the shared memory * @param stream the stream to take as output * * @returns 0 if everything went well and -1 in case of error */ int shared_receive(long shm_size, FILE *stream) { shared_t data; int output = EOF; int position = 0; if (shared_init(shm_size, &data) == -1) { /* error is printed by shared_init() */ shared_cleanup(&data); return -1; } do { /* decrement the number of characters and wait if 0 */ if (sem_wait(data.sem_r_id) == -1) { /* try again after a signal interrupt */ if (errno == EINTR) { continue; } else { warn("sem_wait"); shared_cleanup(&data); return -1; } } /* stop if other process triggered shared_cleanup() */ if (data.shm_buffer[data.shm_size] == EOF) { warnx("Sender exited unexpectedly"); shared_close(&data); return -1; } output = data.shm_buffer[position]; position++; if (output != EOF) { if (fputc(output, stream) == EOF) { warn("fputc"); shared_cleanup(&data); return -1; } } /* stay within bounds of the ring buffer */ if (position == shm_size) { position = 0; } /* increment the free space */ if (sem_post(data.sem_w_id) == -1) { warn("sem_post"); shared_cleanup(&data); return -1; } } while (output != EOF); /* force a write of the possibly buffered stream */ if (fflush(stream) == EOF) { warn("fflush"); return -1; } shared_cleanup(&data); return 0; }
int main(int argc, char **argv) { int cmdok, listmode, longlistmode, recovermode, deletemode, id; int status; char *address; listmode = longlistmode = recovermode = deletemode = 0; id = -1; cmdok = 1; switch (argc) { case 1: listmode = 1; break; case 2: if (0 == strcmp("-l", argv[1])) longlistmode = 1; else if (0 == strcmp("-r", argv[1])) recovermode = 1; else if (0 == strcmp("-d", argv[1])) deletemode = 1; else cmdok = 0; break; case 3: if (0 == strcmp("-r", argv[1])) recovermode = 1; else if (0 == strcmp("-d", argv[1])) deletemode = 1; else { cmdok = 0; /* signal invalid cmd line syntax */ break; } if (1 != sscanf(argv[2], "%d", &id)) cmdok = 0; break; default: cmdok = 0; break; } if (0 == cmdok) { printf("usage :\n\n"); printf("smem - list all shared memory segments\n"); printf("\t!\tcouldn't obtain RDONLY lock - info unreliable\n"); printf("\tIdx\thandle of shared memory segment (visible by application)\n"); printf("\tKey\tcurrent system key of shared memory segment. Key\n"); printf("\t\tchanges whenever shmem segment is reallocated. Use\n"); printf("\t\tipcs (or ipcs -a) to view all shmem segments\n"); printf("\tNproc\tnumber of processes attached to segment\n"); printf("\tSize\tsize of shmem segment in bytes\n"); printf("\tFlags\tRESIZABLE - realloc allowed, PERSIST - segment is not\n"); printf("\t\tdeleted after shared_free called by last process attached\n"); printf("\t\tto it.\n"); printf("smem -d - delete all shared memory segments (may block)\n"); printf("smem -d id - delete specific shared memory segment (may block)\n"); printf("smem -r - unconditionally reset all shared memory segments\n\t\t(does not block, recovers zombie handles left by kill -9)\n"); printf("smem -r id - unconditionally reset specific shared memory segment\n"); } if (shared_init(0)) { printf("couldn't initialize shared memory, aborting ...\n"); return(10); } if (listmode) shared_list(id); else if (recovermode) shared_recover(id); else if (deletemode) shared_uncond_delete(id); for (id = 0; id <16; id++) { status = shared_getaddr(id, &address); if (!status)printf("id, status, address %d %d %ld %.30s\n", id, status, address, address); } return(0); }
int main(int argc, char** argv) { if (argc < 2) err("usage : ./obj portno"); int sfd[P]; int i, nsfd, fd = -1; int port = atoi(argv[1]); char buf[M]; fd_set master, test; FD_ZERO(&master); FD_ZERO(&test); shared_init(); platform_init(port); for (i = 0; i < P; i++) { sfd[i] = tcpsocket_bind(port + i); printf("Station %d listening on port : %d\n", i, port + i); if (fd < sfd[i]) fd = sfd[i]; FD_SET(sfd[i], &master); } while (1) { test = master; select(fd + 1, &test, NULL, NULL, NULL); for (i = 0; i < P; i++) { if (FD_ISSET(sfd[i], &test)) { nsfd = tcp_accept(sfd[i]); break; } } if (i != P) { printf("Train arrived on station - %d\n", i); for (i = 0; i < P; i++) { if (available(i)) { printf("Platform %d is available\n", i + 1); sprintf(buf, "%d", port + P + i); write(nsfd, buf, M); sleep(1); sprintf(buf, "pkill -SIGUSR1 p%d", i + 1); system(buf); break; } } if (i == P) { write(nsfd, "-1", 2); } close(nsfd); } } }
int main() { pthread_t *threads; pthread_attr_t attr; thread_data_t *data; int i; shared_obj_t sc; shared_init(&sc,-1); //global barrier #ifdef USE_SenseBarrier printf("SenseBarrier\n"); barrier_t barrier; barrier_init(&barrier); #elif defined(USE_TreeBarrier) printf("TreeBarrier\n"); //radix = (int)(sqrt_my((int)num_thread))+1; radix = 10; num_backup = num_thread; TreeBarrier_t barrier; TreeBarrier_init(&barrier); #elif defined(USE_StaticTreeBarrier) printf("StaticTreeBarrier\n"); radix = 3; //radix = (int)(2*sqrt_my((int)num_thread)); StaticTreeBarrier_t barrier; StaticTreeBarrier_init(&barrier); #endif //initialize the data which will be passed to the threads if ((data = (thread_data_t *)malloc(num_thread * sizeof(thread_data_t))) == NULL) { perror("malloc"); exit(1); } if ((threads = (pthread_t *)malloc(num_thread * sizeof(pthread_t))) == NULL) { perror("malloc"); exit(1); } pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE); //set the data for each thread and create the threads for ( i = 0; i < num_thread; i++) { data[i].thread_id = i; data[i].barrier = &barrier; data[i].sc = ≻ if (pthread_create(&threads[i], &attr, test_thread, (void *)(&data[i])) != 0) { fprintf(stderr, "Error creating thread\n"); exit(1); } } pthread_attr_destroy(&attr); /* Wait for thread completion */ for ( i = 0; i < num_thread; i++) { if (pthread_join(threads[i], NULL) != 0) { fprintf(stderr, "Error waiting for thread completion\n"); exit(1); } } free(threads); free(data); #ifdef USE_SenseBarrier #elif defined(USE_TreeBarrier) TreeBarrier_destroy(); #elif defined(USE_StaticTreeBarrier) StaticTreeBarrier_destroy(); #endif return 0; }
int shared_initDefault(){ return shared_init(32*1024*1024); }
/** * Undoes the effect of force_libcx_init(). Used in some tests. */ void force_libcx_init() { shared_init(); }
static int _f90io_ldr_init(__INT_T *unit, /* unit number */ __INT_T *rec, /* record number for direct access I/O */ __INT_T *bitv, /* same as for ENTF90IO(open_) */ __INT_T *iostat) /* same as for ENTF90IO(open_) */ { int i; G *tmp_gbl; save_gbl(); __fortio_errinit03(*unit, *bitv, iostat, "list-directed read"); allocate_new_gbl(); fcb = __fortio_rwinit(*unit, FIO_FORMATTED, rec, 0 /*read*/); if (fcb == NULL) { if (fioFcbTbls.eof) return EOF_FLAG; /* TBD - does there need to be fioFcbTbls.eor */ return ERR_FLAG; } rec_len = fcb->reclen; internal_file = FALSE; gbl->decimal = fcb->decimal; /* check if recursive io on same external file */ tmp_gbl = NULL; if (gbl_avl > 1) { for (i = gbl_avl - 2; i >= 0; --i) { if (gbl_head[i].fcb == fcb) { tmp_gbl = &gbl_head[i]; break; } } } if (tmp_gbl) { gbl->same_fcb = tmp_gbl; gbl->same_fcb_idx = i; gbl->blank_zero = tmp_gbl->blank_zero; gbl->pad = tmp_gbl->pad; gbl->decimal = tmp_gbl->decimal; gbl->round = tmp_gbl->round; accessed = tmp_gbl->accessed; byte_cnt = tmp_gbl->byte_cnt; prev_tkntyp = tmp_gbl->prev_tkntyp; repeat_cnt = tmp_gbl->repeat_cnt; n_irecs = tmp_gbl->n_irecs; rec_len = tmp_gbl->rec_len; gbl_dtype = tmp_gbl->gbl_dtype; in_recp = tmp_gbl->in_recp; internal_file = tmp_gbl->internal_file; if (tmp_gbl->rbuf_size > rbuf_size) { if (rbufp != rbuf) rbufp = realloc(rbufp, tmp_gbl->rbuf_size); else rbufp = malloc(tmp_gbl->rbuf_size); rbuf_size = tmp_gbl->rbuf_size; } else { rbufp = rbuf; } memcpy(rbufp, tmp_gbl->rbufp, tmp_gbl->rbuf_size); if (tmp_gbl->currc) { currc = rbufp + (tmp_gbl->currc - tmp_gbl->rbufp); } else { currc = NULL; } comma_seen = FALSE; return 0; } else { gbl->same_fcb = tmp_gbl; gbl->same_fcb_idx = 0; fcb->skip = 0; } shared_init(); return 0; }
int shared_malloc(long size, int mode, int newhandle) /* return idx or SHARED_INVALID */ { int h, i, r, idx, key; union semun filler; BLKHEAD *bp; if (0 == shared_init_called) /* delayed initialization */ { if (SHARED_OK != (r = shared_init(0))) return(r); } if (shared_debug) printf("malloc (size = %ld, mode = %d):", size, mode); if (size < 0) return(SHARED_INVALID); if (-1 == (idx = shared_get_free_entry(newhandle))) return(SHARED_INVALID); if (shared_debug) printf(" idx=%d", idx); for (i = 0; ; i++) { if (i >= shared_range) /* table full, signal error & exit */ { shared_demux(idx, SHARED_RDWRITE); return(SHARED_INVALID); } key = shared_kbase + ((i + shared_get_hash(size, idx)) % shared_range); if (shared_debug) printf(" key=%d", key); h = shmget(key, shared_adjust_size(size), IPC_CREAT | IPC_EXCL | shared_create_mode); if (shared_debug) printf(" handle=%d", h); if (SHARED_INVALID == h) continue; /* segment already accupied */ bp = (BLKHEAD *)shmat(h, 0, 0); /* try attach */ if (shared_debug) printf(" p=%p", bp); if (((BLKHEAD *)SHARED_INVALID) == bp) /* cannot attach, delete segment, try with another key */ { shmctl(h, IPC_RMID, 0); continue; } /* now create semaphor counting number of processes attached */ if (SHARED_INVALID == (shared_gt[idx].sem = semget(key, 1, IPC_CREAT | IPC_EXCL | shared_create_mode))) { shmdt((void *)bp); /* cannot create segment, delete everything */ shmctl(h, IPC_RMID, 0); continue; /* try with another key */ } if (shared_debug) printf(" sem=%d", shared_gt[idx].sem); if (shared_attach_process(shared_gt[idx].sem)) /* try attach process */ { semctl(shared_gt[idx].sem, 0, IPC_RMID, filler); /* destroy semaphore */ shmdt((char *)bp); /* detach shared mem segment */ shmctl(h, IPC_RMID, 0); /* destroy shared mem segment */ continue; /* try with another key */ } bp->s.tflag = BLOCK_SHARED; /* fill in data in segment's header (this is really not necessary) */ bp->s.ID[0] = SHARED_ID_0; bp->s.ID[1] = SHARED_ID_1; bp->s.handle = idx; /* used in yorick */ if (mode & SHARED_RESIZE) { if (shmdt((char *)bp)) r = SHARED_IPCERR; /* if segment is resizable, then detach segment */ shared_lt[idx].p = NULL; } else { shared_lt[idx].p = bp; } shared_lt[idx].tcnt = 1; /* one thread using segment */ shared_lt[idx].lkcnt = 0; /* no locks at the moment */ shared_lt[idx].seekpos = 0L; /* r/w pointer positioned at beg of block */ shared_gt[idx].handle = h; /* fill in data in global table */ shared_gt[idx].size = size; shared_gt[idx].attr = mode; shared_gt[idx].semkey = key; shared_gt[idx].key = key; shared_gt[idx].nprocdebug = 0; break; } shared_demux(idx, SHARED_RDWRITE); /* hope this will not fail */ return(idx); }