[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1517
  • Last Modified:

segmentation fault(core dump)

I tried to excute the following code but it gives me segmentation fault(core dump). Im trying to implement lamports algorithm.

#include <stdio.h>
#include<stdlib.h>
#include<pthread.h>
#define NO_OF_THREADS 10

int  choosing[NO_OF_THREADS];
int Enter[NO_OF_THREADS];
void * lamport_fn(void *ptr);
int main()
{
      int i;
for(i=0;i<NO_OF_THREADS;i++)
{
      choosing[i]=0;
      Enter[i]=0;
}
pthread_t thread[NO_OF_THREADS];
     

    /* Create threads  */

     for(i=0;i<NO_OF_THREADS;i++) pthread_create( &thread[i], NULL, lamport_fn,(void *)i);
     
// wait till main completes.
for(i=0;i<NO_OF_THREADS;i++)   pthread_join( thread[i],NULL);
     return 0;
}
  int find_max()
  {
      int i=0,max=0;
        while(i<NO_OF_THREADS)
                     {
                           if(choosing[i]>max)
                           {
                                 max=choosing[i];
                           }
                           i++;
                     }
      
        return max;
  }
  void * lamport_fn(void *ptr) {
      int i=*(int *)ptr;
        int j;
                while (1) {
           Enter[i] = 1;
           choosing[i] = 1 + find_max();
           Enter[i] = 0;
           for (j = 0; j <= NO_OF_THREADS; j++) {
               while (Enter[j] != 0) {
                  // wait until thread j receives its number
               }
              while ((choosing[j]!= 0) && ((choosing[j]< choosing[i])||((choosing[j]==choosing[i])&&(i<j))))
                    {
                  // wait until threads with smaller choosings or with the same
                  // choosing, but with higher priority, finish their work
              }
          }
          // critical section...
               /*

              CS computations
               */

          choosing[i] = 0;
          // non-critical section...
      }
  }
0
pradeepkdr
Asked:
pradeepkdr
1 Solution
 
PaulCaswellCommented:
Hi pradeepkdr,

First look suggests:

           for (j = 0; j <= NO_OF_THREADS; j++) {

should be:

           for (j = 0; j < NO_OF_THREADS; j++) {


Paul
0
 
pradeepkdrAuthor Commented:
thanx for the reply but this time i wasnt able to create threads...for the below program the output is given.
I am trying to simulate lamport bakery algorithm as per wikipedia Not much changes.

http://en.wikipedia.org/wiki/Lamport's_bakery_algorithm

code goes like this:

// declaration and initial values of global variables
#include <stdio.h>
#include<stdlib.h>
#include<pthread.h>
#define NO_OF_THREADS 10

int  choosing[NO_OF_THREADS];
int Enter[NO_OF_THREADS];
void * lamport_fn(void *ptr);
int find_max();

int main()
{
      int i,return_code;
for(i=0;i<NO_OF_THREADS;i++)
{
      choosing[i]=0;
      Enter[i]=0;
}
pthread_t thread[NO_OF_THREADS];
     

    /* Create threads  */

     for(i=0;i<NO_OF_THREADS;i++)
       {  
             printf("creating thread %d\n",i);
             return_code=pthread_create( &thread[i], NULL, lamport_fn,(void *)i);
             if(return_code) printf("cannot creat thread..%d\n",return_code);
       }


     
// wait till main completes.
for(i=0;i<NO_OF_THREADS;i++)   pthread_join( thread[i],NULL);
     return 0;
}
  int find_max()
  {
      int i=0,max=0;
        while(i<NO_OF_THREADS)
                     {
                           if(choosing[i]>max)
                           {
                                 max=choosing[i];
                           }
                           i++;
                     }
      
        return max;
  }
  void * lamport_fn(void *ptr) {
      int i=*(int *)ptr;
        int j;
                while(1)
              {
           Enter[i] = 1;
           choosing[i] = 1 + find_max();
           Enter[i] = 0;
           for (j = 0; j < NO_OF_THREADS; j++) {
               while (Enter[j] != 0) {
                  // wait until thread j receives its number
               }
              while ((choosing[j]!= 0) && ((choosing[j]< choosing[i])||((choosing[j]==choosing[i])&&(i<j))))
                    {
                  // wait until threads with smaller choosings or with the same
                  // choosing, but with higher priority, finish their work
              }
          }
          // critical section...
               /*
           
              CS computations
               */
            printf("hello from Thread no %d",i);

          choosing[i] = 0;
          // non-critical section...
              }
             
  }

i am running solaris.

OUTPUT IS
bash-2.05$ ./a.out
creating thread 0
cannot creat thread..-1
creating thread 1
cannot creat thread..-1
creating thread 2
cannot creat thread..-1
creating thread 3
cannot creat thread..-1
creating thread 4
cannot creat thread..-1
creating thread 5
cannot creat thread..-1
creating thread 6
cannot creat thread..-1
creating thread 7
cannot creat thread..-1
creating thread 8
cannot creat thread..-1
creating thread 9
cannot creat thread..-1


please help.
thanx
0
 
Infinity08Commented:
I suggest using strerror() to get a bit more info on this error ...

Normally it will be either :

[EAGAIN]
    The system lacked the necessary resources to create another thread, or the system-imposed limit on the total number of threads in a process PTHREAD_THREADS_MAX would be exceeded.
[EINVAL]
    The value specified by attr is invalid.
[EPERM]
    The caller does not have appropriate permission to set the required scheduling parameters or scheduling policy.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now