当前位置: 首页>>代码示例>>C++>>正文


C++ pipe2函数代码示例

本文整理汇总了C++中pipe2函数的典型用法代码示例。如果您正苦于以下问题:C++ pipe2函数的具体用法?C++ pipe2怎么用?C++ pipe2使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了pipe2函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: g_unix_open_pipe

/**
 * g_unix_open_pipe:
 * @fds: Array of two integers
 * @flags: Bitfield of file descriptor flags, as for fcntl()
 * @error: a #GError
 *
 * Similar to the UNIX pipe() call, but on modern systems like Linux
 * uses the pipe2() system call, which atomically creates a pipe with
 * the configured flags. The only supported flag currently is
 * %FD_CLOEXEC. If for example you want to configure %O_NONBLOCK, that
 * must still be done separately with fcntl().
 *
 * This function does not take %O_CLOEXEC, it takes %FD_CLOEXEC as if
 * for fcntl(); these are different on Linux/glibc.
 *
 * Returns: %TRUE on success, %FALSE if not (and errno will be set).
 *
 * Since: 2.30
 */
gboolean
g_unix_open_pipe (int     *fds,
                  int      flags,
                  GError **error)
{
  int ecode;

  /* We only support FD_CLOEXEC */
  g_return_val_if_fail ((flags & (FD_CLOEXEC)) == flags, FALSE);

#ifdef HAVE_PIPE2
  {
    int pipe2_flags = 0;
    if (flags & FD_CLOEXEC)
      pipe2_flags |= O_CLOEXEC;
    /* Atomic */
    ecode = pipe2 (fds, pipe2_flags);
    if (ecode == -1 && errno != ENOSYS)
      return g_unix_set_error_from_errno (error, errno);
    else if (ecode == 0)
      return TRUE;
    /* Fall through on -ENOSYS, we must be running on an old kernel */
  }
#endif
  ecode = pipe (fds);
  if (ecode == -1)
    return g_unix_set_error_from_errno (error, errno);

  if (flags == 0)
    return TRUE;

  ecode = fcntl (fds[0], F_SETFD, flags);
  if (ecode == -1)
    {
      int saved_errno = errno;
      close (fds[0]);
      close (fds[1]);
      return g_unix_set_error_from_errno (error, saved_errno);
    }
  ecode = fcntl (fds[1], F_SETFD, flags);
  if (ecode == -1)
    {
      int saved_errno = errno;
      close (fds[0]);
      close (fds[1]);
      return g_unix_set_error_from_errno (error, saved_errno);
    }
  return TRUE;
}
开发者ID:endlessm,项目名称:glib,代码行数:68,代码来源:glib-unix.c

示例2: coprocess_common

pid_t coprocess_common(const char *path, const char *command, 
		char *const argv[], char *const envp[], int pipefd[2]) {
	if (command) {
		int pfd_in[2];
		int pfd_out[2];
		pid_t pid;
		if (pipe2(pfd_in, O_CLOEXEC) == -1 || pipe2(pfd_out, O_CLOEXEC) == -1)
			return -1;
		switch (pid=fork()) {
			case -1:
				return -1;
			case 0:
				if (__builtin_expect(execs_fork_security && execs_fork_security(execs_fork_security_arg) != 0, 0))
					_exit(127);
				if (dup2(pfd_in[0],0) == -1 || dup2(pfd_out[1],1) == -1)
					_exit(127);
				close(pfd_in[0]);
				close(pfd_in[1]);
				close(pfd_out[0]);
				close(pfd_out[1]);
				if (argv) {
					if (path)
						execve(path, argv, envp);
					else
						execvpe(command, argv, envp);
				} else 
					execse(path, command, envp);
				_exit(127);
			default:
				pipefd[0]=pfd_out[0];
				pipefd[1]=pfd_in[1];
				close(pfd_in[0]);
				close(pfd_out[1]);
				return pid;
		}
	} else 
		return 1;
}
开发者ID:rd235,项目名称:s2argv-execs,代码行数:38,代码来源:noshell.c

示例3: SystemNative_Pipe

