Ⅰ linux多線程求解,列題是華清遠見上面的,代碼如下,利用線程互斥鎖實現線程的同步
目測是線程退出時沒有解開互斥鎖,導致其它線程一直在等互斥鎖被解開。
以下是修改後的thrd_func函數代碼:
//線程函數入口
void*thrd_func(void*arg)
{
intthrd_num=(int)arg;
intdelay_time=0;
intcount=0;
intres;
res=pthread_mutex_lock(&mutex);//互斥鎖上鎖
if(res)
{
printf("Thread%dlockfailed ",thrd_num);
pthread_exit(NULL);
}
printf("Thread%disstarting ",thrd_num);
for(count=0;count<REPEAT_NUMBER;count++)
{
delay_time=(int)(rand()%5);//隨機時間數
sleep(delay_time);
printf(" Thread%d:job%ddelay=%d ",thrd_num,count,delay_time);
}
pthread_mutex_unlock(&mutex);//解開互斥鎖
printf("Thread%dfinished ",thrd_num);
pthread_exit(NULL);
}
Ⅱ LINUX下多線程編程基礎問題
你的主線程在 pthread_create 結束並列印 success 後,直接返回退出了,導致整個進程都結束了,你新創出來的線程自然也結束了。解決方法是讓主線程等待新創線程結束後再退出,如下
int tmp = pthread_create(&t_id1, NULL, run1, NULL);
if(tmp == 0)
cout << "success" << endl;
// wait t_id1 complete
pthread_join(t_id1, NULL);
return 0;
Ⅲ Linux下如何寫可重啟的多線程子系統
在開發內核模塊或驅動時,如果處理失誤,導致內核線程中出現死鎖或者死循環,你會發現,除了重啟之外,你沒有任何可以做的。這時你的輸入不起任何作用,終端(不是指遠程的ssh工具)只會在那重復的輸出類似「BUG: soft lockup - CPU#0 stuck for 67s! [fclustertool:2043]」,更無奈的是你重啟之後導致系統掛起的堆棧信息也看不到,你所能做的就是一遍遍的加調試信息,一遍遍的重啟機器(這是我的經歷,現在想想很傻)。
這種情況你肯定不是第一個遇到的,所以內核肯定會提供處理這種情況的一些機制。但是如何來找到這些機制在哪個地方,或者說根據什麼信息去google呢?最有用的就是這句話「BUG: soft lockup - CPU#0 stuck for 67s! [fclustertool:2043]」,因為這句話提供你的信息量很大。首先,這條信息可以輸出,說明即使發生死鎖或者死循環,還是有代碼可以執行。第二,可以通過這個日誌信息,找到對應的處理函數,這個函數所在的模塊就是用來處理CPU被過度使用時用到的。所以通過這個事情,可以看到內核列印出的只言片語都有可能成為你解決問題的關鍵,一定要從重視這些信息,從中找出有用的東西。
我經常看的內核版本是官方的2.6.32內核,這個版本中我找到的函數是softlockup_tick(),這個函數在時鍾中斷的處理函數run_local_timers()中調用。這個函數會首先檢查watchdog線程是否被掛起,如果不是watchdog線程,會檢查當前佔有CPU的線程佔有的時間是否超過系統配置的閾值,即softlockup_thresh。如果當前佔有CPU的時間過長,則會在系統日誌中輸出我們上面看到的那條日誌。接下來才是最關鍵的,就是輸出模塊信息、寄存器信息和堆棧信息,檢查softlockup_panic的值是否為1。如果softlockup_panic為1,則調用panic()讓內核掛起,輸出OOPS信息。代碼如下所示:/** This callback runs from the timer interrupt, and checks
* whether the watchdog thread has hung or not:*/void softlockup_tick(void){int this_cpu = smp_processor_id();
unsigned long touch_timestamp = per_cpu(touch_timestamp, this_cpu);
unsigned long print_timestamp;
struct pt_regs *regs = get_irq_regs();
unsigned long now;
/* Warn about unreasonable delays: */
if (now <= (touch_timestamp + softlockup_thresh))return;
per_cpu(print_timestamp, this_cpu) = touch_timestamp;
spin_lock(&print_lock);
printk(KERN_ERR BUG: soft lockup - CPU#%d stuck for %lus! [%s:%d]\n,
this_cpu, now - touch_timestamp,
current-comm, task_pid_nr(current));
print_moles();
print_irqtrace_events(current);if (regs)show_regs(regs);elsemp_stack();
spin_unlock(&print_lock);
if (softlockup_panic)
panic(softlockup: hung tasks);}
但是softlockup_panic的值默認竟然是0,所以在出現死鎖或者死循環的時候,會一直只輸出日誌信息,而不會宕機,這個真是好坑啊!所以你得手動修改/proc/sys/kernel/softlockup_panic的值,讓內核可以在死鎖或者死循環的時候可以宕機。如果你的機器中安裝了kmp,在重啟之後,你會得到一份內核的core文件,這時從core文件中查找問題就方便很多了,而且再也不用手動重啟機器了。如果你的內核是標准內核的話,可以通過修改/proc/sys/kernel/softlockup_thresh來修改超時的閾值,如果是CentOS內核的話,對應的文件是/proc/sys/kernel/watchdog_thresh。CentOS內核和標准內核還有一個地方不一樣,就是處理CPU佔用時間過長的函數,CentOS下是watchdog_timer_fn()函數。
這里介紹下lockup的概念。lockup分為soft lockup和hard lockup。 soft lockup是指內核中有BUG導致在內核模式下一直循環的時間超過10s(根據實現和配置有所不同),而其他進程得不到運行的機會。hard softlockup是指內核已經掛起,可以通過watchdog這樣的機制來獲取詳細信息。這兩個概念比較類似。如果你想了解更多關於lockup的信息,可以參考這篇文檔:
注意上面說的這些,都是在內核線程中有效,對用戶態的死循環沒用。如果要監視用戶態的死循環,或者內存不足等資源的情況,強烈推薦軟體層面的watchdog。具體的操作可以參考下面的文章,都寫的非常好,非常實用:
Ⅳ linux 編寫一個多線程程序,要求主線程創建3個子線程,3個子線程在執行時都修改一個他們的共享變數。
void func1(int n)
{
printf("%d",n*10);
}
void func1(int n)
{
printf("%d",n-10);
}
void func1(int n)
{
printf("%d",n/2);
}
int main(void)
{
int n = 10;
pthread_t 1_thread,2_thread,3_thread;
pthread_create(1_thread,NULL,func1,n);
pthread_create(2_thread,NULL,func2,n);
pthread_create(3_thread,NULL,func3,n);
return 0;
}
Ⅳ Linux多線程編程
程序代碼test.c共兩個線程,一個主線程,一個讀緩存區的線程:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char globe_buffer[100];
void *read_buffer_thread(void *arg); //這里先聲明一下讀緩存的線程,具體實現寫在後面了
int main()
{
int res,i;
pthread_t read_thread;
for(i=0;i<20;i++)
globe_buffer[i]=i;
printf("\nTest thread : write buffer finish\n");
sleep(3);\\這里的3秒是多餘,可以不要。
res = pthread_create(&read_thread, NULL, read_buffer_thread, NULL);
if (res != 0)
{
printf("Read Thread creat Error!");
exit(0);
}
sleep(1);
printf("waiting for read thread to finish...\n");
res = pthread_join(read_thread, NULL);
if (res != 0)
{
printf("read thread join failed!\n");
exit(0);
}
printf("read thread test OK, have fun!! exit ByeBye\n");
return 0;
}
void *read_buffer_thread(void *arg)
{
int i,x;
printf("Read buffer thread read data : \n");
for(i=0;i<20;i++)
{
x=globe_buffer[i];
printf("%d ",x);
globe_buffer[i]=0;//清空
}
printf("\nread over\n");
}
---------------------------------------------------------------------------------
以上程序編譯:
gcc -D_REENTRANT test.c -o test.o –lpthread
運行這個程序:
$ ./test.o:
Ⅵ linux多線程小程序
首先聲明我也不是高手,我和你一樣,正在學習可以討論、討論,我說的只是自己的理解,不一定完全正確!
你使用信號量的作用是做同步的如果operate_data1或者 operate_data2線程先運行的話他們不會先去作運算的,不會出現你說的那種「當數沒全寫到數組里,就進行運算了」的情況,因為在他們運算之前調用了sem_wait(&s);首先要有信號資源可用才可以去運算,而現在只有3種情況可以獲得信號資源:
1、初始化信號量的時候是正值,顯然你的不是 sem_init(&s,0,0);
2、void write_data1(int *a)線程運行後;
3、void write_data1(int *a)線程運行後;
只有以上這三種情況的任意一種發生了,兩個operate_data中的一個才可能獲得資源而運行。要解決這種情況最簡單的情況是把兩個寫數據操作放到一個線程中把兩個操作數據放到一個線程中,這樣就不會出現競爭的情況了。或是在把兩個寫數據操作放到一個線程中,在操作線程中,在操作前sem_wait(&s);在操作後調用sem_post(&s);
Ⅶ Linux下如何實現shell多線程編程以提高應用程序的響應
Linux中多線程編程擁有提高應用程序的響應、使多cpu系統更加有效等優點,下面小編將通過Linux下shell多線程編程的例子給大家講解下多線程編程的過程,一起來了解下吧。
#!/bin/bash
#———————————————————————————–
# 此例子說明了一種用wait、read命令模擬多線程的一種技巧
# 此技巧往往用於多主機檢查,比如ssh登錄、ping等等這種單進程比較慢而不耗費cpu的情況
# 還說明了多線程的控制
#———————————————————————————–
function a_sub
{
# 此處定義一個函數,作為一個線程(子進程)
sleep 3 # 線程的作用是sleep 3s
}
tmp_fifofile=「/tmp/$.fifo」 mkfifo $tmp_fifofile # 新建一個fifo類型的文件
exec 6《》$tmp_fifofile # 將fd6指向fifo類型
rm $tmp_fifofile thread=15 # 此處定義線程數
for
((i=0;i《$thread;i++));do echo
done 》&6 # 事實上就是在fd6中放置了$thread個回車符
for
((i=0;i《50;i++));do # 50次循環,可以理解為50個主機,或其他
read -u6 # 一個read -u6命令執行一次,就從fd6中減去一個回車符,然後向下執行,
# fd6中沒有回車符的時候,就停在這了,從而實現了線程數量控制
{ # 此處子進程開始執行,被放到後台
a_sub &&
{ # 此處可以用來判斷子進程的邏輯
echo 「a_sub is finished」
}
||
{ echo 「sub error」
}
echo 》&6 # 當進程結束以後,再向fd6中加上一個回車符,即補上了read -u6減去的那個
}
& done wait # 等待所有的後檯子進程結束
exec 6》&- # 關閉df6 exit 0
說明:
此程序中的命令
mkfifo tmpfile
和linux中的命令
mknod tmpfile p
效?果相同。區別是mkfifo為POSIX標准,因此推薦使用它。該命令創建了一個先入先出的管道文件,並為其分配文件標志符6。管道文件是進程之間通信的一種方式,注意這一句很重要
exec 6《》$tmp_fifofile # 將fd6指向fifo類型
如果沒有這句,在向文件$tmp_fifofile或者&6寫入數據時,程序會被阻塞,直到有read讀出了管道文件中的數據為止。而執行了上面這一句後就可以在程序運行期間不斷向fifo類型的文件寫入數據而不會阻塞,並且數據會被保存下來以供read程序讀出。
通過運行命令:
time 。/multithread.sh 》/dev/null
最終運算時間: 50/15 = 3組(每組15)+1組(5個《15 組成一個組)= 4組,每組花費時間:3秒,
則 3 * 4 = 12 秒。
傳統非多線程的代碼 運算時間: 50 * 3 = 150 秒。
上面就是Linux下shell多線程編程的實例介紹了,使用多線程編程還能夠改善程序結構,有興趣的朋友不妨試試看吧。
Ⅷ Linux下如何實現shell多線程編程
程序代碼test.c共兩個線程,一個主線程,一個讀緩存區的線程:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char globe_buffer[100];
void *read_buffer_thread(void *arg); //這里先聲明一下讀緩存的線程,具體實現寫在後面了
int main()
{
int res,i;
pthread_t read_thread;
for(i=0;i<20;i++)
globe_buffer[i]=i;
printf("\nTest thread : write buffer finish\n");
sleep(3);\\這里的3秒是多餘,可以不要。
res = pthread_create(&read_thread, NULL, read_buffer_thread, NULL);
if (res != 0)
{
printf("Read Thread creat Error!");
exit(0);
}
sleep(1);
printf("waiting for read thread to finish...\n");
res = pthread_join(read_thread, NULL);
if (res != 0)
{
printf("read thread join failed!\n");
exit(0);
}
printf("read thread test OK, have fun!! exit ByeBye\n");
return 0;
}
void *read_buffer_thread(void *arg)
{
int i,x;
printf("Read buffer thread read data : \n");
for(i=0;i<20;i++)
{
x=globe_buffer[i];
printf("%d ",x);
globe_buffer[i]=0;//清空
}
printf("\nread over\n");
}
---------------------------------------------------------------------------------
以上程序編譯:
gcc -D_REENTRANT test.c -o test.o –lpthread
運行這個程序:
$ ./test.o:
Ⅸ 在Linux環境下,對一個設備文件進行多線程讀寫(兩個線程就行),求大神給一個簡單的程序。
配置文件為 conf.txt
測試代碼如下,注意鏈接的時候加上 -lpthread 這個參數
#include <stdio.h>
#include <errno.h> //perror()
#include <pthread.h>
#include <unistd.h> //sleep()
#include <time.h> // time()
#include <stdlib.h> //rand()
#define FD "conf.txt"
typedef void *(*fun)(void *);
struct my_struct
{
unsigned time_to_wait;
int n;
};
void *test_thread(struct my_struct *);
int main (int argc, char const *argv[])
{
FILE *fp = fopen(FD, "r");
if (fp == NULL)
{
perror(FD);
return -1;
}
srand((unsigned)time(NULL)); //初始化隨機種子
int thread_count;
fscanf(fp, "%d", &thread_count);
fclose(fp);
if (thread_count <= 0)
{
printf("線程數<1,退出程序。\n");
return -1;
}
pthread_t *ptid = (pthread_t *)malloc(sizeof(pthread_t) * thread_count); //保存線程ID
int i;
for (i = 0; i < thread_count; i++)
{
int tw = rand() % thread_count + 1; //隨機等待時間
struct my_struct * p = (struct my_struct *)malloc(sizeof(struct my_struct));
if (p == NULL)
{
perror("內存分配錯誤");
goto ERROR;
}
p->time_to_wait = tw;
p->n = i + 1;
int rval = pthread_create(ptid + i, NULL, (fun) test_thread, (void *)(p)); //注意這里的強制轉換(兩個)
if (rval != 0)
{
perror("Thread creation failed");
goto ERROR;
}
//sleep(1); //這句加也可以,不加也可以。最開始的時候加上這個是為了讓兩個線程啟動的時候之間有一定的時間差
}
printf("主線程啟動\n\n");
fflush(stdout);
for (i = 0; i < thread_count; i++)
{
pthread_join(*(ptid + i), NULL); //等待所有線程退出。
}
printf("\n主線程退出\n");
ERROR:
free(ptid);
return 0;
}
void *test_thread(struct my_struct * p) //線程啟動的時候運行的函數
{
printf("第%d個線程啟動,預計運行%d秒\n", p->n, p->time_to_wait);
fflush(stdout);
sleep(p->time_to_wait); //讓線程等待一段時間
printf("第%d個線程結束\n", p->n);
fflush(stdout);
free(p);
return NULL;
}
你的第二個問題我在網路HI回你了~