public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
* fopen from within a pthread
@ 2018-06-02 22:30 Martin Beynon
  2018-06-02 22:49 ` Paul Pluzhnikov via libc-help
  0 siblings, 1 reply; 4+ messages in thread
From: Martin Beynon @ 2018-06-02 22:30 UTC (permalink / raw)
  To: libc-help

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-06-04 16:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-02 22:30 fopen from within a pthread Martin Beynon
2018-06-02 22:49 ` Paul Pluzhnikov via libc-help
2018-06-04 14:38   ` Carlos O'Donell
2018-06-04 16:13     ` Martin Beynon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).