Linux 一種多執行緒建立 監視手段

2021-10-05 20:36:24 字數 3156 閱讀 2608

目前對linux的多程序、多執行緒理解還不夠深入。在現有理解的基礎上,寫了一套多執行緒的建立、維護例程。具備以下特性:

1.批量建立,類似於註冊;

2.批量監視;

3.重啟機制

直接上**,注釋很詳細

#include #include #include #include #include #include #include //定義最大執行緒數

#define max_thread 3

//執行緒標號

typedef enumthreadnamet;

//執行緒入口函式指標

typedef void*(*cmf)(void *arg);

//執行緒控制塊

//將每個執行緒抽象成由執行緒號、執行緒入口函式、監視錯誤碼組成的結構體

typedef structthreadscbt;

//執行緒鎖

pthread_mutex_t thrlock;

threadscbt thrscb[max_thread];//有幾個執行緒就建立幾個執行緒控制塊

//執行緒0

void *thread_0(void *arg)

}//執行緒1

void *thread_1(void *arg)

}//執行緒2

void *thread_2(void *arg)

}//執行緒塊初始化,主要是對各個pfun進行註冊

void thrscbinit()

int main()

//執行緒監視函式

while(1)

for(i = 0;i < max_thread; i++)

//如果傳送訊號非法

else if(pthread_kill_err == einval)

//否則為執行緒在執行

else

}printf("\n");

pthread_mutex_unlock(&thrlock);

//每隔3秒監視一次

usleep(3000000);

}return 0;

}

編譯執行後如下所示,可以發現執行緒正常列印時監視功能判斷為存活。當執行緒列印完5次後退出,之後被監視功能發現,又成功將其重啟。

wy@wy-virtual-machine:~/test$ ./t

thread:this is thread 0 i->0

thread:this is thread 2 i->0

thread:this is thread 1 i->0

watch :thread 0 is alive

watch :thread 1 is alive

watch :thread 2 is alive

thread:this is thread 1 i->1

thread:this is thread 0 i->1

thread:this is thread 2 i->1

thread:this is thread 1 i->2

thread:this is thread 0 i->2

thread:this is thread 2 i->2

watch :thread 0 is alive

watch :thread 1 is alive

watch :thread 2 is alive

thread:this is thread 1 i->3

thread:this is thread 2 i->3

thread:this is thread 0 i->3

thread:this is thread 1 i->4

thread:this is thread 2 i->4

thread:this is thread 0 i->4

watch :thread 0 is quit,restart it

watch :thread 1 is quit,restart it

watch :thread 2 is quit,restart it

thread:this is thread 0 i->0

thread:this is thread 2 i->0

thread:this is thread 1 i->0

thread:this is thread 2 i->1

thread:this is thread 0 i->1

thread:this is thread 1 i->1

thread:this is thread 0 i->2

thread:this is thread 1 i->2

thread:this is thread 2 i->2

watch :thread 0 is alive

watch :thread 1 is alive

watch :thread 2 is alive

thread:this is thread 2 i->3

thread:this is thread 1 i->3

thread:this is thread 0 i->3

thread:this is thread 1 i->4

thread:this is thread 2 i->4

thread:this is thread 0 i->4

watch :thread 0 is quit,restart it

watch :thread 1 is quit,restart it

watch :thread 2 is quit,restart it

thread:this is thread 0 i->0

thread:this is thread 2 i->0

thread:this is thread 1 i->0

thread:this is thread 2 i->1

thread:this is thread 0 i->1

thread:this is thread 1 i->1

thread:this is thread 0 i->2

thread:this is thread 2 i->2

thread:this is thread 1 i->2

多執行緒的一種場景

void say hello void args 執行緒的執行函式,函式返回的是函式指標,便於後面作為引數 定義執行緒的 id 變數,多個變數使用陣列 pthread t tids num threads int indexes num threads 用陣列來儲存i的值 for int i 0 i...

只有一種實現多執行緒的方式 ?

執行緒是實現多執行緒的基礎,本篇主要講解關於執行緒的兩個問題。1 為什麼說本質上只有一種實現多執行緒的方式?2 實現runnable的方式和繼承thread的方式哪種好?好在 為什麼說本質上只有一種實現多執行緒的方式?1 實現runnable介面的方式class runnableimpl runna...

多執行緒 四種建立方式(一)

舉例 列印1 100的偶數 方法一 繼承thread類 public class testthread01 class numthread01 extends thread 方法二 實現runnable介面 建立執行緒的方式二 實現runnable介面 public class testthread...