[ale] Linuxthreads question

Armsby John-G16665 John.Armsby at motorola.com
Tue Jun 24 15:15:10 EDT 2003


Don't laugh.... I wrote the code below.  It runs beautifully on RedHat and Mandrake.  Basically I have some text files which need to be SEARCHED (based upon a web form) and printed out in a particular order.  File number 1 needs to print, then file number 2, etc.  I created some objects which parse the form, finds lines in the text files, and then prints out html with the data.  

In a crude fashion i believe I created a series of threads, each thread parses and gets the data for a particular file.  Each threads waits its turn to print out.  The good news it works.  I was unable to write a single function to spawn each thread.... I cut and paste.... 

You might want to learn from what works or what you see as unforgivably ugly.  It does seems to demonstrate threads in Linux.  As you can image, getting the threads to work in a certain order is a real challenge.

John

****** here it is don't laugh ****************************


// newVaultGrep.cpp

#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include "parseObject.h"
#include "findObject.h"
#include "printObject.h"
#include <cstring>
#include <iostream>
   using std::cout;
   using std:: endl;
   using std::flush;
   using std::cin;
#include<ctime>

#define BUFSIZE 800


/* g++ parseObject.cpp findObject.cpp printObject.cpp newVaultGrep.cpp  -o /
 var/www/cgi-bin/newVaultGrep.cgi -lpthread
*/

   void *thread_function(void *args) ;
   void *thread_function2(void *args) ;
   void *thread_function3(void *args) ;
   void *thread_function4(void *args) ;
   void *thread_function5(void *args) ;
   void *thread_function6(void *args) ;
   void *thread_function7(void *args) ;



   void printInitialHtml();
   void printEndHTML();
   pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;
   char myQuery[BUFSIZE]; 
   int printableFlag = 1;

   time_t seconds;
   time_t seconds2;
   // clock_t start,finish;
   // double duration;



   int  main(int argc, char *argv[])
   
   {
   seconds = time (NULL);
  //   start = clock(); 
   
      pthread_t  mythread;	// lawrenceville earth
      pthread_t  mythread2; // minx
      pthread_t  mythread3; // powerserver
      pthread_t  mythread4; // penang
      pthread_t  mythread5; // tianjin
      pthread_t  mythread6; // shanghai
      pthread_t  mythread7; // dublin
   
   
      if (argc > 1)
         strcpy(myQuery,argv[1]);
      else
      {
         cin.get();
         cin >> myQuery;
         cin.get();
      }
   //------------------------------------------------------
   
      printInitialHtml();
   
   
      if ( pthread_create( &mythread, NULL, thread_function, NULL) ) {
         cout << "error creating lawrenceville thread.";
         abort();
      }
   
   
   
   
   
   
   
   
      if ( pthread_create( &mythread2, NULL, thread_function2, NULL) ) {
         cout << "error creating minx thread.";
         abort();
      }
   
   
   
   
   
   
   
   
   
      if ( pthread_create( &mythread3, NULL, thread_function3, NULL) ) {
         cout << "error creating powerserver thread.";
         abort();
      }
   
   
   
   
   
   
   
      if ( pthread_create( &mythread4, NULL, thread_function4, NULL) ) {
         cout << "error creating penang thread.";
         abort();
      }
   
   
   
      if ( pthread_create( &mythread5, NULL, thread_function5, NULL) ) {
         cout << "error creating tianjin thread.";
         abort();
      }
   
   
   
   
      if ( pthread_create( &mythread6, NULL, thread_function6, NULL) ) {
         cout << "error creating shanghai thread.";
         abort();
      }
   
   
   
   
      if ( pthread_create( &mythread7, NULL, thread_function7, NULL) ) {
         cout << "error creating dublin thread.";
         abort();
      }
   
   
   
   
   
   
   //-------------------------------------------------
   
   
   
   
   
      if ( pthread_join ( mythread, NULL ) ) {
         cout << "error joining lawrenceville thread.";
         abort();
      }
   
   
   
   
      if ( pthread_join ( mythread2, NULL ) ) {
         cout << "error joining minx thread.";
         abort();
      }
   
   
   
   
      if ( pthread_join ( mythread3, NULL ) ) {
         cout << "error joining powerserver thread.";
         abort();
      }
   
   
   
      if ( pthread_join ( mythread4, NULL ) ) {
         cout << "error joining penang thread.";
         abort();
      }
   
   
   
      if ( pthread_join ( mythread5, NULL ) ) {
         cout << "error joining tianjin thread.";
         abort();
      }
   
   
   
   
      if ( pthread_join ( mythread6, NULL ) ) {
         cout << "error joining shanghai thread.";
         abort();
      }
   
   
   
      if ( pthread_join ( mythread7, NULL ) ) {
         cout << "error joining dublin thread.";
         abort();
      }
   
   
   
   
//	finish =clock();
//	duration = (double)(finish - start) / CLOCKS_PER_SEC;

	
//	cout << "<p>start: " << start << "<p>"
//	     << "<p>finish: " << finish << "<p>"
//	     <<	flush;

//	cout << "<p>" << duration << " seconds" << flush;
   //-----------------------------------------------------------
        seconds2 = time (NULL);
	cout << "<p>" << (seconds2-seconds) <<	" seconds..." << flush;
       printEndHTML();
      return 0;
   }




   void *thread_function(void *args) 
   {
      parseObject p(myQuery);
     findObject Lawrenceville(p,p.getQueryStringArrayElement(3)    ); 
  //   findObject Lawrenceville(p, ".vault_log");
   
   
   
      while(printableFlag != 1)
      {
         usleep(50);      
      }
   
   
      pthread_mutex_lock(&mymutex);
   
      printObject printIT1(Lawrenceville,
                          "Lawrenceville Listing...",
                          '/',
                          "Program Name",
                          "FileName_Rev",
                          "Date",
                          "http://earth.wepd.mot.com");
      printableFlag = 2;
   
   
      pthread_mutex_unlock(&mymutex);   
   
   
   
      return NULL;
   }


   void *thread_function2(void *args) 
   {
      parseObject p(myQuery);
      findObject Minx(p, ".Minx.txt");
   
      while(printableFlag != 2)
      {
          usleep(50);
      }
   
   
      pthread_mutex_lock(&mymutex);
   
      printObject printIT2(Minx,
                          "Minx Archives...",
                          '/',
                          "Part Number",
                          "Description",
                          "ECN/PAX No.",
                          "http://earth.wepd.mot.com");
      printableFlag = 3;
      pthread_mutex_unlock(&mymutex);   
   
   
      return NULL;
   }

   void *thread_function3(void *args) 
   {
      parseObject p(myQuery);
      findObject Powerserver(p, "powerserver_log");
      while(printableFlag != 3)
      {
          usleep(50);
      }
   
      pthread_mutex_lock(&mymutex);
   
   
      printObject printIT3(Powerserver,
                          "Powerserver Archives - Uncontrolled...",
                          '\\',
                          "Program Name",
                          "FileName_Rev",
                          "Date",
                          "http://ga25-fs.wepd.mot.com");
      printableFlag =4;
      pthread_mutex_unlock(&mymutex);   
   
      return NULL;
   
   
   }


   void *thread_function4(void *args) 
   {
      parseObject p(myQuery);
      findObject Penang(p, "penang_log");
   
      while(printableFlag != 4)
      {
          usleep(50);
      }
   
      pthread_mutex_lock(&mymutex);
   
   
      printObject printIT4(Penang,
                          "Penang Archives - Uncontrolled...",
                          '\\',
                          "Program Name",
                          "FileName_Rev",
                          "Date",
                          "http://esg-pen14.pen.comm.mot.com");
   
      printableFlag =5;
      pthread_mutex_unlock(&mymutex);   
   
   
      return NULL;
   }


   void *thread_function5(void *args) 
   {
      parseObject p(myQuery);
      findObject Tianjin(p, "tianjin_log");
   
      while(printableFlag != 5)
      {
            usleep(50);
      }
   
      pthread_mutex_lock(&mymutex);
   
   
      printObject printIT5(Tianjin,
                          "Tianjin Archives - Uncontrolled...",
                          '\\',
                          "Program Name",
                          "FileName_Rev",
                          "Date",
                          "http://esgtjn07");
   
      printableFlag =6;
      pthread_mutex_unlock(&mymutex);   
   
   
      return NULL;
   }



   void *thread_function6(void *args) 
   {
      parseObject p(myQuery);
      findObject Shanghai(p, "tianjin2_log");
   
      while(printableFlag != 6)
      {
          usleep(50);
      }
   
      pthread_mutex_lock(&mymutex);
   
   
      printObject printIT6(Shanghai,
                          "Shanghai Archives - Uncontrolled...",
                          '\\',
                          "Program Name",
                          "FileName_Rev",
                          "Date",
                          "http://esgtjn03");
   
      printableFlag =7;
      pthread_mutex_unlock(&mymutex);   
   
   
      return NULL;
   }




   void *thread_function7(void *args) 
   {
      parseObject p(myQuery);
      findObject Dublin(p, "dublin_log");
   
      while(printableFlag != 7)
      {
          usleep(50);
      }
   
      pthread_mutex_lock(&mymutex);
   
   
      printObject printIT7(Dublin,
                          "Dublin Archives - Uncontrolled...",
                          '\\',
                          "Program Name",
                          "FileName_Rev",
                          "Date",
                          "http://epd_dub02");
   
      printableFlag =8;
      pthread_mutex_unlock(&mymutex);   
   
   
      return NULL;
   }


























