0

I'm practicing process communication between parent process and 1 child. 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). 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);

}

  • What is this supposed to do? What does it do? What does it not do (that you expected it to do)? – KevinDTimm Apr 20 '17 at 15:58
  • I input a number on child->parents reads it->input again on child->parent reads and so on. I just made a simple case of just 5 numbers – BlackFolgore Apr 20 '17 at 15:59
  • 1
    `send` is not a solution to your problem, and you definitely cannot call it on a pid as if it were a socket filedescriptor. – Petr Skocik Apr 20 '17 at 16:03
  • 1
    I've never seen two argument versions of `send()` and `recv()`. – Sean Bright Apr 20 '17 at 16:03
  • My solution resides on pipes then? (I got 2 arg send and receive from the book I mentioned above :P) – BlackFolgore Apr 20 '17 at 16:05
  • It does compile if we add stdio.h and sys/type.h as includes. But as @PSkocik said, this send and recv applied to pids should never work, i don't even get, yet, how this is compiling with just two arguments on send/recv. There are several techniques for IPC, be pipes, shared memory or whatever. Try to be more precise on what you want to achieve, overall. – José Fonte Apr 20 '17 at 16:15
  • #include #include #include #include I use those. I know how to use shared memory but I have no clue on how to make it wait for the parent to read after child continues to write. – BlackFolgore Apr 20 '17 at 16:16
  • @JoséFonte. It compiles because there is no include and this is c, not c++ – Robert Jacobs Apr 20 '17 at 16:17
  • Why can't you use pipes? – Robert Jacobs Apr 20 '17 at 16:18
  • Use a semaphore to have process wait in shared memory. http://stackoverflow.com/a/8359403/2066459 – Robert Jacobs Apr 20 '17 at 16:21
  • We should not be able, since it's a laboratory practice and in class we have not seen pipes yet. So I should be able to find a way using shared memory or mailboxes. – BlackFolgore Apr 20 '17 at 16:21
  • @BlackFolgore So what IPC mechanisms _can_ you use? – Petr Skocik Apr 20 '17 at 16:22
  • @PSkocik : Shared memory, message passing(such as mailbox). Haven't seen semaphores as well. – BlackFolgore Apr 20 '17 at 16:25
  • Also, on your code, the parent just receives once, while the child is sending 5 times, whatever process you implement, you must check that. – José Fonte Apr 20 '17 at 16:27
  • @JoséFonte yeah, I want to make that similar to a blocking send, where child will wait until parent receives the message. – BlackFolgore Apr 20 '17 at 16:46

1 Answers1

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). 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. Please note that 99 cannot be used as a number or the parent will exit prematurely. 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 :/

#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);
}
José Fonte
  • 4,016
  • 2
  • 16
  • 28