⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test10.c

📁 华清远见应用培训班例程源码
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <string.h>///////////////////////////////////////////////////////////////////////////////// definitions///////////////////////////////////////////////////////////////////////////////#define BUFFER_SIZE	1024///////////////////////////////////////////////////////////////////////////////// global variables///////////////////////////////////////////////////////////////////////////////int global_pipe_fds[2];char *global_send_buffer = "some data write to parent.";char global_read_buffer[BUFFER_SIZE];///////////////////////////////////////////////////////////////////////////////// function prototypes///////////////////////////////////////////////////////////////////////////////static void cleanup(void);void parent_process_main(void);void child_process_main(void);///////////////////////////////////////////////////////////////////////////////// functions///////////////////////////////////////////////////////////////////////////////int main(int argc, char **argv){  pid_t pid;  // int pipe(int filedes[2]);  if (pipe(global_pipe_fds) < 0)  {    // FIXME: deal with this error.    fprintf(stderr, "ERROR: call pipe() failed: %s\n", strerror(errno));    exit(1);  }#ifdef _DEBUG_  fprintf(stdout, "DEBUG: Create pipe successed, fds[0] = %d, fds[1] = %d\n", global_pipe_fds[0], global_pipe_fds[1]);#endif  if ((pid = fork()) < 0)  {    fprintf(stderr, "ERROR: call fork() failed: %s\n", strerror(errno));    cleanup();    // FIXME: deal with this error.    exit(1);  }  if (pid > 0)  {    // XXX: Parent process    close(global_pipe_fds[1]);    parent_process_main();    close(global_pipe_fds[0]);  }  else  {    // XXX: Child process    close(global_pipe_fds[0]);    sleep(1);    child_process_main();    close(global_pipe_fds[1]);    exit(0);  }  return 0;}void child_process_main(void){  // ssize_t write(int fildes, const void *buf, size_t nbyte);  ssize_t written;  if ((written = write(global_pipe_fds[1], global_send_buffer, strlen(global_send_buffer))) < 0)  {    fprintf(stderr, "[%d]Child write to pipe failed: %s\n", getpid(), strerror(errno));    // FIXME: deal with this error.    exit(1);  }#ifdef _DEBUG_  fprintf(stdout, "[%d] Child write %d bytes to pipe.\n", getpid(), written);#endif  if (written < strlen(global_send_buffer))  {    // TODO: we should write rest data to pipe, may be we need a loop  }}void parent_process_main(void){  ssize_t n;  memset(global_read_buffer, 0, BUFFER_SIZE);  // ssize_t read(int fildes, void *buf, size_t nbyte);  if ((n = read(global_pipe_fds[0], global_read_buffer, BUFFER_SIZE)) < 0)  {    // TODO: deal with read return value and error  }  fprintf(stdout, "[%d]Parent read %d bytes from pipe: %s\n", getpid(), n, global_read_buffer);  // TODO: we should read rest data from pipe, may be we need a loop}static void cleanup(void){  close(global_pipe_fds[0]);  close(global_pipe_fds[1]);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -