Linux系统编程——线程(1)
目錄
- 線程概要
- Linux內(nèi)核線程實(shí)現(xiàn)原理
- 線程的共享/不共享資源
- 線程優(yōu)缺點(diǎn)
- 線程控制原語(yǔ)
- pthread_self
- pthread_create
- pthread_exit
- pthread_join
- pthread_cancel
- 終止線程方式
- 控制原語(yǔ)對(duì)比
前情提要: Linux用戶級(jí)線程和內(nèi)核級(jí)線程區(qū)別
線程概要
Linux內(nèi)核線程實(shí)現(xiàn)原理
類Unix系統(tǒng)中,早期是沒(méi)有“線程”概念的,80年代才引入,借助進(jìn)程機(jī)制實(shí)現(xiàn)出了線程的概念。因此在這類系統(tǒng)中,進(jìn)程和線程關(guān)系密切。
輕量級(jí)進(jìn)程(light-weight process),也有PCB,創(chuàng)建線程使用的底層函數(shù)和進(jìn)程一樣,都是clone
從內(nèi)核里看進(jìn)程和線程是一樣的,都有各自不同的PCB,但是PCB中指向內(nèi)存資源的三級(jí)頁(yè)表是相同的
進(jìn)程可以蛻變成線程
線程可看做寄存器和棧的集合
在linux下,線程最是小的執(zhí)行單位;進(jìn)程是最小的分配資源單位
查看線程命令:ps -elf|grep thread
三級(jí)映射:進(jìn)程PCB --> 頁(yè)目錄(可看成數(shù)組,首地址位于PCB中) --> 頁(yè)表 --> 物理頁(yè)面 --> 內(nèi)存單元
線程的共享/不共享資源
| 文件描述符表 | 線程id |
| 每種信號(hào)的處理方式 | 處理器現(xiàn)場(chǎng)和棧指針(內(nèi)核棧) |
| 當(dāng)前工作目錄 | 獨(dú)立的棧空間(用戶空間棧) |
| 用戶ID和組ID | errno變量 |
| 內(nèi)存地址空間(.text/.data/.bss/heap/共享庫(kù)) | 信號(hào)屏蔽字 |
| 調(diào)度優(yōu)先級(jí) |
線程優(yōu)缺點(diǎn)
優(yōu)點(diǎn): 1. 提高程序并發(fā)性 2. 開(kāi)銷(xiāo)小 3. 數(shù)據(jù)通信、共享數(shù)據(jù)方便
缺點(diǎn): 1. 庫(kù)函數(shù),不穩(wěn)定 2. 調(diào)試、編寫(xiě)困難、gdb不支持 3. 對(duì)信號(hào)支持不好
線程控制原語(yǔ)
pthread_self
獲取線程ID。其作用對(duì)應(yīng)進(jìn)程中 getpid() 函數(shù)。
? pthread_t pthread_self(void); 返回值:成功:0; 失敗:無(wú)!
? 線程ID:pthread_t類型,本質(zhì):在Linux下為無(wú)符號(hào)整數(shù)(%lu),其他系統(tǒng)中可能是結(jié)構(gòu)體實(shí)現(xiàn)
? 線程ID是進(jìn)程內(nèi)部,識(shí)別標(biāo)志。(兩個(gè)進(jìn)程間,線程ID允許相同)
pthread_create
創(chuàng)建一個(gè)新線程。 其作用,對(duì)應(yīng)進(jìn)程中fork() 函數(shù)。
? int pthread_create(pthread_t thread, const pthread_attr_t attr, void (start_routine) (void ), void arg);
? 返回值:成功:0; 失敗:錯(cuò)誤號(hào) -----Linux環(huán)境下,所有線程特點(diǎn),失敗均直接返回錯(cuò)誤號(hào)。
參數(shù):
? pthread_t:當(dāng)前Linux中可理解為:typedef unsigned long int pthread_t;
參數(shù)1:傳出參數(shù),保存系統(tǒng)為我們分配好的線程ID
? 參數(shù)2:通常傳NULL,表示使用線程默認(rèn)屬性。若想使用具體屬性也可以修改該參數(shù)。
? 參數(shù)3:函數(shù)指針,指向線程主函數(shù)(線程體),該函數(shù)運(yùn)行結(jié)束,則線程結(jié)束。
? 參數(shù)4:線程主函數(shù)執(zhí)行期間所使用的參數(shù)。
練習(xí):創(chuàng)建一個(gè)新線程,打印線程ID。注意:鏈接線程庫(kù) -lpthread
#include <stdio.h> #include <pthread.h> #include <unistd.h>void *tfn(void *arg) {printf("I'm thread, Thread_ID = %lu\n", pthread_self());return NULL; }int main(void) {pthread_t tid;pthread_create(&tid, NULL, tfn, NULL);sleep(1);printf("I am main, my pid = %d\n", getpid());return 0; }線程默認(rèn)共享數(shù)據(jù)段、代碼段等地址空間,常用的是全局變量,或者傳參形式。而進(jìn)程不共享全局變量,只能借助mmap。
全局變量:
#include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <unistd.h>int var = 100;void *tfn(void *arg) {var = 200;printf("thread\n");return NULL; }int main(void) {printf("At first var = %d\n", var);pthread_t tid;pthread_create(&tid, NULL, tfn, NULL);sleep(1);printf("after pthread_create, var = %d\n", var);return 0; }傳參:
#include <func.h>void *tfn(void *arg){int* var = (int*)arg;*var = 200;printf("thread\n");return NULL; }int main() {int var = 100;printf("At first var = %d\n", var);pthread_t tid;pthread_create(&tid, NULL, tfn, &var);sleep(1);printf("after pthread_create, var = %d\n", var);return 0; }pthread_exit
作用:將單個(gè)線程退出
? void pthread_exit(void *retval); 參數(shù):retval表示線程退出狀態(tài),通常傳NULL
線程中,**禁止使用exit函數(shù),會(huì)導(dǎo)致進(jìn)程內(nèi)所有線程全部退出**。所以,多線程環(huán)境中,應(yīng)盡量少用,或者不使用exit函數(shù),取而代之使用pthread_exit函數(shù),將單個(gè)線程退出。任何線程里exit導(dǎo)致進(jìn)程退出,其他線程未工作結(jié)束,主控線程退出時(shí)不能return或exit。另注意,pthread_exit或者return返回的指針?biāo)赶虻膬?nèi)存單元必須是全局的或者是用malloc分配的,不能在線程函數(shù)的棧上分配,因?yàn)楫?dāng)其它線程得到這個(gè)返回指針時(shí)線程函數(shù)已經(jīng)退出了。
pthread_join
阻塞等待線程退出,獲取線程退出狀態(tài) 其作用,對(duì)應(yīng)進(jìn)程中 waitpid() 函數(shù)。
? int pthread_join(pthread_t thread, void **retval); 成功:0;失敗:錯(cuò)誤號(hào)
? 參數(shù):thread:線程ID (【注意】:不是指針);retval:存儲(chǔ)線程結(jié)束狀態(tài)。
? 對(duì)比記憶:
? 進(jìn)程中:main返回值、exit參數(shù)-->int;等待子進(jìn)程結(jié)束 wait 函數(shù)參數(shù)-->int *
? 線程中:線程主函數(shù)返回值、pthread_exit-->void *;等待線程結(jié)束 pthread_join 函數(shù)參數(shù)-->void **
調(diào)用該函數(shù)的線程將掛起等待,直到id為thread的線程終止。thread線程以不同的方法終止,通過(guò)pthread_join得到的終止?fàn)顟B(tài)是不同的,總結(jié)如下:
如果thread線程通過(guò)return返回,retval所指向的單元里存放的是thread線程函數(shù)的返回值。
如果thread線程被別的線程調(diào)用pthread_cancel異常終止掉,retval所指向的單元里存放的是常數(shù)PTHREAD_CANCELED。
如果thread線程是自己調(diào)用pthread_exit終止的,retval所指向的單元存放的是傳給pthread_exit的參數(shù)。
如果對(duì)thread線程的終止?fàn)顟B(tài)不感興趣,可以傳NULL給retval參數(shù)。
pthread_cancel
殺死(取消)線程 其作用,對(duì)應(yīng)進(jìn)程中 kill() 函數(shù)。
? int pthread_cancel(pthread_t thread); 成功:0;失敗:錯(cuò)誤號(hào)
? 【注意】:線程的取消并不是實(shí)時(shí)的,而有一定的延時(shí)。需要等待線程到達(dá)某個(gè)取消點(diǎn)(檢查點(diǎn))。
? 類似于玩游戲存檔,必須到達(dá)指定的場(chǎng)所(存檔點(diǎn),如:客棧、倉(cāng)庫(kù)、城里等)才能存儲(chǔ)進(jìn)度。殺死線程也不是立刻就能完成,必須要到達(dá)取消點(diǎn)。
? 取消點(diǎn):是線程檢查是否被取消,并按請(qǐng)求進(jìn)行動(dòng)作的一個(gè)位置。通常是一些系統(tǒng)調(diào)用creat,open,pause,close,read,write..... 執(zhí)行命令man 7 pthreads可以查看具備這些取消點(diǎn)的系統(tǒng)調(diào)用列表。也可參閱 APUE.12.7 取消選項(xiàng)小節(jié)。
可粗略認(rèn)為一個(gè)系統(tǒng)調(diào)用(進(jìn)入內(nèi)核)即為一個(gè)取消點(diǎn)。如線程中沒(méi)有取消點(diǎn),可以通過(guò)調(diào)用pthreestcancel函數(shù)自行設(shè)置一個(gè)取消點(diǎn)。
被取消的線程, 退出值定義在Linux的pthread庫(kù)中。常數(shù)PTHREAD_CANCELED的值是-1。可在頭文件pthread.h中找到它的定義:#define PTHREAD_CANCELED ((void *) -1)。因此當(dāng)我們對(duì)一個(gè)已經(jīng)被取消的線程使用pthread_join回收時(shí),得到的返回值為-1。
終止線程的三種方法。注意“取消點(diǎn)”的概念。
#include <stdio.h> #include <unistd.h> #include <pthread.h> #include <stdlib.h>void *tfn1(void *arg) {printf("thread 1 returning\n");return (void *)111; }void *tfn2(void *arg) {printf("thread 2 exiting\n");pthread_exit((void *)222); }void *tfn3(void *arg) {while (1) {//printf("thread 3: I'm going to die in 3 seconds ...\n");//sleep(1);pthread_testcancel(); //自己添加取消點(diǎn)*/}return (void *)666; }int main(void) {pthread_t tid;void *tret = NULL;pthread_create(&tid, NULL, tfn1, NULL);pthread_join(tid, &tret);printf("thread 1 exit code = %d\n\n", (int)tret);pthread_create(&tid, NULL, tfn2, NULL);pthread_join(tid, &tret);printf("thread 2 exit code = %d\n\n", (int)tret);pthread_create(&tid, NULL, tfn3, NULL);sleep(3);pthread_cancel(tid);pthread_join(tid, &tret);printf("thread 3 exit code = %d\n", (int)tret);return 0; }終止線程方式
總結(jié):終止某個(gè)線程而不終止整個(gè)進(jìn)程,有三種方法:
從線程主函數(shù)return。這種方法對(duì)主控線程不適用,從main函數(shù)return相當(dāng)于調(diào)用exit。
一個(gè)線程可以調(diào)用pthread_cancel終止同一進(jìn)程中的另一個(gè)線程。
線程可以調(diào)用pthread_exit終止自己。
控制原語(yǔ)對(duì)比
? 進(jìn)程 線程
? fork pthread_create
? exit pthread_exit
? wait pthread_join
? kill pthread_cancel
? getpid pthread_self 命名空間
轉(zhuǎn)載于:https://www.cnblogs.com/Mered1th/p/10801287.html
總結(jié)
以上是生活随笔為你收集整理的Linux系统编程——线程(1)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 《GTAVC重制版》全秘籍作弊码一览 G
- 下一篇: 《GTA3重制版》秘籍作弊码大全