CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2011
    Posts
    2

    Semaphore trouble

    I got my program to compile but i keep getting as segmentation fault when i try to run it can someone please point me in the right direction.

    Code:
    #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() &#37; 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;
          
        }
    }
    You can delete this post i solved my problem.
    Last edited by nakeo; October 12th, 2011 at 07:32 AM. Reason: fixed my problem

  2. #2
    Join Date
    Oct 2011
    Posts
    59

    Re: Semaphore trouble

    Well, I would try and find out where the segfault is coming from.

    Try putting some output lines to find out where the problem is lying. If you use cout, ensure that you use endl or a flush, otherwise it may not output the proper location as the data may not get out to the screen as it may still be buffered. endl calls flush which will ensure that you see the output.

    Once you determine where the error is you will have a better idea as to how to fix it. If not, just post your code and output showing where it died and I'm sure someone will try and help ya.

    Cheers,


    A
    Last edited by adrian_h; October 18th, 2011 at 11:02 AM.

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Semaphore trouble

    Did you forget to pass the params to the program?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Aug 2009
    Posts
    440

    Re: Semaphore trouble

    According to the edit comment, looks like he solved his problem.

  5. #5
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Semaphore trouble

    You need to check and validate both argc and argv, otherwise you will get this on the next program you write too.

    HTH,
    ahoodin
    To keep the plot moving, that's why.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured