infinite loop, synchronizing gossip-based method


 
Thread Tools Search this Thread
Top Forums Programming infinite loop, synchronizing gossip-based method
# 1  
Old 12-04-2010
infinite loop, synchronizing gossip-based method

the following code runs, but it hangs somewhere, i don't know why,

Code:
#include<iostream>
#include<vector>
#include<cstdlib>
#include<ctime>
#include<list>
#include<pthread.h>
#include<cstring>
using namespace std;

pthread_mutex_t     listlock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t  pathlock = PTHREAD_MUTEX_INITIALIZER;

#define _NODES 8

 typedef struct{  
   int   id; 
   char  msg[10];
 } thread_parm_t;

struct Node
{  
  pthread_mutex_t f_lock;
  thread_parm_t *data;
  vector<int> edges;
  bool msg_received;               
};

list<struct Node*> sub_graph;
//list<Node*>::iterator it;

char get_node_name(int    );
void find_neighbours(struct Node* );

void  node_alloc(thread_parm_t *p)    // allocate the object 
{
    struct Node *nd;
       
    if((nd = (struct Node *)malloc(sizeof(struct Node))) != NULL)
    {

      if(pthread_mutex_init(&nd -> f_lock, NULL) != 0)
      {
     free(nd);
           exit(1);
      }

      //*** locking   
      pthread_mutex_lock(&nd -> f_lock);

        nd -> data = p; 
        nd -> msg_received = true;  
        find_neighbours(nd);
        
                             //*** unlocking
        pthread_mutex_unlock(&nd -> f_lock);

      
      //*** locking
        pthread_mutex_lock(&listlock);
             
        sub_graph.push_back(nd); 
     
                         //*** unlocking
        pthread_mutex_unlock(&listlock);\

      return;
    }
    else{
         cout << "Memory allocation failed...\n";
     exit(1);
    } 
}

int path[_NODES][_NODES] = {
      { 0, 1, 1, 1, 0, 0, 0, 0 },
      { 1, 0, 0, 1, 1, 0, 0, 0 },
      { 1, 0, 0, 1, 0, 1, 0, 0 },
      { 1, 1, 1, 0, 1, 0, 0, 0 },
      { 0, 1, 0, 1, 0, 1, 0, 1 },
      { 0, 0, 1, 0, 1, 0, 1, 1 },
      { 0, 0, 0, 0, 0, 1, 0, 1 },
      { 0, 0, 0, 0, 1, 1, 1, 0 }
                                 };
char get_node_name(int index)
{
  char c;
      switch(index)
    {
       case 0:  c = 'A';
                        break;
       case 1:  c = 'B';
                        break;
       case 2:  c = 'C';
                        break;
       case 3:  c = 'D';
                        break;
       case 4:  c = 'E';
                        break;
       case 5:  c = 'F';
                        break;
       case 6:  c = 'G';
                        break;
       case 7:  c = 'H';
                        break;
    }//end of switch

  return c;
}

void find_neighbours(struct Node *nd)
{
//*** locking
  pthread_mutex_lock(&pathlock);
  pthread_mutex_lock(&nd -> f_lock);
                //***

   int index = nd -> data -> id;
   for(int j=0; j < _NODES; j++){

        if(path[index][j]){

             nd->edges.push_back(j);          
        }
     }//end of for

//*** unlocking
  pthread_mutex_unlock(&nd -> f_lock);
  pthread_mutex_unlock(&pathlock);
                //***
}

struct Node* get_subgraph_node(int index)
{
   struct Node* nd;

   //*** locking
    pthread_mutex_lock(&listlock);
                //***
   list<Node*>::iterator it;

   for(it=sub_graph.begin(); it!=sub_graph.end(); it++)
   {
      //*** locking
        pthread_mutex_lock(&(*it) -> f_lock);
                           //***

      if((*it) -> data -> id == index)
      {
         if((nd = (struct Node *)malloc(sizeof(struct Node))) != NULL){

           //*** locking
            pthread_mutex_lock(&nd -> f_lock);
                                //***

             nd -> data -> id = (*it) -> data -> id;
                 nd -> edges = (*it) -> edges;
                 nd -> msg_received = (*it) -> msg_received;

      //*** unlocking 
         pthread_mutex_unlock(&nd -> f_lock);
         pthread_mutex_unlock(&listlock);
                                   //***
         return nd;  
         }
      }
           
   }//end of for

//*** unlocking   
  pthread_mutex_unlock(&listlock);
            //***

  return NULL;
}

void print_sub_graph()
{
   list<Node*>::iterator it;   

   for(it=sub_graph.begin(); it!=sub_graph.end(); it++)
   {

     cout << " MSG Received     MESSAGE\n\n";

     cout << "      " << (*it)->msg_received; cout << "           ";
     cout << (*it)->data->msg;
  
     cout << "\n\n... finding neighbours ...\n";
     int cap = (*it)->edges.size();
     cout << "... " << cap << " edges found ...\n";
     for(int index = 0; index < cap; index++){
        cout << "      " << get_node_name((*it)->data->id) << " --> " << get_node_name((*it)->edges[index]);
        cout << endl;
    }
     cout << "\n\n\n";
   }
}


bool search_subgraph(int index)
{
   list<Node*>::iterator it;

   pthread_mutex_lock(&listlock);
   for(it=sub_graph.begin(); it!=sub_graph.end(); it++)
   {
       pthread_mutex_lock(&(*it) -> f_lock);
     if((*it) -> data -> id == index)
       pthread_mutex_unlock(&(*it) -> f_lock);
       pthread_mutex_unlock(&listlock);
             return true;        
   }
   pthread_mutex_unlock(&listlock);
   return false;
}


void *thr_func(void *arg)
{
  // find adjacent vertices of the current node, if they do not exist in the sub_graph
  // allocate memory and new vertices to the sub_graph list, then create new thread
    thread_parm_t *tmp;
    thread_parm_t *p = (thread_parm_t *)arg; 
     
     struct Node *nd, *vertex;              
     struct timespec in, out;
     int index, random;
    
     in.tv_sec = 0;
     in.tv_nsec = 100000;
     vertex = NULL;
        
    
     // no locking for getting size()
 while(1)
{

   pthread_t tid;

     if((sub_graph.size() != _NODES))
     {
                      
             nd = get_subgraph_node(p -> id);
             
            //get a random neighbour
             srand((unsigned)time(0));

             //*** locking
             pthread_mutex_lock(&nd -> f_lock);
                                      //***
             
             index =  rand() % (int)nd -> edges.size();
             random = nd -> edges.at(index);
                     
             //*** unlocking
             pthread_mutex_unlock(&nd -> f_lock);            
                     //***

             if(!search_subgraph(random))
             {
               //new vertex allocation
               tmp -> id = random;
              
               for(int i = 0; i < 10; i++) 
               tmp -> msg[i] = p -> msg[i];           
               node_alloc(tmp);

              pthread_create(&tid, NULL, thr_func, (void *)tmp);
 
             }
             else
             {/*
                vertex = get_subgraph_node(random);
                if(vertex != NULL){
                  //sleep(1);
                  nanosleep(&in, &out);
                     continue;
                }*/
             }
     }

     else
    {
      cout << "\n\nmessage disseminated to " << sub_graph.size() << " nodes\n";
         
          //*** destroying structure lock
         
        
                               //***
          print_sub_graph();
        exit(0);                     

        }//end of else
  }//while
}


int main(int argc, char **argv)
{
  struct Node *nd;  
  pthread_t tid;
  thread_parm_t *parm = NULL;
  

  //sending message to the first node

  parm = (thread_parm_t *)malloc(sizeof(thread_parm_t));
  strcpy(parm->msg, "purple"); 
  parm->id = 0;
    
     node_alloc(parm);             
     pthread_create(&tid, NULL, thr_func, (void *)parm);
     
     //Only waiting for the first thread to terminate
     pthread_join(tid, NULL);
  
 return 0;
}


Last edited by saman_glorious; 12-05-2010 at 02:14 PM..
# 2  
Old 12-06-2010
Code:
bool search_subgraph(int index)
{
   list<Node*>::iterator it;

   pthread_mutex_lock(&listlock);
   for(it=sub_graph.begin(); it!=sub_graph.end(); it++)
   {
       pthread_mutex_lock(&(*it) -> f_lock);
     if((*it) -> data -> id == index)
       pthread_mutex_unlock(&(*it) -> f_lock);
       pthread_mutex_unlock(&listlock);
             return true;        
   }
   pthread_mutex_unlock(&listlock);
   return false;
}

Under certain circumstances this will lock listlock but never unlock it.
# 3  
Old 12-08-2010
more help on gossip implementation plz

but i have no idea how you guessed the problem! anyway
I would thank if send me your running code as it seems you already run the program

