cmd_test.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "cmd_test.h"
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #define buf_SIZE 256 // 定义消息的最大长度
  10. int shell_pipe_test(int argc, char **argv)
  11. {
  12. int fd[2], i, n;
  13. pid_t pid;
  14. int ret = pipe(fd); // 创建一个管道
  15. if (ret < 0)
  16. {
  17. printf("pipe error");
  18. exit(1);
  19. }
  20. pid = fork(); // 创建一个子进程
  21. if (pid < 0)
  22. {
  23. printf("fork error");
  24. exit(1);
  25. }
  26. if (pid == 0)
  27. { // 子进程
  28. close(fd[1]); // 关闭管道的写端
  29. for (i = 0; i < 3; i++)
  30. { // 循环三次
  31. char buf[buf_SIZE] = {0};
  32. n = read(fd[0], buf, buf_SIZE); // 从管道的读端读取一条消息
  33. if (n > 0)
  34. {
  35. printf("Child process received message: %s\n", buf); // 打印收到的消息
  36. if (strcmp(buf, "quit") == 0)
  37. { // 如果收到的消息是"quit"
  38. printf("Child process exits.\n"); // 打印退出信息
  39. break; // 跳出循环
  40. }
  41. else
  42. { // 如果收到的消息不是"quit"
  43. printf("Child process is doing something...\n"); // 模拟子进程做一些操作
  44. usleep(100);
  45. }
  46. }
  47. }
  48. close(fd[0]); // 关闭管道的读端
  49. exit(0);
  50. }
  51. else
  52. { // 父进程
  53. close(fd[0]); // 关闭管道的读端
  54. for (i = 0; i < 3; i++)
  55. { // 循环三次
  56. char *msg = "hello world";
  57. if (i == 1)
  58. {
  59. msg = "how are you";
  60. usleep(1000);
  61. }
  62. if (i == 2)
  63. {
  64. msg = "quit";
  65. usleep(1000);
  66. }
  67. n = strlen(msg);
  68. printf("Parent process send:%s\n", msg);
  69. write(fd[1], msg, n); // 向管道的写端写入一条消息
  70. if (strcmp(msg, "quit") == 0)
  71. { // 如果发送的消息是"quit"
  72. printf("Parent process exits.\n"); // 打印退出信息
  73. break; // 跳出循环
  74. }
  75. }
  76. close(fd[1]); // 关闭管道的写端
  77. wait(NULL); // 等待子进程结束
  78. }
  79. return 0;
  80. }
  81. int shell_pipe2_test(int argc, char **argv)
  82. {
  83. int fd[2], i, n;
  84. pid_t pid;
  85. int ret = pipe2(fd, O_NONBLOCK); // 创建一个管道
  86. if (ret < 0)
  87. {
  88. printf("pipe error\n");
  89. exit(1);
  90. }
  91. pid = fork(); // 创建一个子进程
  92. if (pid < 0)
  93. {
  94. printf("fork error\n");
  95. exit(1);
  96. }
  97. if (pid == 0)
  98. { // 子进程
  99. close(fd[1]); // 关闭管道的写端
  100. for (i = 0; i < 10; i++)
  101. {
  102. char buf[buf_SIZE] = {0};
  103. n = read(fd[0], buf, buf_SIZE); // 从管道的读端读取一条消息
  104. if (n > 0)
  105. {
  106. printf("Child process received message: %s\n", buf); // 打印收到的消息
  107. if (strcmp(buf, "quit") == 0)
  108. { // 如果收到的消息是"quit"
  109. printf("Child process exits.\n"); // 打印退出信息
  110. break; // 跳出循环
  111. }
  112. else
  113. { // 如果收到的消息不是"quit"
  114. printf("Child process is doing something...\n"); // 模拟子进程做一些操作
  115. // usleep(1000);
  116. }
  117. }
  118. else
  119. {
  120. printf("read error,buf is empty\n");
  121. }
  122. }
  123. close(fd[0]); // 关闭管道的读端
  124. exit(0);
  125. }
  126. else
  127. { // 父进程
  128. close(fd[0]); // 关闭管道的读端
  129. for (i = 0; i < 100; i++)
  130. {
  131. char *msg = "hello world";
  132. if (i < 99 & i > 0)
  133. {
  134. msg = "how are you";
  135. // usleep(1000);
  136. }
  137. if (i == 99)
  138. {
  139. msg = "quit";
  140. // usleep(1000);
  141. }
  142. n = strlen(msg);
  143. printf("Parent process send:%s\n", msg);
  144. int r = write(fd[1], msg, n); // 向管道的写端写入一条消息
  145. if (r < 0)
  146. {
  147. printf("write error,buf is full\n");
  148. }
  149. if (strcmp(msg, "quit") == 0)
  150. { // 如果发送的消息是"quit"
  151. printf("Parent process exits.\n"); // 打印退出信息
  152. break; // 跳出循环
  153. }
  154. }
  155. close(fd[1]); // 关闭管道的写端
  156. wait(NULL); // 等待子进程结束
  157. }
  158. return 0;
  159. }