So i got my code to compile with no errors but when i try to run it i get a segmentation fault and i am honestly clueless as to why. Any help would be greatly appreciated.

#include  <stdio.h>
#include  <iostream>
#include  <stdlib.h>
#include  <sys/types.h>
#include  <fcntl.h>
#include  <unistd.h>
#include <errno.h>
#include <time.h>
#include <string>
#include <ctime>
#include "/home/classes/cs3270ZIM/Lib/semaphore.h"

 using namespace std;

int main(int argc, char **argv)
{
 int pid;
 int randomnums , whogoesfirst, oldnumber, newnumber;
 time_t Btime1, Btime2, Etime1, Etime2; // time pointers
 Semaphore semaphore(123, 2);
 semaphore.Init(0,1);
 semaphore.Init(1,99);
 srand(time(NULL));// random number seed
 randomnums = atoi( argv[1] );
 whogoesfirst = atoi( argv[2] );
 pid = fork();// process forking in two
 if (pid == 0)// Child  process
    {
     time( &Btime1);// process start time 
     if(whogoesfirst==0) // the parent goes first
     {
       for(int index = 0; index < randomnums; index++) // random number loop
        {
        semaphore.Wait(0);
        semaphore.Wait(0);
        oldnumber = semaphore.ReadValue(1);
        newnumber=rand() % 100;
        cout << "child:" << " OldNumber: " << oldnumber << "NewNumber: " << newnumber<< endl;
        semaphore.Signal(0);
        semaphore.Init(1,newnumber);
        }
       

      }
      else // child first
      {
       for(int index = 0; index < randomnums; index++) // random number loop
        {
          oldnumber=semaphore.ReadValue(1);
          newnumber=rand() % 100;
          cout << "child:" << " OldNumber: " << oldnumber << "NewNumber: " << newnumber << endl;
          semaphore.Signal(0);
          sleep(5);
          semaphore.Wait(0);
        }
      }
      time( &Etime1);// process end time
      cout << "\n I am the child; my ID = " << getpid() << "Begin Time: " << ctime (&Btime1)<< "End Time: " << ctime (&Etime1) << endl;
    }

 else
    { // Parent process
     time( &Btime2); // process start time
     srand(time(NULL)); //random seeding based on current time
     if(whogoesfirst==1) //child first
     {
      for(int count = 0; count < randomnums; count++)// random number loop
      {
       oldnumber=semaphore.ReadValue(1);
       newnumber=rand() % 100;
       cout << "parent:" << " OldNumber: " << oldnumber << "NewNumber: " << newnumber << endl;
       semaphore.Wait(0);
      }
     }
     else //parent first
     {
      for(int index = 0; index < randomnums; index++) // random number loop
        {
          oldnumber=semaphore.ReadValue(1);
          newnumber=rand() % 100;
          cout << "parent:" << " OldNumber: " << oldnumber << "NewNumber: " << newnumber << endl;
          semaphore.Signal(0);
          sleep(5);
          semaphore.Wait(0);
        }
     }
     time( &Etime2);// process end time
     cout << "\n I am the parent; my ID = " << getpid() << "Begin Time: " << ctime (&Btime2)<< "End Time: " << ctime (&Etime2) << endl;
      
    }
}

here is

Edited 5 Years Ago by Nakeo: n/a

Attachments
//////////////////////////////////////////////////////////////////////////

// File:	semaphore.h

//////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>

//define KEY	888

union semun {
    int	val;
    struct	semid_ds	*buf;
    ushort	*array;
};

class Semaphore {
	
  private:
    const unsigned int	SemCount;
    int 		SemID;

  public:

    // Create a semaphore group.  Return the semaphore id for the group. 
    Semaphore (key_t key, int howManySemaphoresToCreat);

    // Set the initial value of a semaphore.
    void Init (int SemaphoreNumber, int Value);


    // Return the current value of the semaphore, semaphoreNumber
    int ReadValue (int SemaphoreNumber);


    // Decrease the value of a semaphore, semaphoreNumber, by 1
    void Wait (int SemaphoreNumber);


    // Increase the value of a semaphore, semaphoreNumber, by 1.
    void Signal (int SemaphoreNumber);

    // Remove the semaphore group.
    void Destroy( );


};

Compilation does not mean a correct program.
Have you try to debug your program?
Just by a quick look on your program i am unable to understand the use of

line 33. semaphore.Wait(0);
       Line 34. semaphore.Wait(0);

Compilation does not mean a correct program.
Have you try to debug your program?
Just by a quick look on your program i am unable to understand the use of

line 33. semaphore.Wait(0);
       Line 34. semaphore.Wait(0);

Those two lines where human error, thanks for pointing it out. I also have solved the segmentation fault problem and my program is working perfectly.

This article has been dead for over six months. Start a new discussion instead.