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

📄 test2.c

📁 这个是学习嵌入式开发的重要例子
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <errno.h>#define BUFFER_SIZE	1024void child_process_main(int fd);void parent_process_main(int fd);int main(int argc, char **argv){  int fds[2];  // int pipe(int fildes[2]);  // Upon successful completion, 0 shall be returned; otherwise, -1 shall be returned and errno set to  indi-  // cate the error.  if (pipe(fds) < 0)  {    fprintf(stderr, "Create pipe failed: %s\n", strerror(errno));    exit(1);  }  fprintf(stdout, "Created a new pipe, fds[0] = %d, fds[1] = %d\n", fds[0], fds[1]);  pid_t pid;  if ((pid = fork()) < 0)  {    fprintf(stdout, "fork() failed: %s\n", strerror(errno));    exit(1);  }  else if (pid == 0)  {    // XXX: child process close write endpoint, open read endpoint for reading    close(fds[1]);    child_process_main(fds[0]);    exit(0);  }  // XXX: parent process close read endpoint, open write endpoint for writting  close(fds[0]);  parent_process_main(fds[1]);  close(fds[0]);  close(fds[1]);  return 0;}void child_process_main(int fd){  fprintf(stdout, "[%d]Read data from pipe, fd = %d\n", getpid(), fd);  char buf[BUFFER_SIZE];  ssize_t n;  memset(buf, 0, BUFFER_SIZE);  // ssize_t read(int fildes, void *buf, size_t nbyte);  n = read(fd, buf, BUFFER_SIZE);  if (n < 0)  {    fprintf(stdout, "[%d]Read from pipe failed: %s\n", getpid(), strerror(errno));    // FIXME: handle read error  }  else if (n == 0)  {    fprintf(stdout, "[%d]Read from pipe return 0.\n", getpid());  }  else  {    fprintf(stdout, "[%d]Read %d bytes from pipe.\n", getpid(), n);    fprintf(stdout, "[%d]Message: %s\n", getpid(), buf);    if (strlen(buf) < 8)    {      fprintf(stdout, "[%d]Can not get data length.\n", getpid());      // FIXME: read rest data.    }    else    {      int length;      char str[BUFFER_SIZE];      memset(str, 0, BUFFER_SIZE);      memcpy(str, buf, 8);      length = atoi(str);      if ((strlen(buf)) < 8 + length)      {	fprintf(stdout, "[%d]There's no enough data for parsing", getpid());	// FIXME: read rest data      }      else      {	fprintf(stdout, "[%d]We have read all required data.\n", getpid());      }    }  }}void parent_process_main(int fd){  pid_t pid;  pid = getpid();  fprintf(stdout, "[%d]Write data to pipe, fd = %d\n", getpid(), fd);  char buf[BUFFER_SIZE];  char wbuf[BUFFER_SIZE];  memset(buf, 0, BUFFER_SIZE);  // char *fgets(char *restrict s, int n, FILE *restrict stream);  if (fgets(buf, BUFFER_SIZE, stdin) == NULL)  {    fprintf(stdout, "[%d]Read failed from stdin: %s\n", getpid(), strerror(errno));    // FIXME: handle read from stdin failed  }  else  {    // ssize_t write(int fildes, const void *buf, size_t nbyte);    ssize_t n;    int length;    length = strlen(buf);    memset(wbuf, 0, BUFFER_SIZE);    // int snprintf(char *str, size_t size, const char *format, ...);    snprintf(wbuf, BUFFER_SIZE, "%08d%s", length, buf);    fprintf(stdout, "[%d]Composed to-be-sent buffer: %s\n", pid, wbuf);    n = write(fd, wbuf, strlen(wbuf));    fprintf(stdout, "[%d]Write %d bytes to pipe.\n", pid, n);  }}

⌨️ 快捷键说明

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