int32_t SystemNative_Pipe(int32_t pipeFds[2], int32_t flags)
{
    switch (flags)
    {
        case 0:
            break;
        case PAL_O_CLOEXEC:
#if HAVE_O_CLOEXEC
            flags = O_CLOEXEC;
#endif
            break;
        default:
            assert_msg(false, "Unknown pipe flag", (int)flags);
            errno = EINVAL;
            return -1;
    }

    int32_t result;
#if HAVE_PIPE2
    // If pipe2 is available, use it.  This will handle O_CLOEXEC if it was set.
    while ((result = pipe2(pipeFds, flags)) < 0 && errno == EINTR);
#else
    // Otherwise, use pipe.
    while ((result = pipe(pipeFds)) < 0 && errno == EINTR);

    // Then, if O_CLOEXEC was specified, use fcntl to configure the file descriptors appropriately.
#if HAVE_O_CLOEXEC
    if ((flags & O_CLOEXEC) != 0 && result == 0)
#else
    if ((flags & PAL_O_CLOEXEC) != 0 && result == 0)
#endif
    {
        while ((result = fcntl(pipeFds[0], F_SETFD, FD_CLOEXEC)) < 0 && errno == EINTR);
        if (result == 0)
        {
            while ((result = fcntl(pipeFds[1], F_SETFD, FD_CLOEXEC)) < 0 && errno == EINTR);
        }

        if (result != 0)
        {
            int tmpErrno = errno;
            close(pipeFds[0]);
            close(pipeFds[1]);
            errno = tmpErrno;
        }
    }
#endif
    return result;
}
开发者ID:KrzysztofCwalina,项目名称:corefx,代码行数:49,代码来源:pal_io.c

示例4: forkpty

int forkpty(int *pm, char *name, const struct termios *tio, const struct winsize *ws)
{
	int m, s, ec=0, p[2], cs;
	pid_t pid=-1;
	sigset_t set, oldset;

	if (openpty(&m, &s, name, tio, ws) < 0) return -1;

	sigfillset(&set);
	pthread_sigmask(SIG_BLOCK, &set, &oldset);
	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);

	if (pipe2(p, O_CLOEXEC)) {
		close(s);
		goto out;
	}

	pid = fork();
	if (!pid) {
		close(m);
		close(p[0]);
		if (login_tty(s)) {
			write(p[1], &errno, sizeof errno);
			_exit(127);
		}
		close(p[1]);
		pthread_setcancelstate(cs, 0);
		pthread_sigmask(SIG_SETMASK, &oldset, 0);
		return 0;
	}
	close(s);
	close(p[1]);
	if (read(p[0], &ec, sizeof ec) > 0) {
		int status;
		waitpid(pid, &status, 0);
		pid = -1;
		errno = ec;
	}
	close(p[0]);

out:
	if (pid > 0) *pm = m;
	else close(m);

	pthread_setcancelstate(cs, 0);
	pthread_sigmask(SIG_SETMASK, &oldset, 0);

	return pid;
}
开发者ID:AndyHuang7601,项目名称:EpicGames-UnrealEngine,代码行数:49,代码来源:forkpty.c

示例5: _ribified_pipe2

int _ribified_pipe2(int pipefd[2], int flags) {

    if (0 > pipe2(pipefd, flags | O_NONBLOCK))
        return -1;

    if (0 == ribs_epoll_add(pipefd[0], EPOLLIN | EPOLLET, event_loop_ctx) &&
        0 == ribs_epoll_add(pipefd[1], EPOLLOUT | EPOLLET, event_loop_ctx))
        return 0;

    int my_error = errno;
    close(pipefd[0]);
    close(pipefd[1]);
    errno = my_error;
    return -1;
}
开发者ID:Pferd,项目名称:ribs2,代码行数:15,代码来源:ribify.c

示例6: MakePipe

void MakePipe(int out_pipefds[2]) {
#if THRILL_HAVE_PIPE2
    if (pipe2(out_pipefds, O_CLOEXEC) != 0)
        throw ErrnoException("Error creating pipe");
#elif defined(_MSC_VER)
    if (_pipe(out_pipefds, 256, O_BINARY) != 0)
        throw ErrnoException("Error creating pipe");
#else
    if (pipe(out_pipefds) != 0)
        throw ErrnoException("Error creating pipe");

    PortSetCloseOnExec(out_pipefds[0]);
    PortSetCloseOnExec(out_pipefds[1]);
#endif
}
开发者ID:bingmann,项目名称:thrill,代码行数:15,代码来源:porting.cpp

示例7: manage_server_event

static void
manage_server_event (struct item_s *it, uint32_t evt)
{
    struct sockaddr_storage ss;
    int cli, rc, opt;

    ASSERT (evt & EPOLLIN);
    (void) evt;
    socklen_t sslen = sizeof (ss);

    cli = accept4 (it->fd, SA (&ss), &sslen, O_NONBLOCK | O_CLOEXEC);
    if (cli >= 0) {

        opt = PIPE_SIZE / 2;
        setsockopt (cli, SOL_SOCKET, SO_RCVBUF, &opt, sizeof (opt));
        opt = PIPE_SIZE;
        setsockopt (cli, SOL_SOCKET, SO_SNDBUF, &opt, sizeof (opt));

        sock_set_chatty (cli, 1);

        struct item_s *c = malloc (sizeof (struct item_s));

        c->loaded = 0;
        c->pfd[0] = c->pfd[1] = -1;
        if (0 > (rc = pipe2 (c->pfd, O_NONBLOCK | O_CLOEXEC))) {
            (void) close (cli);
            free (c);
            return;
        }
        c->fd = cli;
        c->events = EPOLLIN;
        c->type = CLIENT;
        c->shut = 0;
        fcntl (c->pfd[1], F_SETPIPE_SZ, PIPE_SIZE);

        struct epoll_event evt;

retry_add:
        evt.data.ptr = c;
        evt.events = EPOLLIN;
        rc = epoll_ctl (fd_epoll, EPOLL_CTL_ADD, cli, &evt);
        if (rc < 0) {
            if (rc == EINTR)
                goto retry_add;
            abort ();
        }
    }
}
开发者ID:jfsmig,项目名称:lbtk,代码行数:48,代码来源:echo-tcp-splice.c

