如何在C语言中正常运行这两个进程?

huangapple go评论121阅读模式
英文:

how can i run these two proceses normally in c language?

问题

这段代码运行正常,但消费者进程不工作,我尝试获取producer_pid的值并检查发生了什么,但当我写printf("%d\n", producer_pid)时,如果不加\n,它只给出一个值。我对计算机编程一无所知,我正在自学,如果问题很糟糕,我很抱歉。

我认为生产者进程没有结束,有什么办法可以帮助我吗?

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #define BUFFER_SIZE 2
  8. int buffer[BUFFER_SIZE];
  9. int sayac = 0;
  10. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  11. void produce(int item) {
  12. buffer[sayac] = item;
  13. sayac++;
  14. }
  15. int consume() {
  16. int item = buffer[sayac - 1];
  17. sayac--;
  18. return item;
  19. }
  20. void *producer_thread(void *arg) {
  21. int *pipe_fd = (int *)arg;
  22. for (int i = 1; i <= 10; i++) {
  23. pthread_mutex_lock(&mutex);
  24. while (sayac == BUFFER_SIZE) {
  25. pthread_mutex_unlock(&mutex);
  26. usleep(100000); // 短暂等待以释放循环
  27. pthread_mutex_lock(&mutex);
  28. }
  29. produce(i);
  30. printf("生产者: %d 已生产。\n", i);
  31. write(pipe_fd[1], &i, sizeof(i));
  32. pthread_mutex_unlock(&mutex);
  33. sleep(1);
  34. }
  35. close(pipe_fd[1]);
  36. return NULL;
  37. }
  38. void *consumer_thread(void *arg) {
  39. int *pipe_fd = (int *)arg;
  40. for (int i = 1; i <= 10; i++) {
  41. pthread_mutex_lock(&mutex);
  42. while (sayac == 0) {
  43. pthread_mutex_unlock(&mutex);
  44. usleep(100000); // 短暂等待以释放循环
  45. pthread_mutex_lock(&mutex);
  46. }
  47. int item = consume();
  48. printf("消费者: %d 已消费。\n", item);
  49. int received_data;
  50. read(pipe_fd[0], &received_data, sizeof(received_data));
  51. pthread_mutex_unlock(&mutex);
  52. sleep(1);
  53. }
  54. close(pipe_fd[0]);
  55. return NULL;
  56. }
  57. int main() {
  58. int pipe_fd[2];
  59. if (pipe(pipe_fd) == -1) {
  60. perror("无法创建管道");
  61. return 1;
  62. }
  63. pid_t producer_pid = fork();
  64. printf("%d ---------------", producer_pid);
  65. if (producer_pid == 0) {
  66. // 子生产者进程代码
  67. close(pipe_fd[0]); // 关闭读取端
  68. producer_thread((void *)pipe_fd);
  69. exit(0);
  70. } else if (producer_pid > 0) {
  71. pid_t consumer_pid = fork();
  72. if (consumer_pid == 0) {
  73. // 子消费者进程代码
  74. close(pipe_fd[1]); // 关闭写入端
  75. consumer_thread((void *)pipe_fd);
  76. exit(0);
  77. } else if (consumer_pid > 0) {
  78. // 主进程代码
  79. close(pipe_fd[0]); // 关闭读取端
  80. close(pipe_fd[1]); // 关闭写入端
  81. wait(NULL);
  82. wait(NULL);
  83. printf("主进程完成。\n");
  84. } else {
  85. fprintf(stderr, "无法创建消费者进程。\n");
  86. return 1;
  87. }
  88. } else {
  89. fprintf(stderr, "无法创建生产者进程。\n");
  90. return 1;
  91. }
  92. return 0;
  93. }
英文:

this code works fine but consumer process does not work, i tried to get producer_pid value and check what is going on but when i write the printf("%d\n",producer_pid) it gives two values when do not put \n it gives one value. i am new to all computer programming i am educating myself, if the question is so bad i am sorry

