简体   繁体   English

父母与1个孩子之间的IPC。

[英]IPC between parent and 1 child.

I'm practicing process communication between parent process and 1 child. 我正在练习父进程和1个孩子之间的进程通信。 What I want to do is that every message that the child sends the parent reads it(some sort of blocking send, parent must read the message first before child continues to send another message). 我想做的是孩子发送给父级的每条消息都读取它(某种阻止发送,父级必须先阅读该消息,然后孩子继续发送另一条消息)。 I can't use pipes. 我不能使用管道。 I've read on Silberschatz book about blocking send, but I haven't found a good example of it(maybe mailbox too). 我已经读过Silberschatz关于阻塞发送的书,但是我没有找到一个很好的例子(也许还有邮箱)。 Any help would be nice! 你能帮忙的话,我会很高兴! This is a piece of code: 这是一段代码:

int main(int argc, char** argv) {
printf("This process: ");
printf("%d\n",getpid());
printf("Parent: ");
printf("%d\n",getppid());
pid_t f;
int input;
f = fork();
if (f == 0) {
    for(int i=0;i<5;i++){
        printf("Input a number: ");
        scanf("%d",&input);
        send(getppid(),input);
    }
    printf("\n");
    exit(0);
} else { 
recv(f,input);
printf("%d",input);
}
wait();
exit(0);

} }

This is a terrible hack but works :/ You should use semaphores or some kind of signaling to the shared memory. 这是一个可怕的技巧,但是可以解决:/您应该使用信号量或某种方式向共享内存发送信号。 Create the file for ftok (createthisfile.txt). 为ftok创建文件(createthisfile.txt)。 This is doing intensive polling to the shared memory block and uses a "code" (int = 99) to indicate the parent that the child for loop has finished. 这将对共享内存块进行密集轮询,并使用“代码”(int = 99)指示父对象for循环的孩子已经完成。 Please note that 99 cannot be used as a number or the parent will exit prematurely. 请注意,不能将99用作数字,否则父级会过早退出。 As a last note, you are studying this, then you should solve your problems by yourself, get tips and hints but do not let others do the work for you :/ otherwise you will major in stackoverflow, not on computer science :) I did it as a quick exercise but regret to post it :/ 最后一点,您正在研究,然后应该自己解决问题,获取提示和提示,但不要让其他人为您完成工作:/否则您将专注于stackoverflow,而不是计算机科学:)它是一项快速练习,但遗憾地将其发布:/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>

int main(int argc, char** argv) {
   key_t k;
   pid_t f;
   int shmid;
   int input;

   printf("This process: ");
   printf("%d\n",getpid());
   printf("Parent: ");
   printf("%d\n",getppid());

   k = ftok ("createthisfile.txt", 'R');
   shmid = shmget(k, sizeof(int), IPC_CREAT|SHM_R|SHM_W);
   char *addr = shmat(shmid,0,0);

   f = fork();

   if (f == -1) {
      exit(1);
   }

   if (f == 0) {
      for(int i=0;i<5;i++){
         printf("Input a number: ");
         scanf("%d",&input);
         memcpy(addr, &input, sizeof(int));
      }
      input = 99;
      memcpy(addr, &input, sizeof(int));
      printf("\n");
      exit(0);
   } else { 
      while (1) {
         memcpy(&input, addr, sizeof(int));
         if (input != 0) { 
            if (input == 99) {
               exit (0);
            }
            printf("[Parent reads: %d]\n",input);
            input = 0;
            memcpy(addr, &input, sizeof(int));
         }
      }
   }

   exit(0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM