Find the answer to your Linux question:
Results 1 to 2 of 2
Sorry, I changed my post because I found that solution, and didn't want to bother for that. But, since I didnt find a way to delete the post, I thought ...
  1. #1
    Just Joined!
    Join Date
    Jun 2010
    Posts
    11

    POSIX__Philosophers at dinner: Segmentation fault for wrong initialization

    Sorry, I changed my post because I found that solution, and didn't want to bother for that. But, since I didnt find a way to delete the post, I thought of updating it:
    Hi, I am beginning to write the code for the philosophers problem.
    As you know, they can eat, think or wait for forks. When they have 2 forks, one by each side, they eat. Then they set the 2 forks as available, and think.
    Each philosopher is a thread.
    My problem is that it gives segmentation fault, somehow, when I try to initialize. The thing I really don't understand, is why it sometimes gives segmentation fault on initialization #0!!! (But generally on initialization #1)
    I have set 5 initializations in total, defined by N, that is also the number of threads I'm working on.



    PHP Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<pthread.h>
    #include<errno.h>
    #include <time.h>

    #define N 5


    pthread_t vThreadM;
    pthread_mutex_t vMux;
    pthread_cond_t vCond;
    int disponibile;
    int succ;
    int prec;
    static 
    void filosofo(void arg);

    void Pthread_mutex_lock(pthread_mutex_t *mtx)
       { 
    int err;
       if ( ( 
    err=pthread_mutex_lock(mtx)) != ) {
          
    errno=err;
          
    perror("lock");
          
    pthread_exit((void *)17); /* maybe */
          
    printf("Printf errore lock\n");
       }
       else 
    printf("locked\n");}


    void Pthread_mutex_unlock(pthread_mutex_t *mtx) {

     
    int err;
       if ( ( 
    err=pthread_mutex_unlock(mtx)) != ) {
          
    errno=err;
          
    perror("lock");
          
    pthread_exit((void *)18); /* maybe */
          
    printf("Printf errore unlock\n");
       }
    }

    int main(void){


    pthread_t vThread=malloc(N*sizeof(pthread_t));
    pthread_mutex_t vMux=malloc(N*sizeof(pthread_mutex_t));
    pthread_cond_t vCond=malloc(N*sizeof(pthread_cond_t));
    int disponibile=malloc(N*sizeof(int));
    int succ=malloc(N*sizeof(int));
    int prec=malloc(N*sizeof(int));

     
    int i;

      for(
    i=0;i<N;i++)
        {
          
    pthread_cond_init(vCond+i,NULL);
          
    pthread_mutex_init(vMux+i,NULL);
          
    succ[i]=i+1;
          
    prec[i]=i-1;
          
    disponibile[i]=1;
        }
      
    succ[N-1]=0;
      
    prec[0]=N-1;


      for(
    i=0;i<N;i++)
        {
          
    printf("Creazione %d sta per avvenire\n",i);
          
    int point=malloc(sizeof(int));
          * 
    point=i;
          
    pthread_create(vThread+i,NULL,&filosofo,point);
          
    point=NULL;
          
    printf("Creazione %d avvenuta\n",i);
        }


      

      return(
    0);
    }



    static 
    void filosofo(void arg)
    {
      
    int k=*((int *)arg);
      while(
    1)
        {
          
    Pthread_mutex_lock(vMux+k);
          if (!(
    disponibile[k]&&disponibile[succ[k]]))
        
    printf("Filosofo numero %d: sto aspettando\n",k);

          while(!(
    disponibile[k]&&disponibile[succ[k]]))
        {
          if (!
    disponibile[k])
        
    pthread_cond_wait(vCond+k,vMux+k);
          if (!
    disponibile[succ[k]])
        
    pthread_cond_wait(vCond+succ[k],vMux+succ[k]);
        }
          
    printf("Filosofo numero %d: sto mangiando\n",k);
          
    pthread_cond_signal(vCond+succ[k]);
          
    pthread_cond_signal(vCond+k);
          
    printf("Filosofo numero %d: sto pensando\n",k);
          
    Pthread_mutex_unlock(vMux+k);
        }
      return (
    void*)0;


  2. #2
    Just Joined! shtromm's Avatar
    Join Date
    Jun 2010
    Posts
    31
    You may just pass those as a parameterlist - roughly:

    Code:
    static void *filosofo
        (pthread_t *vThread, 
         pthread_mutex_t *vMux,
         pthread_cond_t * vCond,
        void * arg)

Posting Permissions

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