|
楼主 |
发表于 2008-8-12 19:40:40
|
显示全部楼层
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/mman.h>
- #include <pthread.h>
- #include <limits.h>
- void thread_fun()
- {
- int i = 0;
- for(i=0; i < 5; i++)
- {
- printf("hello\n");
- sleep(1);
- }
- }
- int main(int argc, char *argv[])
- {
- int ret = 0;
- pthread_t tid;
- pthread_attr_t attr;
- void *stack_addr;
- size_t stack_size = PTHREAD_STACK_MIN * 4;
- ret = pthread_attr_init(&attr);
- if(ret!=0)
- {
- printf("error line :%d, ret is %d.\n", __LINE__, ret);
- return -1;
- }
- ret = pthread_attr_getstacksize(&attr, &stack_size);
- if(ret!=0)
- {
- printf("error line :%d, ret is %d.\n", __LINE__, ret);
- return -1;
- }
- printf("stack size is %d.\n", stack_size);
- stack_addr = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0 );
- if(stack_addr!=NULL)
- {
- printf("stack is %p.\n", stack_addr);
- }
- else if(stack_addr == MAP_FAILED)
- {
- printf("map failed.\n");
- return -1;
- }
- ret = pthread_attr_setstack(&attr, stack_addr, stack_size);
- if(ret!=0)
- {
- printf("error line :%d, ret is %d.\n", __LINE__, ret);
- return -1;
- }
- ret = pthread_create(&tid, &attr, (void*) thread_fun, NULL);
- if(ret!=0)
- {
- printf("error line :%d, ret is %d.\n", __LINE__, ret);
- return -1;
- }
-
- ret = pthread_join(tid, NULL);
- if(ret!=0)
- {
- printf("error line :%d, ret is %d.\n", __LINE__, ret);
- return -1;
- }
- ret = munmap(stack_addr, stack_size);
- if(ret!=0)
- {
- printf("error line :%d, ret is %d.\n", __LINE__, ret);
- return -1;
- }
- return 0;
- }
复制代码
不过这样的方法不知道常用不常用,本人见识比较浅,还请大家提提意见。 |
|