⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pthread.c

📁 This a pthread program code which is compiled under the linux. Someone who want to test the pthread
💻 C
字号:
// 951417 孫健銘 HW2#include<pthread.h>/*create thread*/#include<stdio.h>#include<sys/types.h>//取得Pid#include<unistd.h>//建立行程#include<stdlib.h>#include<sys/wait.h>void *func(void *temp){	char *ptr = (char *)temp;	printf("This is : %s\n", ptr);/**********************************************************************************************/			//結合hw1 Pid       pid_t pid;//建立行程id變數	 printf("Process        Process's id           Round\n");       pid = fork();//建立子行程	 if(pid > 0)//父行程      	  {		printf("Parent 		    %d  		   %d\n",getpid(),1);            sleep(2);	   			wait(NULL);//在此等待 等程式全部結束後才跳離視窗             // exit(-1);      	  }       else if(pid==0)//子行程      	  {            printf("Child     	    %d	      %d\n",getpid(),1);            sleep(3);//休息3秒      	  }	 else  if(pid < 0)//fork 失敗      	  {          printf("fork is not success");          exit(-1);      	  }/**********************************************************************************************/}int main(){	pthread_t thread1,thread2;/*宣告型態*/	char *message1 = "Thread 1";	char *message2 = "Thread 2";	/**************************************************************/	//pthread_create	/*&thread 儲存Thread的代碼*/	/*NULL代表預設 pthread_attr_t*/	/*函式指標*/	/*傳入參數為(char*)型態須先轉成(void*)型態,以完成messge被強制轉型的動作*/	/**************************************************************/	//pthread_join	/*使Thread等待其他Thread結束之後才可以繼續執*/	/**************************************************************/	pthread_create(&thread1,NULL,func,(void*) message1);	pthread_create(&thread2,NULL,func,(void*) message2);			pthread_join(thread1,NULL);	pthread_join(thread2,NULL);return 0;}

⌨️ 快捷键说明

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