- 論壇徽章:
- 1
|
我寫了個小測試程序 是非分離的線程在沒使用pthread_join來釋放其資源的情況下,我想驗(yàn)證這種情況下雖然線程函數(shù)執(zhí)行完畢退出了,但因?yàn)闆]有用pthread_join來釋放其資源,所以線程的函數(shù)?臻g仍沒有被釋放,無法被使用。
代碼如下:
void* fun(void* arg);
int main()
{
int i = 0;
int threadNum = 0;
for(i=0; i<10000000; i++)
{
sleep(1);
pthread_t tid;
int err = pthread_create(&tid, NULL, fun, NULL);
if(err != 0)
{
printf("errno is %d\n", err);
printf("err is %s.\n", strerror(err));
}
else
{
printf("tid is %lu, threadNum is %d \n", tid, ++threadNum);
}
}
sleep(100000);
return 0;
}
void* fun(void* arg)
{
int a[1000];
memset(a, 0, 1000*sizeof(int));
printf("%s\n", "This is in fun.");
return NULL;
}
函數(shù)的打印結(jié)果是:
tid is 3086392208, threadNum is 1
This is in fun.
tid is 3077999504, threadNum is 2
This is in fun.
tid is 3069606800, threadNum is 3
This is in fun.
tid is 3061214096, threadNum is 4
This is in fun.
tid is 3052821392, threadNum is 5
This is in fun.
tid is 3044428688, threadNum is 6
This is in fun.
tid is 3036035984, threadNum is 7
This is in fun.
tid is 3027643280, threadNum is 8
This is in fun.
。。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。。
一直在打印,且tid在不斷變化(因?yàn)榕f的線程的資源沒有被釋放,其id號不能被新的線程繼續(xù)使用),運(yùn)行一段時間后出現(xiàn)錯誤,打印
errno is 12
err is Cannot allocate memory.
errno is 12
err is Cannot allocate memory.
errno is 12
err is Cannot allocate memory.
我使用 pmap -d +進(jìn)程號
看到的內(nèi)存情況是這樣的:
[root@host snmpmanager]# pmap -d 10626
10626: ./ notdetch
Address Kbytes Mode Offset Device Mapping
08048000 4 r-x-- 0000000000000000 0fd:00000 notdetch
08049000 4 rw--- 0000000000000000 0fd:00000 notdetch
0804a000 132 rw--- 000000000804a000 000:00000 [ anon ]
416fd000 76 r-x-- 0000000000000000 0fd:00000 libpthread-2.5.so
41710000 4 r---- 0000000000012000 0fd:00000 libpthread-2.5.so
41711000 4 rw--- 0000000000013000 0fd:00000 libpthread-2.5.so
41712000 8 rw--- 0000000041712000 000:00000 [ anon ]
475e9000 100 r-x-- 0000000000000000 0fd:00000 ld-2.5.so
47602000 4 r---- 0000000000018000 0fd:00000 ld-2.5.so
47603000 4 rw--- 0000000000019000 0fd:00000 ld-2.5.so
4bc09000 1244 r-x-- 0000000000000000 0fd:00000 libc-2.5.so
4bd40000 8 r---- 0000000000137000 0fd:00000 libc-2.5.so
4bd42000 4 rw--- 0000000000139000 0fd:00000 libc-2.5.so
4bd43000 12 rw--- 000000004bd43000 000:00000 [ anon ]
af7b4000 4 ----- 00000000af7b4000 000:00000 [ anon ]
af7b5000 8192 rw--- 00000000af7b5000 000:00000 [ anon ]
affb5000 4 ----- 00000000affb5000 000:00000 [ anon ]
affb6000 8192 rw--- 00000000affb6000 000:00000 [ anon ]
b07b6000 4 ----- 00000000b07b6000 000:00000 [ anon ]
b07b7000 8192 rw--- 00000000b07b7000 000:00000 [ anon ]
b0fb7000 4 ----- 00000000b0fb7000 000:00000 [ anon ]
b0fb8000 8192 rw--- 00000000b0fb8000 000:00000 [ anon ]
b17b8000 4 ----- 00000000b17b8000 000:00000 [ anon ]
b17b9000 8192 rw--- 00000000b17b9000 000:00000 [ anon ]
b1fb9000 4 ----- 00000000b1fb9000 000:00000 [ anon ]
b1fba000 8192 rw--- 00000000b1fba000 000:00000 [ anon ]
b27ba000 4 ----- 00000000b27ba000 000:00000 [ anon ]
b27bb000 8192 rw--- 00000000b27bb000 000:00000 [ anon ]
。。。。。。。。。。。。。。。。。
發(fā)現(xiàn)8142在不斷增多,一直到程序打印
errno is 12
err is Cannot allocate memory.后,pmap的輸出才不再增加,
我想問的是pmap的輸出中,所出現(xiàn)的那么多8142是不是就是每個線程未釋放的函數(shù)?臻g呢?
anon是什么意思?
謝謝 請大家看下~~~ |
|