void init() { signal(SIGUSR1, &signal1); printf("[Init][Pid: %d] Scheduler Testing Program\n", sched_getpid()); sched_nice(-20); for (int i = 0; i < 15; i++) { if (sched_fork() == 0) { printf("[Child][Pid: %d] Parent Pid: %d\n", sched_getpid(), sched_getppid()); sched_nice(-19 + i); struct timespec start, stop; clock_gettime(CLOCK_REALTIME, &start); for (long int j = 0; j < 1000000000; j++); clock_gettime(CLOCK_REALTIME, &stop); printf("[Child][Pid: %d] Execution Complete. Took %ld Seconds.\n", sched_getpid(), (long int)(stop.tv_sec - start.tv_sec)); sched_exit(i); printf("[Child][Pid: %d] This Will Never Get Executed\n", sched_getpid()); } } printf("[Init][Pid: %d] Process Information\n", sched_getpid()); sched_ps(); for (int i = 0; i < 15; i++) { int returncode; int cc = sched_wait(&returncode); printf("[Init][Pid: %d] Child Returned [%d] With Exit Code [%d]\n", sched_getpid(), cc, returncode); } int returncode; int cc = sched_wait(&returncode); printf("[Init][Pid: %d] Calling Wait With No Children Returns [%d]\n", sched_getpid(), cc); sched_sleep(&wait1); for (int i = 0; i < 15; i++) { if (sched_fork() == 0) { printf("[Child][Pid: %d] Parent Pid: %d\n", sched_getpid(), sched_getppid()); sched_nice(-19 + i); if (i % 2 == 1) sched_sleep(&wait1); else sched_sleep(&wait2); printf("[Child][Pid: %d] Execution Complete.\n", sched_getpid()); sched_exit(i); } } for (int i = 0; i < 1000000000; i++); printf("[Init][Pid: %d] Process Information\n", sched_getpid()); sched_ps(); printf("Wakeup 2\n"); sched_wakeup(&wait2); printf("Wakeup 1\n"); sched_wakeup(&wait1); for (int i = 0; i < 15; i++) { int returncode; int cc = sched_wait(&returncode); printf("[Init][Pid: %d] Child Returned [%d] With Exit Code [%d]\n", sched_getpid(), cc, returncode); } printf("[Init][Pid: %d] Exiting Testing Program. Passing Control Back To Idle\n", sched_getpid()); sched_exit(0); }
static int tty_blockin_output(struct tty *t, char ch) { struct idesc_wait_link iwl; int ret; idesc_wait_init(&iwl, POLLOUT | POLLERR); do { if (tty_output(t, ch)) return 0; if (!t->idesc) return -EBADF; ret = idesc_wait_prepare(t->idesc, &iwl); if (!ret) { mutex_unlock(&t->lock); tty_out_wake(t); ret = sched_wait(); mutex_lock(&t->lock); } idesc_wait_cleanup(t->idesc, &iwl); } while (!ret); return ret; }
void parent() { fprintf(stdout, "Process %d with parent %d start\n", sched_getpid(), sched_getppid()); int i, code; for (i = -2; i < 2; i += 1) { sched_wait(&code); fprintf(stdout, "Process %d exited\n", code); } return; }
void parent() { fprintf(stderr,"parent hello world my pid is %d and my parent is %d\n",sched_getpid(),sched_getppid()); long long i; for (i = 0; i < 1000000000; i++) { if (i%100000000==0) { fprintf(stderr,"pid=%d, i=%lld, ticks=%d\n",sched_getpid(),i,sched_gettick()); } } int ret; sched_wait(&ret); printf("one child returned %d\n",ret); sched_wait(&ret); printf("another child returned %d\n",ret); sched_exit(0); }
/* join worker threads (called when Lua exits). not joining workers causes a race condition since lua_close unregisters dynamic libs with dlclose and thus libpthreads can be unloaded while there are workers that are still alive. */ void sched_join_workers( void ) { lua_State *L = luaL_newstate(); const char *wtb = "workerstbcopy"; /* wait for all running lua processes to finish */ sched_wait(); /* initialize new state and create table to copy worker ids */ lua_newtable( L ); lua_setglobal( L, wtb ); lua_getglobal( L, wtb ); pthread_mutex_lock( &mutex_sched ); /* determine remaining active worker threads and copy their ids */ lua_getglobal( workerls, LUAPROC_SCHED_WORKERS_TABLE ); lua_pushnil( workerls ); while ( lua_next( workerls, -2 ) != 0 ) { lua_pushlightuserdata( L, lua_touserdata( workerls, -2 )); lua_pushboolean( L, TRUE ); lua_rawset( L, -3 ); /* pop value, leave key for next iteration */ lua_pop( workerls, 1 ); } /* pop workers copy table name from stack */ lua_pop( L, 1 ); /* set all workers to be destroyed */ destroyworkers = workerscount; /* wake workers up */ pthread_cond_signal( &cond_wakeup_worker ); pthread_mutex_unlock( &mutex_sched ); /* join with worker threads (read ids from local table copy ) */ lua_getglobal( L, wtb ); lua_pushnil( L ); while ( lua_next( L, -2 ) != 0 ) { // pthread_join(( pthread_t )lua_touserdata( L, -2 ), NULL ); pthread_join(get_thread(L, -2), NULL); /* pop value, leave key for next iteration */ lua_pop( L, 1 ); } lua_pop( L, 1 ); lua_close( workerls ); lua_close( L ); }
void async_sleep_relative(long millisecs) { // Convert to an absolute timeout struct timespec now; int result = clock_gettime(CLOCK_REALTIME, &now); assert(result == 0); now.tv_sec += millisecs / 1000L; now.tv_nsec += (millisecs % 1000L) * 1000000L; if (now.tv_nsec > 1000000000L) { now.tv_nsec -= 1000000000L; now.tv_sec++; } sched_wait(&now); }
void mutex_lock(mutex_t *m, const char *file, const char *func, int line, const char *descr) { // KASSERT(CIPL==0); spinlock_lock(&m->mtx_slock); if ( atomic_change_int(&m->mtx_locked, MUTEX_LOCKED) == MUTEX_UNLOCKED) { m->mtx_owner = curthread; spinlock_unlock(&m->mtx_slock); } else { list_insert_tail(&m->mtx_locking, curthread); int x = spltty(); spinlock_unlock(&m->mtx_slock); sched_wait(file,func,line,descr); splx(x); } }
/* wait until there are no more active lua processes */ static int luaproc_wait( lua_State *L ) { sched_wait(); return 0; }
void async_sleep_absolute(const struct timespec* time) { sched_wait(time); }