From: Peng Yu <pengyu.ut@gmail.com>
To: Manfred <mx2927@gmail.com>
Cc: libc-help <libc-help@sourceware.org>,
Florian Weimer <fweimer@redhat.com>
Subject: Re: What is the point of IPC_PRIVATE of shmget?
Date: Mon, 12 Apr 2021 14:05:45 -0500 [thread overview]
Message-ID: <CABrM6wkK-3mURvpp1NMUO_XyhP7QRgmn7tt0q9RK+wqLZe675w@mail.gmail.com> (raw)
In-Reply-To: <9653dac2-7c03-cad0-34fa-94d0101165d8@gmail.com>
On Mon, Apr 12, 2021 at 1:59 PM Manfred via Libc-help
<libc-help@sourceware.org> 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 <fweimer@redhat.com> 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 <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
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
prev parent reply other threads:[~2021-04-12 19:05 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-12 15:54 Peng Yu
2021-04-12 16:06 ` tomas
2021-04-12 16:07 ` Florian Weimer
2021-04-12 16:12 ` Peng Yu
2021-04-12 16:14 ` Florian Weimer
2021-04-12 16:18 ` Florian Weimer
2021-04-12 17:23 ` Peng Yu
2021-04-12 18:17 ` J Lumby
2021-04-12 17:55 ` Manfred
2021-04-12 19:05 ` Peng Yu [this message]
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=CABrM6wkK-3mURvpp1NMUO_XyhP7QRgmn7tt0q9RK+wqLZe675w@mail.gmail.com \
--to=pengyu.ut@gmail.com \
--cc=fweimer@redhat.com \
--cc=libc-help@sourceware.org \
--cc=mx2927@gmail.com \
/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).