示例8: init_pipe

int init_pipe(/*out*/pipe_t* pipe)
{
   int err;

   static_assert(1 + &pipe->read == &pipe->write, "Arrayzugriff möglich");

   if (/*Fehler?*/ pipe2(&pipe->read, O_NONBLOCK|O_CLOEXEC)) {
      err = errno;
      goto ONERR;
   }

   return 0;
ONERR:
   TRACEEXIT_ERRLOG(err);
   return err;
}
开发者ID:je-so,项目名称:js-projekt,代码行数:16,代码来源:pipe.c

示例9: vlc_pipe

/**
 * Creates a pipe (see "man pipe" for further reference).
 */
int vlc_pipe (int fds[2])
{
#ifdef HAVE_PIPE2
    if (pipe2 (fds, O_CLOEXEC) == 0)
        return 0;
    if (errno != ENOSYS)
        return -1;
#endif

    if (pipe (fds))
        return -1;

    fcntl (fds[0], F_SETFD, FD_CLOEXEC);
    fcntl (fds[1], F_SETFD, FD_CLOEXEC);
    return 0;
}
开发者ID:Flameeyes,项目名称:vlc,代码行数:19,代码来源:filesystem.c

示例10: main

int main(void) {
  int pipe_fds[2];
  int i;
  char* buf = mmap(NULL, SIZE, PROT_READ | PROT_WRITE,
                   MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);

  test_assert(buf != MAP_FAILED);
  test_assert(0 == pipe2(pipe_fds, O_NONBLOCK));
  for (i = 0; i < 10000; ++i) {
    test_assert(-1 == read(pipe_fds[0], buf, SIZE));
    test_assert(errno == EAGAIN);
  }

  atomic_puts("EXIT-SUCCESS");
  return 0;
}
开发者ID:CarterTsai,项目名称:rr,代码行数:16,代码来源:read_nothing.c

示例11: main

int
main(void)
{
	(void) close(0);
	(void) close(1);
	int fds[2];
	if (pipe(fds))
		perror_msg_and_fail("pipe");

	(void) close(0);
	(void) close(1);
	if (pipe2(fds, O_NONBLOCK))
		perror_msg_and_skip("pipe2");

	return 0;
}
开发者ID:Fox-Heracles,项目名称:platform_external_strace,代码行数:16,代码来源:pipe.c

示例12: netcv_open_device

int netcv_open_device(adapter *ad)
{
	//fprintf(stderr, "REEL: netcv_open_device (id=%d)\n", ad->id);

	SN.want_commit = 0;
	SN.ncv_rec = NULL;

	/* create DVR pipe for TS data transfer from libmcli to minisatip */
	int pipe_fd[2];
	if (pipe2 (pipe_fd, O_NONBLOCK)) LOGL (0, "netceiver: creating pipe failed");
	ad->dvr = pipe_fd[0];	// read end of pipe
	SN.pwfd = pipe_fd[1];	// write end of pipe
	LOGL(0, "netceiver: creating DVR pipe for adapter %d  -> dvr: %d", ad->id, ad->dvr);

	return 0;
}
开发者ID:lars18th,项目名称:minisatip,代码行数:16,代码来源:netceiver.c

示例13: popen_impl