Last edited by saman_glorious; 12-08-2010 at 02:18 AM..
# 4  
Old 12-08-2010
Quote:
Originally Posted by saman_glorious
but i have no idea how you guessed the problem!
Easy: Look for code paths that lock a mutex but don't unlock it.
Quote:
I would thank if send me your running code as it seems you already run the program
I haven't completely followed the logic in your program, no. What I often look for in deadlocks is a pattern like this:
Code:
void something()
{
    lock_mutex();

    do_something();

    if(something_failed)
            return(error);

    unlock_mutex();
}

...meaning, if something_failed, something() returns early and doesn't unlock the mutex. Whatever else is waiting for could wait forever for an unlock that never happened.

How to fix that depends entirely on what you want to do with your function logic but might be as easy as
Code:
void something()
{
    lock_mutex();

    do_something();

    if(something_failed)
    {
            unlock_mutex();
            return(error);
    }

    unlock_mutex();
}

It's better to avoid return statements in a critical section entirely though, but how to do that also depends on your program logic.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Infinite loop query

I have a script script.shwhich is scheduled to run at 11 AM everyday. # script.sh Code: ./scb_script.sh & unfortunately scb_script.sh is running today in infinite loop as respective files are not available. My question, when script.sh starts running tomorrow, will the old process be... (1 Reply)
Discussion started by: JSKOBS
1 Replies

2. Shell Programming and Scripting

How to stop infinite loop

Im unable to stop the below infinite loop (bash script). Can someone tell me why this isnt responding to signals eg: ctrl+c (SIGINT) or ctrl+z c=0 test_loop() { c=$(($c+1)) echo "count value is : $c " sleep 1 test_loop } Im using: SunOS 5.10 PS: If run this as... (13 Replies)
Discussion started by: Arun_Linux
13 Replies

3. Shell Programming and Scripting

My for loop decides to become an infinite loop?

Hi, I was debating if I should put this in the dummies or scripts section, I apologize in advance if I chose poorly. Fairly new to Unix and BASH scripting but I thought I made it fairly well given my limited understanding. However, the output indicates that it's looping and I'm ending up with a... (5 Replies)
Discussion started by: gotreef
5 Replies

4. Homework & Coursework Questions

Help with infinite loop problem

1. The problem statement, all variables and given/known data: My problem is an infinite loop when i press any other key other then Y or y in the while loop. what i want it to do is return to the normal script outside of it if pressing N or n or keep asking the same question if its any other... (4 Replies)
Discussion started by: Ren_kun
4 Replies

5. Shell Programming and Scripting

Call a infinite loop

Hi All, I need to run an infinite loop. requirement below: function1 --> creates a file file1 function2 ---> need to call if the file creates i am running these both function via a script --> script.sh i need to run the function1 first and if the file file1 creates then need to run the... (3 Replies)
Discussion started by: satyaranjon
3 Replies

6. UNIX for Advanced & Expert Users

Procmail and infinite loop

I wanted to copy (not forward but copy) all incoming email to another address of mine. It worked, but now I encountered an infinite loop problem: When the second address doesn't like the content and bounces the message back, the bounce message will be sent back and forth. So, what I have in... (1 Reply)
Discussion started by: distill
1 Replies

7. Shell Programming and Scripting

Infinite while loop

what is the difference between while:,while true and while false? (6 Replies)
Discussion started by: proactiveaditya
6 Replies

8. Programming

Is my code in an Infinite Loop?

Production C code compiled without the dash-g option is running, and seems to be in an infinite loop. Is there a way to tell? Is there a diagnostic tool that will report what objects or what lines of code or even what functions are being executed? Or is my best option to kill it with a dump? ... (5 Replies)
Discussion started by: marcus121
5 Replies

9. Shell Programming and Scripting

Infinite loop not looping

Hi guys, I'm having a problem getting my infinite loop to loop. It simply reads in the users choice form the menu, executes the corresponding case statement and quits instead of looping back to the main menu again. I have a feeling it might be something with my if then statements within the case... (2 Replies)
Discussion started by: hootdocta5
2 Replies

10. Solaris

ls command in infinite Loop

Hi, whenever I am giving a 'ls' command system is going into infinite loop displaying the current home directory. There is no separate shell script/file with ls name anywhere in the system. I am using Solaris 10. Any help / guidance in solving this problem is highly appreciated. ... (3 Replies)
Discussion started by: umakant
3 Replies
Login or Register to Ask a Question