锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
int main(void)
{
pid_t pid=fork();
if(pid==0)
{
int j ;
for(j=0;j<10;j++)
{
printf("child: %d\n",j);
sleep(1);
}
}
else if (pid>0)
{
int i;
for(i=0;i<10;i++)
{
printf("parent: %d\n",i);
sleep(1);
}
}
else
{
fprintf(stderr,"can't fork ,error %d\n",errno);
exit(1);
}
printf("This is the end !");
}
]]>
鍏堣創淇″彿閲忕殑浠g爜.
#include<stdio.h>
#include<sys/time.h>
#define MAX 10
pthread_t thread[2];
pthread_mutex_t mut;
int number=0,i;
void * thread1()
{
printf("thread1: I'm thread 1 \n");
for(i =0;i<MAX ;i++)
{
printf("thread 1: number=%d \n",number);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
sleep(2);
}
printf("thread1: 涓誨嚱鏁板湪絳夋垜瀹屾垚浠誨姟鍚楋紵\n");
pthread_exit(NULL);
}
void * thread2()
{
printf("thread2: I'm thread 2 \n");
for(i =0; i<MAX;i++)
{
printf("thread2 : number=%d\n",number);
pthread_mutex_lock(&mut);
number++;
pthread_mutex_unlock(&mut);
sleep(3);
}
printf("thread2 : 涓誨嚱鏁板湪絳夋垜瀹屾垚浠誨姟涔堬紵\n");
pthread_exit(NULL);
}
void thread_create(void)
{
/*鍒涘緩綰跨▼*/
pthread_create(&thread[0],NULL,thread1,NULL);
printf("綰跨▼1琚垱寤猴紒\n");
pthread_create(&thread[1],NULL,thread2,NULL);
printf("綰跨▼2琚垱寤猴紒\n");
}
void thread_wait(void)
{
/*絳夊緟綰跨▼緇撴潫*/
pthread_join(thread[0],NULL);
printf("綰跨▼1宸茬粡緇撴潫錛乗n");
pthread_join(thread[1],NULL);
printf("綰跨▼2宸茬粡緇撴潫!\n");
}
int main()
{
/*鐢ㄩ粯璁ゅ睘鎬у垵濮嬪寲浜掓枼閿?/
pthread_mutex_init(&mut,NULL);
printf("鎴戞槸涓誨嚱鏁幫紝鎴戞鍦ㄥ垱寤虹嚎紼嬶紒\n");
thread_create();
printf("鎴戞槸涓誨嚱鏁幫紝鎴戞鍦ㄧ瓑寰呯嚎紼嬪畬鎴愪換鍔★紒\n");
thread_wait();
}
鎵ц鐨勭粨鏋滄槸:
thread1: I'm thread 1
thread 1: number=0
綰跨▼1琚垱寤猴紒
thread2: I'm thread 2
thread2 : number=1
綰跨▼2琚垱寤猴紒
鎴戞槸涓誨嚱鏁幫紝鎴戞鍦ㄧ瓑寰呯嚎紼嬪畬鎴愪換鍔★紒
thread 1: number=2
thread2 : number=3
thread 1: number=4
thread 1: number=5
thread2 : number=6
thread 1: number=7
thread2 : number=8
thread 1: number=9
thread2 : number=10
thread1: 涓誨嚱鏁板湪絳夋垜瀹屾垚浠誨姟鍚楋紵
綰跨▼1宸茬粡緇撴潫錛?br>thread2 : 涓誨嚱鏁板湪絳夋垜瀹屾垚浠誨姟涔堬紵
綰跨▼2宸茬粡緇撴潫!
鑰屾垜浠敤鑷棆閿?浠g爜:
* time :2008.4.30
* author:will cao
* Email:sei_michael@126.com
* 鎺㈢儲鑷棆閿佷笌淇″彿閲忕殑鍖哄埆
*/
#include<pthread.h>
#include<stdio.h>
pthread_t thread[2];
pthread_spinlock_t lock ;
#define MAX 10
int number=0,i;
void * thread1()
{
printf ("thread 1 :I began to run !");
for(i=0;i<MAX;i++)
{
printf("thread 1 :number=%d \n",number);
pthread_spin_lock(&lock);
number++;
pthread_spin_unlock(&lock);
}
printf("ok ,I am over !\n");
pthread_exit(NULL);
}
void * thread2 ()
{
printf("thread2 : I start !!!\n");
for(i=0;i<MAX;i++)
{
printf("thread2 : number = %d \n",number);
pthread_spin_lock(&lock);
number++;
pthread_spin_unlock(&lock);
}
printf("thread 2: I am over!!!");
pthread_exit(NULL);
}
void thread_create(void)
{
/*create the threads */
pthread_create(&thread[0],NULL,thread1,NULL);
printf("create the thread 1\n ");
pthread_create(&thread[1],NULL,thread2,NULL);
printf("create the thread 2 \n");
}
void thread_wait(void )
{
/*wait for the thread to be over */
pthread_join(thread[0],NULL);
printf("the thread 1 is over !\n");
pthread_join(thread[1],NULL);
printf("the thread 2 is over ! \n");
}
int main()
{
/* init the spin lock */
pthread_spin_init(&lock,0);
printf("i am the main,and I am creating the threads ");
thread_create();
printf("i am the main,and I am wait for the thread to be over!");
thread_wait();
}
thread 1 :number=1
thread 1 :number=2
thread 1 :number=3
thread 1 :number=4
thread 1 :number=5
thread 1 :number=6
thread 1 :number=7
thread 1 :number=8
thread 1 :number=9
ok ,I am over !
create the thread 1
thread2 : I start !!!
create the thread 2
i am the main,and I am wait for the thread to be over!thread2 : number = 10
thread2 : number = 11
thread2 : number = 12
thread2 : number = 13
thread2 : number = 14
thread2 : number = 15
thread2 : number = 16
thread2 : number = 17
thread2 : number = 18
thread2 : number = 19
thread 2: I am over!!!the thread 1 is over !
the thread 2 is over !
鎬葷粨:浠庤〃闈笂鏉ョ湅,寰堟槑鏄劇殑鍖哄埆鏄綋鎴戜滑鐢ㄧ殑鏄俊鍙烽噺鐨勬椂鍊?榪欎釜鏃跺欐槸鏈夎皟搴︾殑.鍥犱負浠庤繍琛岀粨鏋滀笂鏉ョ湅,涓葷嚎紼嬪湪鍒涘緩鍏朵粬涓や釜綰跨▼鍚?鍏朵粬綰跨▼寮濮嬭繍琛?騫朵笖涓葷嚎紼嬩篃鍦ㄨ繍琛?浣嗘庝箞榪愯榪欎釜鏄棤娉曠‘瀹氱殑,榪欐槸涓涓茍鍙戠殑榪囩▼.
褰撲嬌鐢ㄨ嚜鏃嬮攣鍚?榪欎釜灝變笉涓鏍蜂簡.褰撹繍琛屽埌涓寸晫鍖虹殑鏃跺?瀹冩槸鐩存帴鐨勮繃鍘?涓嶆槸浼氫駭鐢熶竴涓瓑寰?鎴栬呬竴涓皟搴?
涓嶇煡閬撶紪璇戝櫒鏄庝箞緙栬瘧鐨?寰堟兂鐭ラ亾緙栬瘧鍚庝簩榪涘埗浠g爜鏈変粈涔堝尯鍒?浣嗚繖涓ソ鍍忔湁鐐瑰お闅?...涓嶈繃鎴戣寰椾粠榪愯緇撴灉涓婃潵鐪嬭繖涔堝,搴旇宸笉澶氫簡.
]]>
鍐嶆潵鐪嬬湅淇″彿閲?淇″彿閲忕殑瀹炵幇灝變笉榪欒埇綺懼噯浜?濡傛灉浣跨敤涓涓俊鍙烽噺鏉ユ帶鍒朵竴涓復鐣屽尯鐨勮瘽.灝變細鏈夊緢澶氭儏鍐?棣栧厛鏈鏄庢樉鐨勬槸璇昏?鍐欒呴棶棰?鍙互鏈夊涓鑰?鍐欒呭彧鍙互鏈変竴涓?騫朵笖淇″彿閲忕殑瀹炵幇涔熷拰鑷棆閿佹湁鑰呬竴瀹氱殑鍖哄埆.褰撲竴涓俊鍙烽噺涓嶈兘璁塊棶鍚?榪涚▼涓嶄細鍦ㄩ偅閲屽驚鐜?浼氳鐫$湢鎺?褰撲俊鍙烽噺鍙互浣跨敤鐨勬椂鍊?璋冨害鍣ㄤ細浠庡彲浠ヨ皟搴︾殑榪涚▼閫夋嫨涓涓?
鍩烘湰涓婂氨榪欎釜鏍峰瓙.
]]>