// not thread safe - needs a lock when called from the main
// process.
int popen_impl(const char* cmd, const char* mode, pid_t* out_pid) {
  int p[2];
  auto const read = *mode == 'r';
  if (!read && *mode != 'w') return -1;

  if (pipe2(p, O_CLOEXEC) < 0) {
    return -1;
  }

  auto pid = fork();
  if (pid < 0) {
    close(p[0]);
    close(p[1]);
    return -1;
  }
  int child_pipe = read ? 1 : 0;
  if (pid == 0) {
    // child
    mprotect_1g_pages(PROT_READ);
    // If anything goes wrong, let the OOM killer kill this child process.
    Process::OOMScoreAdj(1000);
    // replace stdin or stdout with the appropriate end
    // of the pipe
    if (p[child_pipe] == child_pipe) {
      // pretty unlikely, but if it was we must clear CLOEXEC,
      // and the only way to do that is to dup it to a new fd,
      // and then dup2 it back
      p[child_pipe] = fcntl(child_pipe, F_DUPFD_CLOEXEC, 3);
    }
    dup2(p[child_pipe], child_pipe);
    // no need to close p[child_pipe] because of O_CLOEXEC

    signal(SIGINT, SIG_DFL);
    sigset_t eset;
    sigemptyset(&eset);
    sigprocmask(SIG_SETMASK, &eset, nullptr);
    execl("/bin/sh", "sh", "-c", cmd, nullptr);
    Logger::Warning("Failed to exec: `%s'", cmd);
    _Exit(1);
  }
  // parent

  // close the pipe we're not using
  close(p[child_pipe]);
  *out_pid = pid;
  return p[1-child_pipe];
}
开发者ID:swtaarrs,项目名称:hhvm,代码行数:49,代码来源:light-process.cpp

示例14: install_generic_signal_handle

void install_generic_signal_handle() {
    int pipes[2];
    struct sigaction action;
    sigset_t empty_mask;
    
    sigemptyset(&empty_mask);
    
    /* Fill action with handle_signal function */
    action.sa_sigaction = &handle_signal;
    action.sa_flags = SA_SIGINFO|SA_RESTART;
    action.sa_mask = empty_mask;

    singularity_message(DEBUG, "Assigning generic sigaction()s\n");
    if ( -1 == sigaction(SIGINT, &action, NULL) ) {
        singularity_message(ERROR, "Failed to install SIGINT signal handler: %s\n", strerror(errno));
        ABORT(255);
    }
    if ( -1 == sigaction(SIGQUIT, &action, NULL) ) {
        singularity_message(ERROR, "Failed to install SIGQUIT signal handler: %s\n", strerror(errno));
        ABORT(255);
    }
    if ( -1 == sigaction(SIGTERM, &action, NULL) ) {
        singularity_message(ERROR, "Failed to install SIGTERM signal handler: %s\n", strerror(errno));
        ABORT(255);
    }
    if ( -1 == sigaction(SIGHUP, &action, NULL) ) {
        singularity_message(ERROR, "Failed to install SIGHUP signal handler: %s\n", strerror(errno));
        ABORT(255);
    }
    if ( -1 == sigaction(SIGUSR1, &action, NULL) ) {
        singularity_message(ERROR, "Failed to install SIGUSR1 signal handler: %s\n", strerror(errno));
        ABORT(255);
    }
    if ( -1 == sigaction(SIGUSR2, &action, NULL) ) {
        singularity_message(ERROR, "Failed to install SIGUSR2 signal handler: %s\n", strerror(errno));
        ABORT(255);
    }

    /* Open pipes for handle_signal() to write to */
    singularity_message(DEBUG, "Creating generic signal pipes\n");
    if ( -1 == pipe2(pipes, O_CLOEXEC) ) {
        singularity_message(ERROR, "Failed to create communication pipes: %s\n", strerror(errno));
        ABORT(255);
    }
    generic_signal_rpipe = pipes[0];
    generic_signal_wpipe = pipes[1];
}
开发者ID:yqin,项目名称:singularity,代码行数:47,代码来源:fork.c

示例15: daemonize

int daemonize(void) {
    if (0 > pipe2(child_is_up_pipe, O_CLOEXEC))
        return LOGGER_ERROR("failed to create pipe"), -1;

    pid_t pid = fork();
    if (pid < 0) {
        LOGGER_PERROR("daemonize, fork");
        exit(EXIT_FAILURE);
    }

    if (pid > 0) {
        close(child_is_up_pipe[1]); /* close the write side */
        /* wait for child to come up */
        uint8_t t;
        int res;
        LOGGER_INFO("waiting for the child process [%d] to start...", pid);
        if (0 >= (res = read(child_is_up_pipe[0], &t, sizeof(t)))) {
            if (0 > res)
                LOGGER_PERROR("pipe");
            LOGGER_ERROR("child process failed to start");
            exit(EXIT_FAILURE);
        }
        LOGGER_INFO("child process started successfully");
        exit(EXIT_SUCCESS);
    }

    close(child_is_up_pipe[0]); /* close the read side */

    umask(0);

    if (0 > setsid()) {
        LOGGER_PERROR("daemonize, setsid");
        exit(EXIT_FAILURE);
    }

    int fdnull = open("/dev/null", O_RDWR);
    dup2(fdnull, STDIN_FILENO);
    dup2(fdnull, STDOUT_FILENO);
    dup2(fdnull, STDERR_FILENO);
    close(fdnull);

    pid = getpid();

    LOGGER_INFO("child process started (pid=%d)", pid);

    return 0;
}
开发者ID:niosocket,项目名称:ribs2,代码行数:47,代码来源:daemonize.c


注:本文中的pipe2函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。