[lxc-devel] do pid1 and pid2 run in the same network namespace
Daniel Lezcano
daniel.lezcano at free.fr
Wed Feb 23 08:53:17 UTC 2011
On 02/23/2011 08:39 AM, Maheswara Reddy C - ERS, HCL Tech wrote:
> Hi Daniel,
>
> We could able to create ten (N) number of namespaces, how can we identify the specified namespace.
>
> Shall we use the return value of unshare() , as unique namespace ID ?
No, the unshare returns 0 on success or < 0 on error.
There is no mechanism to assign a namespace an id. May be you can use
the pid number of the first process in the network namespace as an
unique identifier.
> Thanks
> Mahesh
> -----Original Message-----
> From: Daniel Lezcano [mailto:daniel.lezcano at free.fr]
> Sent: Tuesday, February 22, 2011 5:26 PM
> To: Maheswara Reddy C - ERS, HCL Tech
> Cc: lxc-devel at lists.sourceforge.net
> Subject: Re: [lxc-devel] do pid1 and pid2 run in the same network namespace
>
> On 02/22/2011 12:22 PM, Maheswara Reddy C - ERS, HCL Tech wrote:
>>> Then it is quite easy. You just have to fork ten times the routine in the program I gave you in the previous email.
>> Hi Daniel,
>>
>> But I want to run two different (fork() run same copy) process/threads in each namespace, that's why I am using clone() which take function pointer of each process/thered to start.
> Have fun ;)
>
> #include<errno.h>
> #include<sched.h>
> #include<stdio.h>
> #include<unistd.h>
> #include<stdlib.h>
> #include<sys/types.h>
> #include<sys/wait.h>
>
> #define NRNS 10
>
> int myroutine1(void)
> {
> return 0;
> }
>
> int myroutine2(void)
> {
> return 0;
> }
>
> int main(int argc, char *argv[])
> {
>
> int i;
> pid_t pid;
>
> for (i = 0; i< NRNS; i++) {
>
> /* create a new network namespace for my childs */
> if (unshare(CLONE_NEWNET)) {
> perror("unshare");
> return 1;
> }
>
> pid = fork();
> if (pid< 0) {
> perror("fork");
> return 1;
> }
>
> if (!pid)
> exit(myroutine1());
>
> pid = fork();
> if (pid< 0) {
> perror("fork2");
> return 1;
> }
>
> if (!pid)
> exit(myroutine2());
>
> }
>
> /* create a new netns to not share with the last one
> * At the end we have 10 netns + 1 this one
> */
> if (unshare(CLONE_NEWNET)) {
> perror("unshare2");
> return 1;
> }
>
> for (;;) {
>
> pid = wait(NULL);
> if (pid< 0) {
> if (errno == ECHILD)
> return 0;
> if (errno == EINTR)
> continue;
> perror("wait");
> return 1;
> }
> }
>
> }
More information about the lxc-devel
mailing list