int main(void)
{
	int sp[2];

	if (-1 == socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) {
		fprintf(stderr, "socketpair(): %s\n", strerror(errno));
		exit(EXIT_FAILURE);
	}

	switch (fork()) {
	case -1:
		fprintf(stderr, "fork(): %s\n", strerror(errno));
		exit(EXIT_FAILURE);
	case 0:
		close(sp[0]);
		do_child(sp[1]);
		break;
	default:
		close(sp[1]);
		do_parent(sp[0]);
		break;
	}

	exit(EXIT_SUCCESS);
}
Beispiel #2
0
int main(int argc, char **argv) {
  int forkres, i;

  /* Get subtest number */
  if(argc != 2) {
    printf("Usage: %s subtest_no\n", argv[0]);
    exit(-2);
  } else if(sscanf(argv[1], "%d", &subtest) != 1) {
    printf("Usage: %s subtest_no\n", argv[0]);
    exit(-2);
  }
  
  /* Fork off a bunch of children */
  for(i = 0; i < NUMCHILDREN; i++) {
      forkres = fork();
      if(forkres == 0) do_child(i);
      else if(forkres < 0) {
	perror("Unable to fork");
	exit(-1);
      }
  }
  /* do_child always calls exit(), so when we end up here, we're the parent. */
  do_parent();

  exit(-2); /* We're not supposed to get here. Both do_* routines should exit.*/
}
Beispiel #3
0
int
main(int argc, char *argv[])
{
    int rank, size;
    MPI_Comm parent;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    /* Check to see if we *were* spawned -- because this is a test, we
       can only assume the existence of this one executable.  Hence, we
       both mpirun it and spawn it. */

    parent = MPI_COMM_NULL;
    MPI_Comm_get_parent(&parent);
    if (parent != MPI_COMM_NULL) {
        whoami = argv[1];
        do_target(argv, parent);
    } else {
        do_parent(argv, rank, size);
    }

    /* All done */
    MPI_Finalize();
    return 0;
}
Beispiel #4
0
int main(int argc, char **argv) {
  int forkres;

  /* Get subtest number */
  if(argc != 2) {
    printf("Usage: %s subtest_no\n", argv[0]);
    exit(-2);
  } else if(sscanf(argv[1], "%d", &subtest) != 1) {
    printf("Usage: %s subtest_no\n", argv[0]);
    exit(-2);
  }
  
  /* Set up anonymous pipe */
  if(pipe(fd_ap) < 0) {
    perror("Could not create anonymous pipe");
    exit(-1);
  }

  forkres = fork();
  if(forkres == 0) do_child();
  else if(forkres > 0)  do_parent(forkres);
  else { /* Fork failed */
    perror("Unable to fork");
    exit(-1);
  }

  exit(-2); /* We're not supposed to get here. Both do_* routines should exit*/
  
}
Beispiel #5
0
int main(int argc, char **argv) {
    int forkres;
    int master, slave;

    /* Get subtest number */
    if(argc != 2) {
        printf("Usage: %s subtest_no\n", argv[0]);
        exit(-1);
    } else if(sscanf(argv[1], "%d", &subtest) != 1) {
        printf("Usage: %s subtest_no\n", argv[0]);
        exit(-1);
    }

    open_terminal(&master, &slave);

    forkres = fork();
    if(forkres == 0) do_child(master);
    else if(forkres > 0)  do_parent(forkres, slave);
    else { /* Fork failed */
        perror("Unable to fork");
        exit(-1);
    }

    exit(-2); /* We're not supposed to get here. Both do_* routines should exit*/

}
Beispiel #6
0
/* and the main function. */
int main(int argc, char* argv[])
{
    int data_pipe[2]; /* an array to store the file descriptors of the pipe. */
    int pid;       /* pid of child process, or 0, as returned via fork.    */
    int rc;        /* stores return values of various routines.            */

    /* first, create a pipe. */
    rc = pipe(data_pipe);
    if (rc == -1) {
	perror("pipe");
	exit(1);
    }

    /* now fork off a child process, and set their handling routines. */
    pid = fork();

    switch (pid) {
	case -1:	/* fork failed. */
	    perror("fork");
	    exit(1);
	case 0:		/* inside child process.  */
	    do_child(data_pipe);
	    /* NOT REACHED */
	default:	/* inside parent process. */
	    do_parent(data_pipe);
	    /* NOT REACHED */
    }

    return 0;	/* NOT REACHED */
}
Beispiel #7
0
int main(void)
{
  pid_t r;
  subtest = 1;

#ifdef __GNUC__
	printf("Test 52 (GCC) ");
#else
	printf("Test 52 (ACK) ");
#endif
  fflush(stdout);

  if (pipe(pipefdc) == -1) err(1);
  if (pipe(pipefdp) == -1) err(2);

  r = fork();
  if(r < 0) {
	err(3);
  } else if(r == 0) {
	/* Child */
	do_child();
  } else {
	/* Parent */
	do_parent();
  }

  return(0); /* Never reached */

}
Beispiel #8
0
int main(int argc, char** argv)
{
  // We need to keep the original parameters in order to pass them to the
  // model-checked process:
  int argc_copy = argc;
  char** argv_copy = argvdup(argc, argv);
  xbt_log_init(&argc_copy, argv_copy);
  sg_config_init(&argc_copy, argv_copy);

  if (argc < 2)
    xbt_die("Missing arguments.\n");

  bool server_mode = true;
  char* env = std::getenv("SIMGRID_MC_MODE");
  if (env) {
    if (std::strcmp(env, "server") == 0)
      server_mode = true;
    else if (std::strcmp(env, "standalone") == 0)
      server_mode = false;
    else
      xbt_die("Unrecognised value for SIMGRID_MC_MODE (server/standalone)");
  }

  if (!server_mode) {
    setenv(MC_ENV_VARIABLE, "1", 1);
    execvp(argv[1], argv+1);

    std::perror("simgrid-mc");
    return 127;
  }

  // Create a AF_LOCAL socketpair:
  int res;

  int sockets[2];
  res = socketpair(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockets);
  if (res == -1) {
    perror("simgrid-mc");
    return MC_SERVER_ERROR;
  }

  XBT_DEBUG("Created socketpair");

  pid_t pid = fork();
  if (pid < 0) {
    perror("simgrid-mc");
    return MC_SERVER_ERROR;
  } else if (pid == 0) {
    close(sockets[1]);
    return do_child(sockets[0], argv);
  } else {
    close(sockets[0]);
    return do_parent(sockets[1], pid);
  }

  return 0;
}
Beispiel #9
0
/**
 *  Fork/exec the specified processes
 */
