程序與執行緒(二)

2021-07-11 02:15:53 字數 3761 閱讀 4138

linux中的執行緒機制十分特殊,從核心的角度來說並沒有執行緒的概念,故沒有為執行緒單獨定義資料結構,通過clone()建立的linux執行緒,僅被看做乙個與其他程序共享某些資源的特殊程序而已;

linxu中把執行緒分為核心執行緒,核心支援的使用者執行緒和執行緒庫支援的使用者執行緒等3種型別;

1、核心執行緒:核心執行緒的建立於撤銷有核心內部的需求決定,核心執行緒沒有使用者位址空間,它共享核心的正文段和核心全域性資料,具有自己的核心棧,核心執行緒的排程由於不需要經過cpu狀態的轉換,所以核心執行緒間的上下文切換比在使用者執行緒間快得多;

2、核心支援的使用者執行緒:有clone()函式建立的執行緒;

3、執行緒庫支援的使用者執行緒:使用者執行緒是通過執行緒庫實現的,核心不參與排程,它可以在沒有核心參與下建立,釋放和管理,執行緒庫提供同步和排程的方法。核心通過排程程序,程序通過執行緒庫排程執行緒;

一、核心支援的使用者執行緒

通過clone()函式進行建立;

/*int clone(int (*fn)(void *arg),void *stack,int flags,void *arg)

fn:函式指標,執行緒識別符號

flags:

clone_vm:父程序,子程序共享程序空間;

clone_fs:父程序,子程序共享檔案系統資訊;

clone_files:父程序,子程序共享開啟的檔案;

clone_sigghld:子程序終結或者暫停時給父程序發訊號;

clone_sighand:父子程序共享訊號處理函式;

clone_pid:父子程序共享程序識別符號;

clone_vfork:父程序在子程序釋放空間時被喚醒;

stack:執行緒所使用的核心棧;

arg:傳給fn的void*型別的引數

二、執行緒庫支援的使用者執行緒

1、posix執行緒

posix執行緒包提供一組函式,用來建立,刪除和同步同一程序內的執行緒,使用時需要加入標頭檔案pthread.h, 編譯時需要使用-pthread來鏈結執行緒庫

2、執行緒的建立

int  pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void),void *restrict arg);

//tidp:要建立的執行緒的執行緒id

//attr:為新執行緒定義不同的屬性(如棧的大小)

//start_rtn:指定執行的函式

//arg:傳遞給函式的引數

pthread_create與clone的區別在於:clone()建立的是乙個核心支援的使用者執行緒,對核心是可見的且由核心排程,而pthread_create建立乙個執行緒庫支援的使用者執行緒,對核心不可見,由執行緒庫排程;

執行緒的識別符號:與程序類似,執行緒也有執行緒識別符號,可通過pthread_self()獲取;

3、執行緒等待

int pthread_join(pthread_t thread,void **status);

//pthread:指定等待的執行緒

//status:指向指標的指標,用於指定返回值

//函式返回值:0表示成功,非0表示失敗

4、執行緒同步

(1)訊號量

#inclued 

int sem_init(sem_t *sem,int pthread,unsigned int

value);

//初始化訊號量

//sem:為指向訊號量結構的乙個指標

//pshared:不為0時此訊號量在程序間共享,否則只能為當前程序的所有執行緒共享

//value:給出了訊號量的初始值

int sem_wait(sem_t *sem);

int sem_post(sem_t *sem);

int sem_destroy(sem_t *sem);

(2)互斥量

#include

int pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutexattr_t *mutexattr);

int pthread_mutex_lock(pthread_mutex_t *mutex);

int pthread_mutex_unlock(pthread_mutex_t *mutex);

int pthread_mutex_destroy(pthread_mutex_t *mutex);

通過使用互斥量,使用兩個執行緒同時讀取兩個檔案並統計單詞個數:

#include 

#include

#include

#include

#include

pthread_mutex_t counter_clock;

int total_words = 0;

void *count_words(void *f)

prevc = c;

}fclose(fp);

}else

return null;

}int main(int argc,char *argv)

printf("reading...\n");

error = pthread_create(&thread1,null,count_words,(void *)argv[1]);

if(error != 0)

error = pthread_create(&thread2,null,count_words,(void *)argv[2]);

if(error != 0)

pthread_join(thread1,&thread_result1);

pthread_join(thread2,&thread_result2);

printf("total words:%d\n",total_words);

pthread_mutex_destroy(&counter_clock);

return

0;}

5、執行緒終止

執行緒的正常退出包括以下幾種

void pthread_exit(void *rval_prt);

//rval_ptr:執行緒退出返回的指標;

二 程序與執行緒

2.1 程序 現代計算機會在同一時間做許多事,需要一些方法控制這些併發的事情。在某一瞬間,cpu只能執行乙個程序,但是在較短時間內可以執行多個程序,被稱為偽並行。而多處理器系統能夠實現真正的硬體並行。乙個程序就是乙個正在執行的程式的例項,包括程式計數器 暫存器和變數的當前值。實際只有乙個物理程式計數...

程序與執行緒

程序 process 管理單元,管理分配應用程式的資料,的記憶體空間.執行緒 thread 執行單元,執行緒負責執行乙個預先編排好的 段,執行 棧是基於執行緒的.乙個應用程式啟動的時候,程序自動建立,並且會預設建立主線程,主線程負責執行main 方法.thread t new thread new ...

程序與執行緒

程序 是擁有資源的實體,包括 1。乙個程序有乙個虛擬位址空間,不同程序位於不同的 虛擬位址空間中。程序之間若要通訊,必須通過作業系統 的功能呼叫 ipc 2。程序擁有其他資源,例如開啟的檔案和i o裝置。程序結束時,作業系統會自動釋放該程序擁有的所有資源。例如,如果 open乙個檔案而不close它...