[lxc-devel] do pid1 and pid2 run in the same network namespace
    Daniel Lezcano 
    daniel.lezcano at free.fr
       
    Tue Feb 22 11:56:12 UTC 2011
    
    
  
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