static int fork_local_proc(orte_app_context_t* context,
                           orte_odls_child_t *child,
                           char **environ_copy,
                           orte_odls_job_t *jobdat)
{
    orte_iof_base_io_conf_t opts;
    int rc, p[2];
    pid_t pid;
    
    /* we do not forward io, so mark it as complete */
    child->iof_complete = true;

    /* A pipe is used to communicate between the parent and child to
       indicate whether the exec ultimately succeeded or failed.  The
       child sets the pipe to be close-on-exec; the child only ever
       writes anything to the pipe if there is an error (e.g.,
       executable not found, exec() fails, etc.).  The parent does a
       blocking read on the pipe; if the pipe closed with no data,
       then the exec() succeeded.  If the parent reads something from
       the pipe, then the child was letting us know why it failed. */
    if (pipe(p) < 0) {
        ORTE_ERROR_LOG(ORTE_ERR_SYS_LIMITS_PIPES);
        if (NULL != child) {
            child->state = ORTE_PROC_STATE_FAILED_TO_START;
            child->exit_code = ORTE_ERR_SYS_LIMITS_PIPES;
        }
        return ORTE_ERR_SYS_LIMITS_PIPES;
    }
    
    /* Fork off the child */
    pid = fork();
    if (NULL != child) {
        child->pid = pid;
    }
    
    if (pid < 0) {
        ORTE_ERROR_LOG(ORTE_ERR_SYS_LIMITS_CHILDREN);
        if (NULL != child) {
            child->state = ORTE_PROC_STATE_FAILED_TO_START;
            child->exit_code = ORTE_ERR_SYS_LIMITS_CHILDREN;
        }
        return ORTE_ERR_SYS_LIMITS_CHILDREN;
    }
    
    if (pid == 0) {
	close(p[0]);
        do_child(context, child, environ_copy, jobdat, p[1], opts);
        /* Does not return */
    } 

    close(p[1]);
    return do_parent(context, child, environ_copy, jobdat, p[0], opts);
}
Beispiel #10
0
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  ACE_LOG_MSG->open (argv[0]);

  if (argc != 2)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("usage: %n input-file\n"),
                  1));
      ACE_OS::exit (1);
    }

  ACE_FIFO_Recv fifo_reader (FIFO_NAME, O_RDONLY | O_CREAT, PERMS, 0);

  if (fifo_reader.get_handle () == ACE_INVALID_HANDLE)
    return -1;

  pid_t child_pid = ACE_OS::fork ();

  switch (child_pid)
    {
    case -1:
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("%n: %p\n%a"),
                  ACE_TEXT ("fork"),
                  1));
    case 0:
      if (do_child (fifo_reader) == -1)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("%n: %p\n%a"),
                    ACE_TEXT ("do_child"),
                    1));
    default:
      if (do_parent (FIFO_NAME, argv[1]) == -1)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("%n: %p\n%a"),
                    ACE_TEXT ("do_parent"),
                    1));

      // wait for child to ACE_OS::exit.
      if (ACE_OS::waitpid (child_pid, (ACE_exitcode *) 0, 0) == -1)
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("%n: %p\n%a"),
                    ACE_TEXT ("waitpid"),
                    1));
    }

  return 0;
}
Beispiel #11
0
/**
 *  Fork/exec the specified processes
 */
