免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2847 | 回复: 7
打印 上一主题 下一主题

[C] 线程中while和for的强占性问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-26 23:31 |只看该作者 |倒序浏览
thread1{
while(K<20){
lock();

k++;

unlock();}
}

thread2{
for(k=0;k<20;K++)
{lock();
unlock();
;
}


分别创建多个thread1和thread2,就是多个线程调用相同函数(在不同的程序中),调用thread1的程序多个线程会交互运行(就是k不到20的时候会跳到别的线程),而调用thread2的程序的多个线程总是每个线程运行完后再运行下一个线程(就是每个线程到20后再跑下一个)
为什么会出现这种情况,是应为  while的抢占性强一点而for的抢占性弱一点还是别的原因,网上搜了半天也没找到原应。

论坛徽章:
0
2 [报告]
发表于 2009-06-26 23:39 |只看该作者
这个是操作系统调度算法的原因,哪里会有什么while和for的强占性的问题,闻所未温

论坛徽章:
0
3 [报告]
发表于 2009-06-26 23:42 |只看该作者

回复 #2 alexhappy 的帖子

能详细讲解一下么,我看到现在os的调度算法中好像没有类似的情况么

论坛徽章:
0
4 [报告]
发表于 2009-06-27 07:13 |只看该作者
应该是随机的吧,每次系统要操作的都不一样

论坛徽章:
0
5 [报告]
发表于 2009-06-27 10:32 |只看该作者
while的k++在中间,for的在末尾,有区别。

论坛徽章:
0
6 [报告]
发表于 2009-06-27 20:18 |只看该作者

回复 #5 思一克 的帖子

while代码
#include <stdio.h>
#include <pthread.h>
#include<semaphore.h>

#define SIZE 20
int ProductID=0;      
int ConsumeID=0;      
int in = 0;      
int out = 0;  
int buffer[SIZE];
int j=0;
int k=0;  
        
pthread_mutex_t mutex;        
sem_t empty;      
sem_t full;      
pthread_t producerID[5];
pthread_t consumerID[5];      

void *producer(void* ID)
{
       while(j<26){
           sem_wait(&empty);
           pthread_mutex_lock(&mutex);

    printf("producerID:");
       printf("%s\n", ID);
       printf("ProductID:");
       printf("%d\n", ++ProductID);
        
    int i;
       printf("in:");
       printf("%d\n",in);

         if(ProductID==21)
           ProductID=1;
            
       buffer[in] = ProductID;
       in = (in+1)%SIZE;
           j++;
      
       for(i=0;i<SIZE;i++)
      {
         printf(" %d",buffer);
        
       }
printf("\n");
         
           pthread_mutex_unlock(&mutex);
           sem_post(&full);
           sleep(3);
       }
}



void  *consumer(void* ID)
{
       while(k<26){
           sem_wait(&full);
           pthread_mutex_lock(&mutex);

           int i;
           printf("consumerID:");
           printf("%s\n",ID);
           printf("out:");
           printf("%d\n",out);
           ConsumeID = buffer[out];
           buffer[out]=0;
           out = (out+1)%SIZE;
           printf("ConsumeID:");
           printf("%d\n", ConsumeID);
             k++;
      
       for(i=0;i<SIZE;i++)
      {
         printf(" %d",buffer);
        
       }
printf("\n");
         
           pthread_mutex_unlock(&mutex);
           sem_post(&empty);
           sleep(3);
       }
}
int main()
{
char a[10][3]={"p1","p2","p3","p4","p5","c1","c2","c3","c4","c5"};
int i;
sem_init(&empty,0,20);
sem_init(&full,0,0);

for(i=0;i<5;i++)  
{
  pthread_create(&producerID,NULL,&producer,a);      
}  
for(i=0;i<5;i++)  
{   
  pthread_create(&consumerID,NULL,&consumer,a[i+5]);
}      
for(i=0;i<5;i++)
{
  pthread_join(producerID,NULL);
pthread_join(consumerID,NULL);
}
return 0;
}

for代码
#include <stdio.h>
#include <pthread.h>
#include<semaphore.h>

#define SIZE 20
int ProductID=0;      
int ConsumeID=0;      
int in = 0;      
int out = 0;  
int buffer[SIZE];
int j=0;
int k=0;  
        
pthread_mutex_t mutex;        
sem_t empty;      
sem_t full;      
pthread_t producerID[5];
pthread_t consumerID[5];      

void *producer(void* ID)
{
       for(j=0;j<26;j++){
           sem_wait(&empty);
           pthread_mutex_lock(&mutex);

    printf("producerID:");
       printf("%s\n", ID);
       printf("ProductID:");
       printf("%d\n", ++ProductID);
        
    int i;
       printf("in:");
       printf("%d\n",in);

         if(ProductID==21)
           ProductID=1;
            
       buffer[in] = ProductID;
       in = (in+1)%SIZE;
         
      
       for(i=0;i<SIZE;i++)
      {
         printf(" %d",buffer);
        
       }
printf("\n");
         
           pthread_mutex_unlock(&mutex);
           sem_post(&full);
           sleep(3);
       }
}



void  *consumer(void* ID)
{
       for(k=0;k<26;k++){
           sem_wait(&full);
           pthread_mutex_lock(&mutex);

           int i;
           printf("consumerID:");
           printf("%s\n",ID);
           printf("out:");
           printf("%d\n",out);
           ConsumeID = buffer[out];
           buffer[out]=0;
           out = (out+1)%SIZE;
           printf("ConsumeID:");
           printf("%d\n", ConsumeID);
            
      
       for(i=0;i<SIZE;i++)
      {
         printf(" %d",buffer);
        
       }
printf("\n");
         
           pthread_mutex_unlock(&mutex);
           sem_post(&empty);
           sleep(3);
       }
}
int main()
{
char a[10][3]={"p1","p2","p3","p4","p5","c1","c2","c3","c4","c5"};
int i;
sem_init(&empty,0,20);
sem_init(&full,0,0);

for(i=0;i<5;i++)  
{
  pthread_create(&producerID,NULL,&producer,a);      
}  
for(i=0;i<5;i++)  
{   
  pthread_create(&consumerID,NULL,&consumer,a[i+5]);
}      
for(i=0;i<5;i++)
{
  pthread_join(producerID,NULL);
pthread_join(consumerID,NULL);
}
return 0;
}

论坛徽章:
0
7 [报告]
发表于 2009-06-27 20:19 |只看该作者

回复 #4 aaaaa5aa 的帖子

可是不管运行多少次,结果是一样的啊,所以我很迷惑

论坛徽章:
0
8 [报告]
发表于 2009-06-27 20:28 |只看该作者
你把k跑到100万呢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP