当前位置: 技术问答>linux和unix
Linux 线程 时钟中断
来源: 互联网 发布时间:2016-12-28
本文导语: 最近在看Modern Operating System,第三版,其中章节2.2.2倒数第4段如下: Another common thread call is thread-yield, which allows a thread to voluntarily give up the CPU to let another thread run. Such a call is important because there is ...
最近在看Modern Operating System,第三版,其中章节2.2.2倒数第4段如下:
Another common thread call is thread-yield, which allows a thread to voluntarily
give up the CPU to let another thread run. Such a call is important because
there is no clock interrupt to actually enforce multiprogramming as there is with
processes. Thus it is important for threads to be polite and voluntarily surrender
the CPU from time to time to give other threads a chance to run. Other calls allow
one thread to wait for another thread to finish some work, for a thread to announce
that it has finished some work, and so on.
大意是一个良好设计的多线程程序中,每个线程应该及时调用thread_yield释放CPU,因为线程库无法像进程那样通过时钟中断强制线程让出CPU。
我的理解是如果一个线程霸占了CPU(单核系统),那么该进程中的其他线程就无法执行了。但是我测试的结果却正好相反,线程间交替执行,测试代码见最下方。
我猜测可能有两个原因:
1、书上写错了,不过觉得不大可能
2、每次进程新获取时间片时,从就绪的线程中随机选了一个来执行(该线程由于进入死循环,会占用整个进程的时间片),所以看上去好像是在进程时间片内轮流执行。
我感觉应该是第2种原因,但不知道该如何用一段测试代码去证明,请教各位大牛有没有什么好的方法。如果上面两种猜测都不对,那么究竟是什么原因造成的呢?
PS:分数只剩下58分了,有点少,不好意思...
测试代码如下:
Another common thread call is thread-yield, which allows a thread to voluntarily
give up the CPU to let another thread run. Such a call is important because
there is no clock interrupt to actually enforce multiprogramming as there is with
processes. Thus it is important for threads to be polite and voluntarily surrender
the CPU from time to time to give other threads a chance to run. Other calls allow
one thread to wait for another thread to finish some work, for a thread to announce
that it has finished some work, and so on.
大意是一个良好设计的多线程程序中,每个线程应该及时调用thread_yield释放CPU,因为线程库无法像进程那样通过时钟中断强制线程让出CPU。
我的理解是如果一个线程霸占了CPU(单核系统),那么该进程中的其他线程就无法执行了。但是我测试的结果却正好相反,线程间交替执行,测试代码见最下方。
我猜测可能有两个原因:
1、书上写错了,不过觉得不大可能
2、每次进程新获取时间片时,从就绪的线程中随机选了一个来执行(该线程由于进入死循环,会占用整个进程的时间片),所以看上去好像是在进程时间片内轮流执行。
我感觉应该是第2种原因,但不知道该如何用一段测试代码去证明,请教各位大牛有没有什么好的方法。如果上面两种猜测都不对,那么究竟是什么原因造成的呢?
PS:分数只剩下58分了,有点少,不好意思...
测试代码如下:
#include
#include
#include
#include
#include
#define MAX 10
pthread_t thread[10];
pthread_mutex_t mut;
int number=0, i;
int a = 1;
void *thread1(void* arr)
{
if (a == -1)
printf ("thread1 : I'm thread 1n");
else
a = -1;
while(1+1>1){
printf("thread 1 runningn");
}
pthread_exit(NULL);
}
void *thread2(void* arr)
{
if (a == -1)
printf("thread2 : I'm thread 2n");
else
a = -1;
while(1+1>1){
printf("thread 2 runningn");
}
pthread_exit(NULL);
}
void *thread3(void* arr)
{
if (a == -1)
printf("thread3 : I'm thread 3n");
else
a = -1;
while(1+1>1){
printf("thread 3 runningn");
}
pthread_exit(NULL);
}
void *thread4(void* arr)
{
if (a == -1)
printf("thread4 : I'm thread 4n");
else
a = -1;
while(1+1>1){
printf("thread 4 runningn");
}
pthread_exit(NULL);
}
void thread_create(void)
{
int temp;
memset(&thread, 0, sizeof(thread)); //comment1
printf("create thread 1n");
if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2
{
}
else
{
}
printf("create thread 2n");
if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3
{}
else
{}
printf("create thread 3n");
if((temp = pthread_create(&thread[2], NULL, thread3, NULL)) != 0) //comment3
{}
else
{}
printf("create thread 4n");
if((temp = pthread_create(&thread[3], NULL, thread4, NULL)) != 0) //comment3
{}
else
{}
}
void thread_wait(void)
{
if(thread[0] !=0) { //comment4
pthread_join(thread[0],NULL);
}
if(thread[1] !=0) { //comment5
pthread_join(thread[1],NULL);
}
}
int main()
{
pthread_mutex_init(&mut,NULL);
thread_create();
thread_wait();
return 0;
}
|
linux的调度策略是几种调度策略的综合,不是简单一种调度策略。
要不系统很容易挂掉。
给你个链接看看
http://topic.csdn.net/u/20100511/14/8E9B2F67-4527-476A-B4BB-E46D19885E9A.html#r_65356828
要不系统很容易挂掉。
给你个链接看看
http://topic.csdn.net/u/20100511/14/8E9B2F67-4527-476A-B4BB-E46D19885E9A.html#r_65356828