1

Here's a revision of my programming problem:

  1. Fork off two idle processes that run for a random time, each one running between 0 - 20 seconds.
  2. Processes should use signals SIGSTOP and SIGCONT to schedule the processes in a round-robin queue.
  3. When the child processes BOTH terminate for each idle process, the parent should terminate as well.
  4. If one of the child processes terminate, it should keep going, until both of them are terminated using SIGCHLD.

This is about how far I'm at.

#include <iostream>
#include <unistd.h>
#include <sys/wait.h>

using namespace std;
using std::cout;

volatile sig_atomic_t keep_going = 1;

void catch_child (int sig)
{ int status, p;
  p = waitpid(-1,&status,WNOHANG);
  if (p==0) return;
  keep_going=0;
  cout<<"Child process caught: "<< p <<endl;
  return;
}


int  main()
{
  pid_t pid;
  pid_t mypid;
  int rand1;

  rand1 = rand()%20;    

  signal (SIGCHLD, catch_child);

  pid = fork();
  if (pid<0) {
    cout<<"Error\n";
    return 0;
  }
  else if (pid==0) {


    mypid = getpid();
    cout << "Child pid is " << mypid << "\n";
    execlp("./idle","./idle","1",rand1,0);
  } else {
    mypid=getpid();

    cout<<"parent procss is "<<mypid<<" and child is "<<pid<<"\n";
    sleep(5);

    cout<<"pausing child "<<pid<<" for 5 seconds\n";
    kill(pid,SIGSTOP);

    for (int i=0; i<5; i++) sleep(1);

    cout<<"Resuming child "<<pid<<"\n";
    kill(pid,SIGCONT);

    cout<<"Parent process pid is "<<mypid<<" waiting for child to stop\n";
    while (keep_going==1) sleep(1);
    // wait(NULL);


    cout <<"Parent knows that Child now done\n";
  }
  return 0;
}
Grzegorz Adam Kowalski
  • 5,243
  • 3
  • 29
  • 40
Lord Bahamut
  • 153
  • 1
  • 3
  • 19
  • Basically, I'm trying to figure out how to start off with solving this programming challenge. What works best for those who can solve this problem? Flow charts? Psuedocode? Basic to intermediate programming I can understand, however, I find this to be an entire new parameter to test my programming knowledge. – Lord Bahamut Mar 28 '14 at 08:51
  • 4
    C and C++ are two different languages. Since here you are trying to learn some POSIX features and the specification language for POSIX is C, I'd suggest that you stick to C. Besides from that your question is far too broad for SO. – Jens Gustedt Mar 28 '14 at 09:16
  • I'll do my best to refine my question. – Lord Bahamut Mar 28 '14 at 14:44
  • Made an update, hopefully which is more clear and less broad. If I need to revise further please let me know. – Lord Bahamut Mar 28 '14 at 18:04
  • There is still no question in your question. – filmor May 05 '14 at 10:52

0 Answers0