<p>I need to to print natural nos. 1,2,...n such that the parent process prints all odd numbers and the child process prints all even numbers, and all of this needs to be done using POSIX signals. How would I go about accomplishing this?</p>

<p>The output should be:</p>

<p>Parent : 1<br>
Child  : 2<br>
Parent : 3<br>
...</p>

[{'answer_id': 160070, 'author': 'user10146', 'author_id': 10146, 'author_profile': 'https://Stackoverflow.com/users/10146', 'pm_score': 2, 'selected': False, 'text': '<p>I think this homework assignment was given to you to make you try a lot of solution and conclude by yourself that <strong>signals are not a good synchronisation technique</strong>.</p>\n\n<p>This an invaluable lesson, remember it well and use semaphores from now on! :)</p>\n'}, {'answer_id': 171502, 'author': 'Robert Gamble', 'author_id': 25222, 'author_profile': 'https://Stackoverflow.com/users/25222', 'pm_score': 2, 'selected': False, 'text': '<p>It would probably be more beneficial for you provide what you have so far and explain what doesn\'t work as expected but here is what I came up with:</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;sys/types.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;signal.h&gt;\n#include &lt;stdlib.h&gt;\n\n#define READY_SIGNAL SIGUSR1\n\n/* The ready flag is set when READY_SIGNAL is received.\n * It is needed so that when we wake up from sigsuspend\n * we know whether or not the signal received was READY_SIGNAL. */\nvolatile sig_atomic_t ready;\nvoid make_ready(int i) { ready = 1; }\n\nint\nmain (int argc, char *argv[])\n{\n  pid_t cpid, ppid;  /* pids of the child and parent */\n  /* Signal masks for sigprocmask and sigsuspend */\n  sigset_t block_mask, wait_mask;\n  unsigned long c = 1;   /* The counter */\n  unsigned long n = 100; /* The default max count value */\n  struct sigaction act;\n\n  /* Override the default max count if provided */\n  if (argv[1])\n    n = strtoul(argv[1], NULL, 10);\n\n  /* Prepare signal masks */\n  sigemptyset(&amp;wait_mask);\n  sigemptyset(&amp;block_mask);\n  sigaddset(&amp;block_mask, READY_SIGNAL);\n\n  /* Set the signal mask for the parent to ignore READY_SIGNAL until\n   * we are ready to receive it, the mask will be inherited by the child,\n   * needed to avoid race conditions */\n  sigprocmask(SIG_BLOCK, &amp;block_mask, NULL);\n\n  /* Register the signal handler, will be inherited by the child */\n  act.sa_flags = 0;\n  act.sa_handler = make_ready;\n  sigemptyset(&amp;act.sa_mask);\n  sigaction(READY_SIGNAL, &amp;act, NULL);\n\n  /* Get the parent\'s process id, needed for the child to send signals\n   * to the parent process, could alternatively use getppid in the child */\n  ppid = getpid();\n\n  /* Call fork, storing the child\'s process id needed for the parent to\n   * send signals to the child */\n  cpid = fork();\n\n  if (cpid &lt; 0) {\n    perror("Fork failed");\n    exit(EXIT_FAILURE);\n  }\n\n  if (cpid == 0) {\n    /* Child */\n    c = 2;  /* Child\'s first number will always be 2 */\n    if (c &gt; n) exit(0); /* If c &gt; n we have nothing to do */\n\n    do {\n      /* Suspend until we receive READY_SIGNAL */\n      while (!ready) sigsuspend(&amp;wait_mask);\n\n      /* Print out number, flush for proper output sequencing when output\n         is not a terminal. */\n      printf("Child: %lu\\n", c);\n      fflush(stdout);\n\n      ready = 0; /* Reset ready flag */\n      c += 2; /* Increment counter */\n\n      /* Wake up parent process */\n      kill(ppid, READY_SIGNAL);\n    } while (c &lt;= n);  \n  } else {\n    /* Parent */\n    for (;;) {\n      /* Print out number, flush for proper output sequencing when output\n         is not a terminal. */\n      printf("Parent: %lu\\n", c);\n      fflush(stdout);\n\n      c += 2; /* Increment counter */\n\n      kill(cpid, READY_SIGNAL); /* Wake up child process */\n\n      if (c &gt; n) break; /* Don\'t go back to sleep if we are done */\n\n      ready = 0; /* Reset ready flag */\n\n      /* Suspend until we receive READY_SIGNAL */\n      while (!ready) sigsuspend(&amp;wait_mask);\n    };\n\n    wait4(cpid, NULL, 0); /* Don\'t exist before child finishes */\n  }\n\n  return 0;\n}\n</code></pre>\n\n<p>This passes these basic tests:</p>\n\n<p><code>./print_with_signals 100000|sort -n -k 2 -c &amp;&amp; echo "Success"</code>\n<br/>\n<code>./print_with_signals 100001|sort -n -k 2 -c &amp;&amp; echo "Success"</code></p>\n'}]
['https://Stackoverflow.com/questions/35380', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2727/']