Ⅰ 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回你了~