static int odls_alps_fork_local_proc(void *cdptr)
{
    orte_odls_spawn_caddy_t *cd = (orte_odls_spawn_caddy_t*)cdptr;
    int p[2];
    pid_t pid;

    /* A pipe is used to communicate between the parent and child to
       indicate whether the exec ultimately succeeded or failed.  The
       child sets the pipe to be close-on-exec; the child only ever
       writes anything to the pipe if there is an error (e.g.,
       executable not found, exec() fails, etc.).  The parent does a
       blocking read on the pipe; if the pipe closed with no data,
       then the exec() succeeded.  If the parent reads something from
       the pipe, then the child was letting us know why it failed. */
    if (pipe(p) < 0) {
        ORTE_ERROR_LOG(ORTE_ERR_SYS_LIMITS_PIPES);
        if (NULL != cd->child) {
            cd->child->state = ORTE_PROC_STATE_FAILED_TO_START;
            cd->child->exit_code = ORTE_ERR_SYS_LIMITS_PIPES;
        }
        return ORTE_ERR_SYS_LIMITS_PIPES;
    }

    /* Fork off the child */
    pid = fork();
    if (NULL != cd->child) {
        cd->child->pid = pid;
    }

    if (pid < 0) {
        ORTE_ERROR_LOG(ORTE_ERR_SYS_LIMITS_CHILDREN);
        if (NULL != cd->child) {
            cd->child->state = ORTE_PROC_STATE_FAILED_TO_START;
            cd->child->exit_code = ORTE_ERR_SYS_LIMITS_CHILDREN;
        }
        return ORTE_ERR_SYS_LIMITS_CHILDREN;
    }

    if (pid == 0) {
        close(p[0]);
#if HAVE_SETPGID
        setpgid(0, 0);
#endif
        do_child(cd, p[1]);
        /* Does not return */
    }

    close(p[1]);
    return do_parent(cd, p[0]);
}
Beispiel #12
0
static
atf_error_t
fork_with_streams(atf_process_child_t *c,
                  void (*start)(void *),
                  const atf_process_stream_t *outsb,
                  const atf_process_stream_t *errsb,
                  void *v)
{
    atf_error_t err;
    stream_prepare_t outsp;
    stream_prepare_t errsp;
    pid_t pid;

    err = stream_prepare_init(&outsp, outsb);
    if (atf_is_error(err))
        goto out;

    err = stream_prepare_init(&errsp, errsb);
    if (atf_is_error(err))
        goto err_outpipe;

    pid = fork();
    if (pid == -1) {
        err = atf_libc_error(errno, "Failed to fork");
        goto err_errpipe;
    }

    if (pid == 0) {
        do_child(start, v, &outsp, &errsp);
        UNREACHABLE;
        abort();
        err = atf_no_error();
    } else {
        err = do_parent(c, pid, &outsp, &errsp);
        if (atf_is_error(err))
            goto err_errpipe;
    }

    goto out;

err_errpipe:
    stream_prepare_fini(&errsp);
err_outpipe:
    stream_prepare_fini(&outsp);

out:
    return err;
}
Beispiel #13
0
int main(int argc, char** argv) {
  setup_shared_port();

  pid_t child_pid = fork();
  if (child_pid == -1) {
    FAIL("forking");
  }

  if (child_pid == 0) {
    mach_port_t shared_port_child = recover_shared_port_child();
    do_child(shared_port_child);
  } else {
    mach_port_t shared_port_parent = recover_shared_port_parent();
    mach_port_t child_task_port = do_parent(shared_port_parent);
    sploit(child_pid, child_task_port);
  }

  return 0;
}
Beispiel #14
0
int main(int argc, char** argv) {
    int done = 0;
    kern_return_t err;
    mach_port_t exception_port = MACH_PORT_NULL;
    err = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &exception_port);
    if (err != KERN_SUCCESS) {
        printf("[-] failed to allocate port\n");
        return 1;
    }

    err = mach_port_insert_right(mach_task_self(),
                                 exception_port,
                                 exception_port,
                                 MACH_MSG_TYPE_MAKE_SEND);

    if (err != KERN_SUCCESS) {
        printf("[-] failed to insert send right\n");
        return 1;
    }

    while (!done) {
        setup_shared_port();
        pid_t child_pid = fork();
        if (child_pid == -1) {
            FAIL("forking");
        }

        if (child_pid == 0) {
            mach_port_t shared_port_child = recover_shared_port_child();
            do_child(shared_port_child);
            sploit_child();
        } else {
            mach_port_t shared_port_parent = recover_shared_port_parent();
            mach_port_t child_task_port = do_parent(shared_port_parent);
            done = sploit_parent(child_task_port, exception_port);
            int sl;
            wait(&sl);
        }
    }

    mach_port_destroy(mach_task_self(), exception_port);
    return 0;
}
Beispiel #15
0
int
main(int argc, char **argv)
{
	if (argc != 1)
		err_quit("usage: qlen");

	Socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd);

	bzero(&serv, sizeof(serv));
	serv.sin_family = AF_INET;
	serv.sin_port = htons(PORT);
	Inet_pton(AF_INET, ADDR, &serv.sin_addr);

	if ( (pid = Fork()) == 0)
		do_child();
	else
		do_parent();
		
	exit(0);
}
int main(void)
{
	// index array
	int x = 0;

	// semaphore stuff
	emptysem  = semget(IPC_PRIVATE,1,SEMPERM|IPC_CREAT|IPC_EXCL);
	empty.val = 0;
	rc1 = semctl (emptysem_id, 0, SETVAL, empty);  
	fullsem  = semget(IPC_PRIVATE,1,SEMPERM|IPC_CREAT|IPC_EXCL);
	full.val = initialization value;
	rc2 = semctl (fullsem_id, 0, SETVAL, full);  
	mutexsem  = semget(IPC_PRIVATE,1,SEMPERM|IPC_CREAT|IPC_EXCL);
	mutex.val = 1;
	rc3 = semctl (mutexsem_id, 0, SETVAL, mutex);  

	//request segment
	segid = shmget(IPC_PRIVATE,SIZE,IPC_CREAT | IPC_EXCL | SHMPERM);

	//check system call return
	if(segid<0) {
		perror(strerror(errno));
		exit(1);
		}

	//fill array
	for(x = 0 ; x < MAX ; x+=1)
		{data[x] = x;}

	//spawn child process
	retcheck = fork();
	//Begin consumer/producer by retcheck
	if(retcheck == 0)
		{do_child();}
	else {
		retcheck == fork(); 
		if (retcheck == 0)
			{do child2()};
	 else do_parent();
	}
int main(int argc, char *argv[])
{
	int ptys[2];
	int retval;
	int pid;	
	if(openpty(&ptys[0], &ptys[1], name, NULL, NULL) < 0) {
		perror("openpty");
		return 1;
	}
	printf("Using %s\n", name);
	pid = fork();
	if (pid == -1) {
		fprintf(stderr, "Error forking\n");
		exit(-1);
	}
	if (pid == 0) /* child proc */
		do_child(ptys);
	else
		do_parent(ptys);
	/* not reached */
	return 0;
}
Beispiel #18
0
int main(void)
{
	pid_t	pid;

	if (pipe(pipe_parent) < 0)
		err_sys("pipe error");
	if (pipe(pipe_child) < 0)
		err_sys("pipe error");

	if ((pid = fork()) < 0)
		err_sys("fork error");

	else if (pid > 0) {		/* parent */
		close(pipe_parent[0]); /* close read end of pipe */
		close(pipe_child[1]); /* close write end of pipe */
		do_parent();
	} else {				/* child */
		close(pipe_parent[1]); /* close write end of pipe */
		close(pipe_child[0]); /* close read end of pipe */
		do_child();
	}
	exit(EXIT_SUCCESS);
}
Beispiel #19
0
int main(void)
{
  pid_t r;
  subtest = 1;

  start(52);

  if (pipe(pipefdc) == -1) err(1);
  if (pipe(pipefdp) == -1) err(2);

  r = fork();
  if(r < 0) {
	err(3);
  } else if(r == 0) {
	/* Child */
	do_child();
  } else {
	/* Parent */
	do_parent();
  }

  return(0); /* Never reached */

}
Beispiel #20
0
void test(parent_exit_t parent_exit_time, debugger_exit_t debugger_exit_time)
{
    pid_t parent, child, debugger;
    int ret;
    int fds[2];

    /* pipe for parent to send child pid to grandparent */
    ret = pipe(fds);
    if (-1 == ret) {
        err(1, "failed to create pipe");
    }

    parent = fork();
    if (parent == 0) {
        /* parent sub-branch */

        ret = close(fds[0]);
        if (ret == -1) {
            err(1, "close read end of pipe");
        }

        child = fork();
        if (child == 0) {
            /* child */
            ret = close(fds[1]);
            if (ret == -1) {
                err(1, "close write end of pipe");
            }

            do_child();
        } else if (child == -1) {
            err(1, "parent failed to fork child");
        } else {
            /* parent */
            if (-1 == write(fds[1], &child, sizeof(child))) {
                err(1, "writing child pid to grandparent");
            }

            if (parent_exit_time == eParentIsDebugger) {
                debugger = -1;

                if (-1 == write(fds[1], &debugger, sizeof(debugger))) {
                    err(1, "writing debugger pid to grandparent");
                }
                ret = close(fds[1]);
                if (ret == -1) {
                    err(1, "close write end of pipe");
                }

                do_debugger(child, debugger_exit_time);
            } else {
                debugger = fork();
                if (debugger == 0) {
                    /* debugger */
                    ret = close(fds[1]);
                    if (ret == -1) {
                        err(1, "close write end of pipe");
                    }

                    do_debugger(child, debugger_exit_time);
                } else if (debugger == -1) {
                    err(1, "parent failed to fork debugger");
                } else {
                    /* still parent */
                    if (-1 == write(fds[1], &debugger, sizeof(debugger))) {
                        err(1, "writing debugger pid to grandparent");
                    }
                    ret = close(fds[1]);
                    if (ret == -1) {
                        err(1, "close write end of pipe");
                    }

                    do_parent(child, debugger, parent_exit_time, debugger_exit_time);
                }
            }
        }
    } else if (parent == -1) {
        err(1, "grandparent failed to fork parent");
    } else {
        ret = close(fds[1]);
        if (ret == -1) {
            err(1, "close write end of pipe");
        }

        if (-1 == read(fds[0], &child, sizeof(child))) {
            err(1, "could not read child pid");
        }

        if (-1 == read(fds[0], &debugger, sizeof(debugger))) {
            err(1, "could not read debugger pid");
        }

        ret = close(fds[0]);
        if (ret == -1) {
            err(1, "close read end of pipe");
        }

        do_grandparent(parent, child, debugger, debugger_exit_time);
    }
}
Beispiel #21
0
/**
 *  Fork/exec the specified processes
 */
static int odls_default_fork_local_proc(orte_app_context_t* context,
                                        orte_proc_t *child,
                                        char **environ_copy,
                                        orte_job_t *jobdat)
{
    orte_iof_base_io_conf_t opts;
    int rc, p[2];
    pid_t pid;
    
    if (NULL != child) {
        /* should pull this information from MPIRUN instead of going with
         default */
        opts.usepty = OPAL_ENABLE_PTY_SUPPORT;
        
        /* do we want to setup stdin? */
        if (NULL != child &&
            (jobdat->stdin_target == ORTE_VPID_WILDCARD ||
             child->name.vpid == jobdat->stdin_target)) {
            opts.connect_stdin = true;
        } else {
            opts.connect_stdin = false;
        }
        
        if (ORTE_SUCCESS != (rc = orte_iof_base_setup_prefork(&opts))) {
            ORTE_ERROR_LOG(rc);
            if (NULL != child) {
                child->state = ORTE_PROC_STATE_FAILED_TO_START;
                child->exit_code = rc;
            }
            return rc;
        }
    }

    /* A pipe is used to communicate between the parent and child to
       indicate whether the exec ultimately succeeded or failed.  The
       child sets the pipe to be close-on-exec; the child only ever
       writes anything to the pipe if there is an error (e.g.,
       executable not found, exec() fails, etc.).  The parent does a
       blocking read on the pipe; if the pipe closed with no data,
       then the exec() succeeded.  If the parent reads something from
       the pipe, then the child was letting us know why it failed. */
    if (pipe(p) < 0) {
        ORTE_ERROR_LOG(ORTE_ERR_SYS_LIMITS_PIPES);
        if (NULL != child) {
            child->state = ORTE_PROC_STATE_FAILED_TO_START;
            child->exit_code = ORTE_ERR_SYS_LIMITS_PIPES;
        }
        return ORTE_ERR_SYS_LIMITS_PIPES;
    }
    
    /* Fork off the child */
    pid = fork();
    if (NULL != child) {
        child->pid = pid;
    }
    
    if (pid < 0) {
        ORTE_ERROR_LOG(ORTE_ERR_SYS_LIMITS_CHILDREN);
        if (NULL != child) {
            child->state = ORTE_PROC_STATE_FAILED_TO_START;
            child->exit_code = ORTE_ERR_SYS_LIMITS_CHILDREN;
        }
        return ORTE_ERR_SYS_LIMITS_CHILDREN;
    }
    
    if (pid == 0) {
	close(p[0]);
#if HAVE_SETPGID
        setpgid(0, 0);
#endif
        do_child(context, child, environ_copy, jobdat, p[1], opts);
        /* Does not return */
    } 

    close(p[1]);
    return do_parent(context, child, environ_copy, jobdat, p[0], opts);
}