Пример #1
0
int main()
{
	int port = get_port(12123);
	parent = getpid();
 
   child = fork();
   if (child == -1)
   {   
      fprintf(stderr, "can't fork, error %d\n", errno);
      exit(EXIT_FAILURE);
   }
 
   if (child == 0)
   {
      childproc(port);
      _exit(0);
   }
   else
   { 
	   int ret = parentproc(port);
	   if(1 == session_closed_called && 0 == ret)
      exit(0);
      else
      exit(ret ? ret : 21);
   }
   return 1;
}
int
main()
{
	port = get_port(11123);
	parent = getpid();

	child = fork();
	if (-1 == child)
	{   
		fprintf(stderr, "can't fork, error %d\n", errno);
		exit(EXIT_FAILURE);
	}

	if (child == 0)
	{
		int ret = childproc();
		_exit(ret);
	}
	else
	{ 
		int ret = parentproc();
		exit(ret);
	}
	return 1;
}
Пример #3
0
int main(void)
{
	int i, cpid;

	//create 5 child process
	for(i=0; i<5; i++)
	{
		cpid = fork();
		if(cpid < 0)
			printf("fork failed\n");
		else if(cpid == 0)//child process
			childproc();
	}

	for(i=0; i<5; i++)
		wait(NULL);

	exit(0);
}
Пример #4
0
void*
oscmd(char **args, int nice, char *dir, int *fd)
{
	Targ *t;
	int r, fd0[2], fd1[2], fd2[2], wfd[2], n, pid;

	t = mallocz(sizeof(*t), 1);
	if(t == nil)
		return nil;

	fd0[0] = fd0[1] = -1;
	fd1[0] = fd1[1] = -1;
	fd2[0] = fd2[1] = -1;
	wfd[0] = wfd[1] = -1;
	if(pipe(fd0) < 0 || pipe(fd1) < 0 || pipe(fd2) < 0 || pipe(wfd) < 0)
		goto Error;
	if(fcntl(wfd[1], F_SETFD, FD_CLOEXEC) < 0)	/* close on exec to give end of file on success */
		goto Error;

	t->fd[0] = fd0[0];
	t->fd[1] = fd1[1];
	t->fd[2] = fd2[1];
	t->wfd = wfd[1];
	t->args = args;
	t->dir = dir;
	t->gid = up->env->gid;
	if(t->gid == -1)
		t->gid = gidnobody;
	t->uid = up->env->uid;
	if(t->uid == -1)
		t->uid = uidnobody;

	signal(SIGCHLD, SIG_DFL);
	switch(pid = fork()) {
	case -1:
		goto Error;
	case 0:
		setpgrp();
		if(nice)
			setpriority(PRIO_PROCESS, 0, 19);
		childproc(t);
		_exit(1);
	default:
		t->pid = pid;
		if(Debug)
			print("cmd pid %d\n", t->pid);
		break;
	}

	close(fd0[0]);
	close(fd1[1]);
	close(fd2[1]);
	close(wfd[1]);

	n = read(wfd[0], up->genbuf, sizeof(up->genbuf)-1);
	close(wfd[0]);
	if(n > 0){
		close(fd0[1]);
		close(fd1[0]);
		close(fd2[0]);
		free(t);
		up->genbuf[n] = 0;
		if(Debug)
			print("oscmd: bad exec: %q\n", up->genbuf);
		error(up->genbuf);
		return nil;
	}

	fd[0] = fd0[1];
	fd[1] = fd1[0];
	fd[2] = fd2[0];
	return t;

Error:
	r = errno;
	if(Debug)
		print("oscmd: %q\n",strerror(r));
	close(fd0[0]);
	close(fd0[1]);
	close(fd1[0]);
	close(fd1[1]);
	close(fd2[0]);
	close(fd2[1]);
	close(wfd[0]);
	close(wfd[1]);
	error(strerror(r));
	return nil;
}