i think producer process is not ending any idea that could help me?

  1. #include &lt;stdio.h&gt;
  2. #include &lt;stdlib.h&gt;
  3. #include &lt;pthread.h&gt;
  4. #include &lt;unistd.h&gt;
  5. #include &lt;sys/types.h&gt;
  6. #include &lt;sys/wait.h&gt;
  7. #define BUFFER_SIZE 2
  8. int buffer[BUFFER_SIZE];
  9. int sayac = 0;
  10. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  11. void produce(int item) {
  12. buffer[sayac] = item;
  13. sayac++;
  14. }
  15. int consume() {
  16. int item = buffer[sayac - 1];
  17. sayac--;
  18. return item;
  19. }
  20. void *producer_thread(void *arg) {
  21. int *pipe_fd = (int *)arg;
  22. for (int i = 1; i &lt;= 10; i++) {
  23. pthread_mutex_lock(&amp;mutex);
  24. while (sayac == BUFFER_SIZE) {
  25. pthread_mutex_unlock(&amp;mutex);
  26. usleep(100000); // Kısa bir s&#252;re bekleyerek d&#246;ng&#252;y&#252; serbest bırak
  27. pthread_mutex_lock(&amp;mutex);
  28. }
  29. produce(i);
  30. printf(&quot;&#220;retici: %d &#252;retildi.\n&quot;, i);
  31. write(pipe_fd[1], &amp;i, sizeof(i));
  32. pthread_mutex_unlock(&amp;mutex);
  33. sleep(1);
  34. }
  35. close(pipe_fd[1]);
  36. return NULL;
  37. }
  38. void *consumer_thread(void *arg) {
  39. int *pipe_fd = (int *)arg;
  40. for (int i = 1; i &lt;= 10; i++) {
  41. pthread_mutex_lock(&amp;mutex);
  42. while (sayac == 0) {
  43. pthread_mutex_unlock(&amp;mutex);
  44. usleep(100000); // Kısa bir s&#252;re bekleyerek d&#246;ng&#252;y&#252; serbest bırak
  45. pthread_mutex_lock(&amp;mutex);
  46. }
  47. int item = consume();
  48. printf(&quot;T&#252;ketici: %d t&#252;ketildi.\n&quot;, item);
  49. int received_data;
  50. read(pipe_fd[0], &amp;received_data, sizeof(received_data));
  51. pthread_mutex_unlock(&amp;mutex);
  52. sleep(1);
  53. }
  54. close(pipe_fd[0]);
  55. return NULL;
  56. }
  57. int main() {
  58. int pipe_fd[2];
  59. if (pipe(pipe_fd) == -1) {
  60. perror(&quot;Pipe oluşturulamadı&quot;);
  61. return 1;
  62. }
  63. pid_t producer_pid = fork();
  64. printf(&quot;%d ---------------&quot;,producer_pid);
  65. if (producer_pid == 0) {
  66. // Yavru &#252;retici işlem kodu
  67. close(pipe_fd[0]); // Okuma tarafını kapat
  68. producer_thread((void *)pipe_fd);
  69. exit(0);
  70. } else if (producer_pid &gt; 0) {
  71. pid_t consumer_pid = fork();
  72. if (consumer_pid == 0) {
  73. // Yavru t&#252;ketici işlem kodu
  74. close(pipe_fd[1]); // Yazma tarafını kapat
  75. consumer_thread((void *)pipe_fd);
  76. exit(0);
  77. } else if (consumer_pid &gt; 0) {
  78. // Ana işlem kodu
  79. close(pipe_fd[0]); // Okuma tarafını kapat
  80. close(pipe_fd[1]); // Yazma tarafını kapat
  81. wait(NULL);
  82. wait(NULL);
  83. printf(&quot;Ana işlem tamamlandı.\n&quot;);
  84. } else {
  85. fprintf(stderr, &quot;T&#252;ketici işlem oluşturulamadı.\n&quot;);
  86. return 1;
  87. }
  88. } else {
  89. fprintf(stderr, &quot;&#220;retici işlem oluşturulamadı.\n&quot;);
  90. return 1;
  91. }
  92. return 0;
  93. }

答案1

得分: 1

这里的主要问题是你正在打开一个 pthread 互斥锁来同步使用 fork() 创建的进程之间的操作。

pthread_mutex,顾名思义,是设计用于线程之间的同步的。它无法在使用 fork 创建的进程之间进行同步,因为这些进程将拥有不同的内存空间。

对于你的实现,甚至没有必要使用 pthread_mutex。write() 和 read() 是阻塞的,所以你可以通过使用管道来实现两个进程的同步。

另外,当你使用 printf() 时,有时操作系统会等待而不打印所有的日志。使用 fflush() 来查看所有的日志。

英文:

the main problem here is that you're opening a pthread mutex to sync between processes opened using fork().

pthread_mutex, as its name would suggest, is designed to sync between threads.
It will not be able to sync between processes created using fork since the processes will have a different memory space.

For your implementation, there isn't much of a point to even use pthread_mutex. write() and read() are blocking, so you can have both processes sync by using the pipe.

On a different subject, when you use printf(), sometimes the OS will wait and not print all of the log. use fflush() to see all your logs.

huangapple
  • 本文由 发表于 2023年8月9日 17:04:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定