QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2842|回复: 5

uclinux的一个多线程编程的问题,大家都过来讨论

[复制链接]
发表于 2006-9-15 16:10:44 | 显示全部楼层 |阅读模式
一个示范的多线程程序,就在主函数里面创建两个线程,然后在两个线程里面是死循环,一直打印。但是一运行就一直重复打印下面的东西:

Allocation of length 2097152 from process 19 failed
Buffer memory:      368kB
Free pages:        4724kB (     0kB HighMem)
Zone:DMA freepages:     0kB min:     0kB low:     0kB high:     0kB
Zone:Normal freepages:  4724kB min:    80kB low:   160kB high:   240kB
Zone:HighMem freepages:     0kB min:     0kB low:     0kB high:     0kB
( Active: 135, inactive: 43, free: 1181 )
= 0kB)
1*4kB 0*8kB 1*16kB 1*32kB 1*64kB 0*128kB 0*256kB 1*512kB 0*1024kB 2*2048kB = 4724kB)
= 0kB)


是什么原因啊?
程序我在linux底下编译而且运行过,没有一点问题。
但是在uclinux底下就这样了。



程序:



#include<stdio.h>
#include<pthread.h>
#include<time.h>
#include<errno.h>

void thread1(void)
{int i=0;
for(i=0;i<6;i++)
        {printf("\tthread1\n");
         if(i==2)
                 pthread_exit(0);

        usleep(5000);
        }

}


void thread2(void)
{
   int i;
   while(1){
             for(i=0;i<3;i++)
                  printf("thread2\n");
             usleep(5000);}
           pthread_exit(0);


}



int main(void)
{
pthread_t id1,id2;
int ret;
pthread_attr_t attr;

//init thread
pthread_attr_init(&attr);

pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM);

pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

//create thread
ret=pthread_create(&id1,&attr,(void *) thread1,NULL);
if(ret!=0){
        printf("create pthread1 error\n");
        exit(1);
        }

ret=pthread_create(&id2,NULL,(void *) thread2,NULL);
if(ret!=0){printf("create pthread2 error\n");
        exit(1);
        }

pthread_join(id1,NULL);       
pthread_join(id2,NULL);

return 0;
}
 楼主| 发表于 2006-9-15 16:12:45 | 显示全部楼层
void thread1(void)
{int i=0;
for(i=0;i<6;i++)
{printf("\tthread1\n");
if(i==2)
pthread_exit(0);

usleep(5000);
}

}

这个应该是个死循环
改了的哪个忘拷出来了
回复

使用道具 举报

 楼主| 发表于 2006-9-15 20:23:12 | 显示全部楼层
各位达人快发表下意见啊。好急啊
回复

使用道具 举报

发表于 2006-9-17 00:07:11 | 显示全部楼层
重新编译一下uClinux,把uClinux分配内存设的大点,试一下!!
回复

使用道具 举报

发表于 2006-9-17 09:40:58 | 显示全部楼层
单独运行一个 pthread 有问题么?
回复

使用道具 举报

 楼主| 发表于 2006-9-21 15:05:53 | 显示全部楼层
pthread_attr_init(&attr);

pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM);

pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);


把这几句去掉就没问题了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-4-27 03:50 , Processed in 0.080617 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表