test1.c

来自「unix下多进程编程实例」· C语言 代码 · 共 50 行

C
50
字号
#include <pthread.h>

typedef struct _song{
	int i;
	int j;
} SONG;

void* thread1();
void* thread2();
main()
{
        pthread_t       thd1,thd2;
        pthread_attr_t  thdattr1,thdattr2;
        int iLoop,thd;
		SONG sSong;

		sSong.i=1;
		sSong.j=2;
	thd=pthread_self();
	printf("thd0=%d\n",thd);		
        pthread_attr_init(&thdattr1);
        pthread_attr_init(&thdattr2);
        pthread_create(&thd1,&thdattr1,thread1,&sSong);
        printf("create thread 1 is %d\n",thd1);
        pthread_create(&thd2,&thdattr2,thread2,NULL);
        printf("create thread 2 is %d\n",thd2);
        for(iLoop=0;iLoop<10;iLoop++){
                printf("this is main %d\n",iLoop);
                sleep(3);
        }
}

void* thread1(SONG *sSong)
{
        int iLoop,thd;
        thd=pthread_self();
        printf("thd1=%d\n",thd);
	execlp("test2","test2",NULL);
}
void* thread2()
{
        int iLoop,thd;
	thd=pthread_self();
	printf("thd2=%d\n",thd);
        for(iLoop=0;iLoop<10;iLoop++){
                printf("this is thread 2\n");
                sleep(1);
        }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?