0

I get this segmentation fault.

It will run the first method, and then the printf line of the 2nd method and then goes directly to a segmentation fault. I don't understand why... I deallocated the previous allocation. The moment my second method runs, it will output the prinf statement: client then segmentation fault

void 
ThreadTest()
{

   DEBUG('t', "Entering SimpleTest");
   printf("THREAD TEST\n");

      Thread *s = new Thread("thread");
      s->Fork(client2,1);

      Thread *g = new Thread("thread");
      g->Fork(client, 1);


}

MY first method running

void
client2(int which)
{
  DEBUG('t', "Entering SimpleTest");


  printf("client2\n");

  int urlSize;
  const int sz = 50;
  char url[sz];

  FILE *fp = fopen("url.txt", "r");
  if (!fp)
    printf("  Cannot open file url.txt!\n");


  else {
    int pos = 0;

    char c = getc(fp);

    while (c != EOF || pos == sz - 1)  
      {

    if (c == '\n') 
      {
        url[pos] = '\0';
        //serve(url);
        pos = 0;
        //url size
        urlSize = sizeof(url)/sizeof(url[0]);   

//---------------------------------------------------------------------------    

        urlList = (List*)malloc(urlSize*sizeof(List));

//---------------------------------------------------------------------------


        clientTXT.P(); //wait
        mutex2.P();//entering critical region

        urlList -> Append(url);
        cout<<"urls: "<< url<<endl;

        mutex2.V();//leaving critical region
        clientURL.V();// increment slots or client url request objects

//--------------------------------------------------------------------------   

        free(urlList);

//---------------------------------------------------------------------------    


      }
    else 
      {
        url[pos++] = c;
      }
    c = getc(fp);
      }
    fclose(fp);
    printf("No more URLs\n");
  }



}

second method running

void
client(int request)
{
  DEBUG('t', "Entering SimpleTest");
  printf("client\n");

  int urlSize;


  while(TRUE){

    char* nextUrl;
    printf("segment");
    nextUrl = (char*)urlList->Remove();

    urlSize = sizeof(nextUrl)/sizeof(nextUrl[0]);     


    Request *reqq = new Request(nextUrl, urlSize, request, 1);
//---------------------------------------------------------------------------    
    reqList = (List*)malloc(urlSize*sizeof(List));

    //--------------------------------------------------------
    clientURL.P(); //wait
    mutex.P();    //entering critical region

    reqList->Append(reqq);
    cout<<"urldest: "<<(reqq->urlDest)<<endl;

    mutex.V();     //exit critical region
    serverURL.V();  //signal
    //-------------------------------------------------------- 

    free(reqList);
//---------------------------------------------------------------------------    

  }
}
monkey doodle
  • 690
  • 5
  • 12
  • 22
  • In your client thread, you're assuming that urlList has been allocated (or malloced) and initialized (hey, where do you do that?). Looks like you are doing that construction in the client2 thread. Just because you start the client2 thread first does not guarantee that the malloc will occur before it's first use in the client thread. Consider doing the construction before starting either thread. – user3813353 Oct 17 '14 at 02:11

1 Answers1

1

The problem is likely occurring because you are not synchronizing access to urlList. In client2 you are allocating urlList and then accessing it in client, however there is no guarantee of ordering of execution of the two threads. Consider using synchronization primitives to control access to urlList to ensure it is allocated before it is accessed.

borice
  • 1,009
  • 1
  • 8
  • 15