Note that there are some explanatory texts on larger screens.

plurals
  1. POMultithread Directory and File Search
    text
    copied!<p>I am new to semaphores and the concepts of mutual exclusion and the Forum. I am supposed to recursively text search in files through directories using multithreading. The number of threads is to be given by the user. The issue with this code is it goes through one directory and then waits. I cannot figure out what is wrong.I am getting a segmentation fault error. Cannot figure out why is this happening . I need help ASAP. Thank You</p> <pre><code>#include &lt;iostream&gt; #include &lt;sys/wait.h&gt; #include &lt;sys/types.h&gt; #include &lt;pthread.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; #include &lt;dirent.h&gt; #include &lt;sys/stat.h&gt; #include &lt;fstream&gt; #include &lt;limits.h&gt; #include &lt;stdlib.h&gt; #include &lt;semaphore.h&gt; using namespace std; #include &lt;stdio.h&gt; int iDirectories=0; pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER; sem_t semaphore1; char searchStringThread[PATH_MAX]; int directories=0; class directoryQueue { private: struct Node { char directoryPath[PATH_MAX]; Node *next; }; Node *front; Node *rear; Node *nodeCount; public: directoryQueue(void) { front=NULL; rear=NULL; nodeCount=0; } void Enqueue(char array[PATH_MAX]) { Node *newNode; newNode=new Node; strcpy(newNode-&gt;directoryPath,array); newNode-&gt;next=NULL; if(isEmpty()) { front=newNode; rear=newNode; } else { rear-&gt;next=newNode; rear=newNode; } nodeCount++; } char * Dequeue(void) { Node *temp; if (isEmpty()) cout &lt;&lt; "Error ! Empty Queue "&lt;&lt;endl; else { char *deque; deque=new char[PATH_MAX]; strcpy(deque,front-&gt;directoryPath); temp = front-&gt;next; front = temp; nodeCount--; return deque; } } bool isEmpty(void) { if(nodeCount) return false; else return true; } void makeNull(void) { while(!isEmpty()) { Dequeue(); } } ~directoryQueue(void) { makeNull(); } }; directoryQueue saveDirectory; void *threadHandler(void *) { int thpath_length; char thPath[PATH_MAX]; char saveITDirectory[PATH_MAX]; char itDirectory[PATH_MAX]; int threadCount; struct dirent *iWalker; DIR *iDirectory; pthread_mutex_lock(&amp;mutex); threadCount=iDirectories++; pthread_mutex_unlock(&amp;mutex); sem_wait(&amp;semaphore1); pthread_mutex_lock(&amp;mutex); strcpy(itDirectory,saveDirectory.Dequeue()); pthread_mutex_unlock(&amp;mutex); iDirectory=opendir(itDirectory); if(iDirectory==NULL) { cout&lt;&lt;"Error"&lt;&lt;endl; cout&lt;&lt;itDirectory&lt;&lt;" Cannot be Opened"&lt;&lt;endl; exit(10000); } while((iWalker=readdir(iDirectory)) !=NULL) { if(iWalker-&gt;d_type==DT_REG) { strcpy(saveITDirectory,iWalker-&gt;d_name); cout&lt;&lt;itDirectory&lt;&lt;"/"&lt;&lt;endl; if (strcmp (saveITDirectory, "..") == 0 || strcmp (saveITDirectory, ".") == 0) { continue; } else { thpath_length = snprintf(thPath,PATH_MAX,"%s/%s",itDirectory,saveITDirectory); cout&lt;&lt;thPath&lt;&lt;endl; if (thpath_length &gt;= PATH_MAX) { cout&lt;&lt;"Path is too long"&lt;&lt;endl; exit (1000); } ifstream openFile; openFile.open(thPath); char line[1500]; int currentLine = 0; if (openFile.is_open()) { while (openFile.good()) { currentLine++; openFile.getline(line, 1500); if (strstr(line, searchStringThread) != NULL){ cout&lt;&lt;thPath&lt;&lt;": "&lt;&lt;currentLine&lt;&lt;": "&lt;&lt;line&lt;&lt;endl; cout&lt;&lt;"This was performed by Thread no. "&lt;&lt;threadCount&lt;&lt;endl; cout&lt;&lt;"ID :"&lt;&lt;pthread_self(); } } } openFile.close(); } } if (closedir (iDirectory)) { cout&lt;&lt;"Unable to close "&lt;&lt;itDirectory&lt;&lt;endl; exit (1000); } } } void walkThroughDirectory(char directory_name[PATH_MAX],char searchString[PATH_MAX]) { DIR * directory; struct dirent * walker; char d_name[PATH_MAX]; int path_length; char path[PATH_MAX]; directory=opendir(directory_name); if(directory==NULL) { cout&lt;&lt;"Error"&lt;&lt;endl; cout&lt;&lt;directory_name&lt;&lt;" Cannot be Opened"&lt;&lt;endl; exit(10000); } while((walker=readdir(directory)) !=NULL) { strcpy(d_name,walker-&gt;d_name); cout&lt;&lt;directory_name&lt;&lt;"/"&lt;&lt;endl; if (strcmp (d_name, "..") == 0 || strcmp (d_name, ".") == 0) { continue; } else { path_length = snprintf(path,PATH_MAX,"%s/%s",directory_name,d_name); cout&lt;&lt;path&lt;&lt;endl; if (path_length &gt;= PATH_MAX) { cout&lt;&lt;"Path is too long"&lt;&lt;endl; exit (1000); } if(walker-&gt;d_type==DT_DIR) { pthread_mutex_lock(&amp;mutex); saveDirectory.Enqueue(path); pthread_mutex_lock(&amp;mutex); sem_post(&amp;semaphore1); directories++; walkThroughDirectory (path,searchString); } else if(walker-&gt;d_type==DT_REG) { ifstream openFile; openFile.open(path); char line[1500]; int currentLine = 0; if (openFile.is_open()) { while (openFile.good()) { currentLine++; openFile.getline(line, 1500); if (strstr(line, searchString) != NULL) cout&lt;&lt;path&lt;&lt;": "&lt;&lt;currentLine&lt;&lt;": "&lt;&lt;line&lt;&lt;endl; } } openFile.close(); } } } if (closedir (directory)) { cout&lt;&lt;"Unable to close "&lt;&lt;directory_name&lt;&lt;endl; exit (1000); } } int main(int argc,char *argv[]) { char * name; cout&lt;&lt;"Total Directories "&lt;&lt; directories&lt;&lt;endl; name=get_current_dir_name(); cout&lt;&lt;"Current Directory is: "&lt;&lt;name&lt;&lt;endl; sem_init(&amp;semaphore1,0,0); strcpy(searchStringThread,argv[1]); int number_of_threads=atoi(argv[3]); pthread_t threads[number_of_threads]; walkThroughDirectory(argv[2],argv[1]); pthread_mutex_lock(&amp;mutex); saveDirectory.Enqueue(argv[2]); pthread_mutex_unlock(&amp;mutex); sem_post(&amp;semaphore1); for(int i=0;i&lt;number_of_threads;i++) { pthread_create(&amp;threads[i],NULL,threadHandler,NULL); } for(int j=0;j&lt;number_of_threads;j++) { pthread_join(threads[j],NULL); } while(saveDirectory.isEmpty()) { cout&lt;&lt;"Queue is Empty"&lt;&lt;endl; cout&lt;&lt;"Exiting"&lt;&lt;endl; exit(10000); } free(name); cout&lt;&lt;"Total Directories "&lt;&lt; directories&lt;&lt;endl; return 0; </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload