差分

移動先: 案内検索

プロセス管理

164 バイト追加, 2016年4月10日 (日) 12:38
/* スレッド */
fork版
<syntaxhighlight lang='C' line="1" >#include <sys/types.h> #include <unistd.h> #include <stdio.h> main() { int i; for(i=0;i<1000;i++) { if ( fork() == 0 ) { fprintf(stderr,"."); _exit(0); } wait(); } printf("%d\n",i); }</syntaxhighlight><pre class="bash">
% cc -O fork.c -o f
</pre>
thread版
<syntaxhighlight lang='C' line="1" >#include <pthread.h> #include <stdlib.h> #include <stdio.h> void *func() { fprintf(stderr,"."); } int main(void) { pthread_t t; int i; for(i=0 ; i < 1000 ; i++) { pthread_create(&t,NULL,func,NULL); pthread_join ( t, NULL ); } printf("%d\n",i); }</syntaxhighlight><pre class="bash">
% cc -O thread.c -lpthread -o t
</pre>
出力
<pre class="bash">
% time ./t
...........(続く)....1000
user 0m0.xxxs
sys 0m0.xxxs
</pre>