JM プロジェクトは、主に Linux 関連のマニュアルページの日本語ページを翻訳・公開しています。
... text (), swapcontext () の使用方法の例を示すものである。 このプログラムを実行すると、以下のような出力が得られる: $ ./a.out main: swapcontext(&uctx_main, &uctx_func2) func2: started func2: swapcontext(&uctx_func2, &uctx_func1) func1: started func1: sw ... apcontext(&uctx_func1, &uctx_func2) func2: returning func1: returning main: exiting プログラムのソース #include < ucontext.h > #include < stdio.h > #include < stdlib.h > static ucontext_t uctx_main ... c1)\n"); if (swapcontext(&uctx_func2, &uctx_func1) == -1) handle_error("swapcontext"); printf("func2: returning\n"); } int main(int argc, char *argv[]) { char func1_stack[16384]; char func2_stack[16384]; if (getcontext(&uctx_func1) == -1) handle_erro ... ... (3) を使って 2 つのスレッドが消費した スレッド毎の CPU 時間を取得する。 下記のシェルのセッションは実行例である。 $ ./a.out Main thread sleeping Subthread starting infinite loop Main thread consuming some CPU time... Process total CPU time: 1.368 Main ... (cid, &ts) == -1) handle_error("clock_gettime"); printf("%4jd.%03ld\n", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000); } int main(int argc, char *argv[]) { pthread_t thread; clockid_t cid; int s; s = pthread_create(&thread, NULL, thread_start, NULL); i ... f (s != 0) handle_error_en(s, "pthread_create"); printf("Main thread sleeping\n"); sleep (1); printf("Main thread consuming some CPU time...\n"); for (int j = 0; j < 2000000; j++) getp ... ... text (), swapcontext () の使用方法の例を示すものである。 このプログラムを実行すると、以下のような出力が得られる: $ ./a.out main: swapcontext(&uctx_main, &uctx_func2) func2: started func2: swapcontext(&uctx_func2, &uctx_func1) func1: started func1: sw ... apcontext(&uctx_func1, &uctx_func2) func2: returning func1: returning main: exiting プログラムのソース #include < ucontext.h > #include < stdio.h > #include < stdlib.h > static ucontext_t uctx_main ... c1)\n"); if (swapcontext(&uctx_func2, &uctx_func1) == -1) handle_error("swapcontext"); printf("func2: returning\n"); } int main(int argc, char *argv[]) { char func1_stack[16384]; char func2_stack[16384]; if (getcontext(&uctx_func1) == -1) handle_erro ... ... する。 $ su # Need privilege to set real-time scheduling policies Password: # ./a.out -mf10 -ar20 -i e Scheduler settings of main thread policy=SCHED_FIFO, priority=10 Scheduler settings in 'attr' policy=SCHED_RR, priority=20 inheritsched is EXPLICIT S ... わりに呼び出したスレッドからスケジューリング属性を取得する ことを意味する。 # ./a.out -mf10 -ar20 -i i Scheduler settings of main thread policy=SCHED_FIFO, priority=10 Scheduler settings in 'attr' policy=SCHED_RR, priority=20 inheritsched is INHERIT Sc ... ttribute to\n"); fpe(" 'explicit' or 'inherit'\n"); fpe("-m<policy><prio> Set scheduling policy and priority on\n"); fpe(" main thread before pthread_create() call\n"); exit(EXIT_FAILURE); } static int get_policy(char p, int *policy) { switch (p) { c ... ... する。 $ su # Need privilege to set real-time scheduling policies Password: # ./a.out -mf10 -ar20 -i e Scheduler settings of main thread policy=SCHED_FIFO, priority=10 Scheduler settings in 'attr' policy=SCHED_RR, priority=20 inheritsched is EXPLICIT S ... わりに呼び出したスレッドからスケジューリング属性を取得する ことを意味する。 # ./a.out -mf10 -ar20 -i i Scheduler settings of main thread policy=SCHED_FIFO, priority=10 Scheduler settings in 'attr' policy=SCHED_RR, priority=20 inheritsched is INHERIT Sc ... ttribute to\n"); fpe(" 'explicit' or 'inherit'\n"); fpe("-m<policy><prio> Set scheduling policy and priority on\n"); fpe(" main thread before pthread_create() call\n"); exit(EXIT_FAILURE); } static int get_policy(char p, int *policy) { switch (p) { c ... ... 。以下のシェルセッションはこのプログラムを実行した際の実行例である。 $ ./a.out thread_func(): started; cancellation disabled main(): sending cancellation request thread_func(): about to enable cancellation main(): thread was canceled プログラムのソース ... ld get canceled while we sleep */ /* Should never get here */ printf("thread_func(): not canceled!\n"); return NULL; } int main(void) { pthread_t thr; void *res; int s; /* Start a thread and then send it a cancellation request */ s = pthread_create(& ... unc, NULL); if (s != 0) handle_error_en(s, "pthread_create"); sleep (2); /* Give thread a chance to get started */ printf("main(): sending cancellation request\n"); s = pthread_cancel(thr); if (s != 0) handle_error_en(s, "pthread_cancel"); /* Join wi ... ... wait(set, &sig); if (s != 0) handle_error_en(s, "sigwait"); printf("Signal handling thread got signal %d\n", sig); } } int main(int argc, char *argv[]) { pthread_t thread; sigset_t set; int s; /* Block SIGQUIT and SIGUSR1; other threads created by ma ... read_sigmask"); s = pthread_create(&thread, NULL, &sig_thread, &set); if (s != 0) handle_error_en(s, "pthread_create"); /* Main thread carries on to create other threads and/or do other work */ pause(); /* Dummy pause so we can test program */ } 関連 ... ... envp array must be terminated by a NULL pointer. The argument vector and environment can be accessed by the new program's main function, when it is defined as: int main(int argc, char *argv[], char *envp[]) Note, however, that the use of a third arg ... ument to the main function is not specified in POSIX.1; according to POSIX.1, the environment should be accessed via the external variable e ... (2)) that is in force at the time of the execve () call. (Architectures with no memory management unit are excepted: they maintain the limit that was in effect before kernel 2.6.23.) This change allows programs to have a much larger argument and/or ... ... c void * thread_func(void *arg) { int tn = (int) arg; /* The multiplier '(2 + tn)' ensures that each thread (including the main thread) allocates a different amount of memory */ for (int j = 0; j < numBlocks; j++) if (malloc(blockSize * (2 + tn)) == ... NULL) errExit("malloc-thread"); sleep(100); /* Sleep until main thread terminates */ return NULL; } int main(int argc, char *argv[]) { int sleepTime; if (argc < 4) { fprintf(stderr, "%s ... xes, and therefore additional arenas won't be allocated (see malloc (3)). */ if (sleepTime > 0) sleep(sleepTime); } /* The main thread also allocates some memory */ for (int j = 0; j < numBlocks; j++) if (malloc(blockSize) == NULL) errExit("malloc"); ... ... 特定の状況では free (3) により自動的に呼び出される。 mallopt (3) の M_TOP_PAD と M_TRIM_THRESHOLD の議論を参照。 Only the main heap (using sbrk (2)) honors the pad argument; thread heaps do not. Since glibc 2.8 this function frees memory in all aren ... as and in all chunks with whole free pages. Before glibc 2.8 this function only freed memory at the top of the heap in the main arena. 関連項目 sbrk (2), malloc (3), mallopt (3) この文書について この man ページは Linux man-pages プロジェクトのリリース ... 日本語の文書やマニュアルを配布している個人/プロジェクトへのリンクです。