From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x133.google.com (mail-il1-x133.google.com [IPv6:2607:f8b0:4864:20::133]) by sourceware.org (Postfix) with ESMTPS id 84F63383582B for ; Mon, 12 Apr 2021 19:05:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 84F63383582B Received: by mail-il1-x133.google.com with SMTP id z9so11980396ilb.4 for ; Mon, 12 Apr 2021 12:05:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=JLI6bw+Uh/n7BYPdBImXamqoacxTmaYrxLSZjk0ixU8=; b=LtPQKw7UqTzHp6m1e0N5klZa8hwchn2uST2Grt7dtC1ayuPyamhmDAGIX+cWIuNPy1 CXqzyya68w7TYjFDWXhAWO/E5wgRsokue4rtC6HfggJr9SSNSPJAQv0lQJVeYypIKYl3 b7Wvh/7LAzmIGeWIFZ9sif85uepeXik9Tw68nkfz31r/HqzdsfnapcuOQt6gc4izo/9E HpHZOrng69J1TmVzx4dhgn8+L811Bt5fy0kU21nhAz0IaOFovOniFEOwm4QlGcfypZVr HHVsfWxp88y1GrcQXY+KdfYpmvEa+75kAE8JjnuQZgyfKj8ooTDmKethJSXNChyTZm6I Pscw== X-Gm-Message-State: AOAM530lZVWYzdyBW0hd8NtNVOWnZJXhW28wWVzPtDjnyIpmnJ2ICMIC xkoPdiTn+bdNPPcvdsqpkHfoxnIyVh6nw4w8/iQ= X-Google-Smtp-Source: ABdhPJzMv/8pUNFErA6jIKX1UnDLzJEPUIjFQSbP0q3ScuQKqU+kX9TVMt4IICCtRYWFjWwAKrpt1E3hX1wODC4zZ5E= X-Received: by 2002:a05:6e02:1baf:: with SMTP id n15mr5207737ili.148.1618254357080; Mon, 12 Apr 2021 12:05:57 -0700 (PDT) MIME-Version: 1.0 References: <87wnt7iizx.fsf@oldenburg.str.redhat.com> <87sg3viio7.fsf@oldenburg.str.redhat.com> <9653dac2-7c03-cad0-34fa-94d0101165d8@gmail.com> In-Reply-To: <9653dac2-7c03-cad0-34fa-94d0101165d8@gmail.com> From: Peng Yu Date: Mon, 12 Apr 2021 14:05:45 -0500 Message-ID: Subject: Re: What is the point of IPC_PRIVATE of shmget? To: Manfred Cc: libc-help , Florian Weimer Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-help@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-help mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 12 Apr 2021 19:05:59 -0000 On Mon, Apr 12, 2021 at 1:59 PM Manfred via Libc-help wrote: > > > > On 4/12/2021 6:14 PM, Florian Weimer via Libc-help wrote: > > * Peng Yu: > > > >> On Mon, Apr 12, 2021 at 11:07 AM Florian Weimer wrote: > >>> > >>> * Peng Yu via Libc-help: > >>> > >>>> I don't get the point of IPC_PRIVATE of shmget(). Since it is just > >>>> used by the current process, why not just use malloc? > >>>> > >>>> Can anybody give a real example in which IPC_PRIVATE must be used, but > >>>> malloc or other variant of *alloc functions are not appropriate to > >>>> use? Thanks. > >>> > >>> shmget(2) says this: > >>> > >>> | BUGS > >>> | The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW > >>> | would more clearly show its function. > >>> > >>> Does this answer your question? > >> > >> No. That is just a naming issue. It is still privately used by the > >> current process instead of other processes. > > > > Oh, it can be shared if the process forks. With malloc, you'd lose the > > sharing. > > Out of curiosity, > Does it need to fork? Can the identifier be shared in some other way? I had the same question. I made a test case for it. It seems that it doesn't matter whether fork is used or not. My test shows that as long as another process obtains the id somehow, it can access it. Can this conclusion be safely made? ==> main.c <== // vim: set noexpandtab tabstop=2: #include #include #include int main(int argc, char *argv[]) { int shmid = atoi(argv[1]); char *str = shmat(shmid, NULL, 0); printf("%s\n",str); return 0; } ==> main_create.c <== // vim: set noexpandtab tabstop=2: #include #include #include #include int main(int argc, char *argv[]) { key_t key = atoi(argv[1]); int shmid; if((shmid = shmget(key, 1024, 0666|IPC_CREAT)) == -1) { perror("shmget()"); return 1; } printf("%d\n", shmid); char *str = shmat(shmid, NULL, 0); strcpy(str, argv[2]); return 0; } $ shmid=$(./main_create 0 xyz); ./main.exe "$shmid" xyz -- Regards, Peng