public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Martin Beynon <mebeyn@gmail.com>
To: libc-help@sourceware.org
Subject: fopen from within a pthread
Date: Sat, 02 Jun 2018 22:30:00 -0000	[thread overview]
Message-ID: <CADpW=+3ve0oLtby3gneBdiQekjUnqZ66yvhkPUV-LSD9F=dg-g@mail.gmail.com> (raw)

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

             reply	other threads:[~2018-06-02 22:30 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-02 22:30 Martin Beynon [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CADpW=+3ve0oLtby3gneBdiQekjUnqZ66yvhkPUV-LSD9F=dg-g@mail.gmail.com' \
    --to=mebeyn@gmail.com \
    --cc=libc-help@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).