-----Original Message-----
From: Christopher Fowler [mailto:cfowler at outpostsentinel.com]
To: ale at ale.org
Sent: Tuesday, June 24, 2003 10:22 AM
To: ale at ale.org
Subject: Re: [ale] Linuxthreads question


On Tue, Jun 24, 2003 at 10:00:44AM -0400, John Mills wrote:
> 
> That was my first thought, roughly, and may still be a better approach. I
> was troubled by FUD as to whether I could have a large enough number of
> shared segments (8-10), then I found 'clone' and 'linuxthreads' which
> seemed to do what I wanted. I could use one BEEGGG segment, but I wanted 
> to allocate and attach each "writer"s buffer individually as a dedicated 
> resource. 

Think about it.  What is shared memory?  It is really just data that
is accessible via anyone with authority to read and/or write.  Here are 
possibilites of share memory.

1) Huge shared memory segments
2) File in /tmp/<filename>
3) Mount a tmpfs and use ram to store
   a temp file
4) Use a database and have an API that
   retrieves the data and converts to pointer

5) etc..

The posibilites are endless.  Shared memory was just an example.
You'll still need a locking system but you can store the 
data anywhere you like.  The benefit of using #1 is that the
pointer in your program is actually pointing to the data.
#2 and #3 will require mmap if you want a pointer to point to
that data.
#4 is a copy in and out system that can have discrepencies.

> 
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale
_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale





More information about the Ale mailing list