This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
fopen from within a pthread
- From: Martin Beynon <mebeyn at gmail dot com>
- To: libc-help at sourceware dot org
- Date: Sat, 2 Jun 2018 23:30:23 +0100
- Subject: fopen from within a pthread
Hi,
I've noticed that if I fopen a file from a program in main nothing unusual
happens. However, if do the same from a pthread my virtual memory size
(VMDATA) increases fairly dramatically (~66MB -- 14716 vs 80252).
On closer inspection, with ltrace, I see that between the fopen and the
SYS_open call there is a SYS_mmap followed immediately by SYS_munmap when
called from within a pthread, but not if called from main(..).
Is this erroneous behaviour, or can this be explained in some way?
Sample Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stddef.h>
void* thread_fn(void *args)
{
FILE* f = fopen("afile", "w");//vmsize = 80252KB
while (1) {
sleep(1);
}
return NULL;
}
int main(int argc, char**argv)
{
pthread_t thread;
// FILE* f = fopen("afile", "w");//vmsize = 14716KB
pthread_create(&thread, NULL, thread_fn, NULL);
while (1) {
sleep(1);
}
return NULL;
}
Compiled with:
gcc -o main main.c -lpthread
With ltrace -fS ./main (fopen in main):
[pid 11405] fopen("afile", "w" <unfinished ...>
[pid 11405] SYS_brk(0) = 0xdd0000
[pid 11405] SYS_brk(0xdf1000) = 0xdf1000
[pid 11405] SYS_open("afile", 577, 0666) = 3
[pid 11405] <... fopen resumed> ) = 0xdd0010
cat /proc/../status:
VmPeak: 14716 kB
VmSize: 14716 kB
VmLck: 0 kB
VmPin: 0 kB
VmHWM: 872 kB
VmRSS: 872 kB
VmData: 8400 kB
VmStk: 132 kB
VmExe: 4 kB
VmLib: 2040 kB
VmPTE: 36 kB
VmPMD: 12 kB
VmSwap: 0 kB
With ltrace -fS ./main (fopen within pthread):
[pid 11435] fopen("afile", "w" <unfinished ...>
[pid 11435] SYS_mmap(0, 0x8000000, 0, 0x4022) = 0x7f579d9e3000
[pid 11435] SYS_munmap(0x7f579d9e3000, 39964672) = 0
[pid 11435] SYS_munmap(0x7f57a4000000, 27144192) = 0
[pid 11435] SYS_mprotect(0x7f57a0000000, 135168, 3) = 0
[pid 11435] SYS_open("afile", 577, 0666) = 3
[pid 11435] <... fopen resumed> ) = 0x7f57a00008c0
cat /proc/../status:
VmPeak: 145788 kB
VmSize: 80252 kB
VmLck: 0 kB
VmPin: 0 kB
VmHWM: 872 kB
VmRSS: 872 kB
VmData: 73936 kB
VmStk: 132 kB
VmExe: 4 kB
VmLib: 2040 kB
VmPTE: 40 kB
VmPMD: 12 kB
VmSwap: 0 kB
I'm running on Xubuntu x64 16.04.04 / glibc 2.23 (ldd (Ubuntu GLIBC
2.23-0ubuntu10) 2.23) / gcc version: gcc (Ubuntu 5.4.0-6ubuntu1~16.04.9)
5.4.0 20160609 and for good measure; Linux 4.4.0-127-generic #153-Ubuntu
SMP Sat May 19 10:58:46 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
Thanks,
Martin