435 Example POSIX pthreads The POSIX standardization organization has developed

435 example posix pthreads the posix standardization

This preview shows page 37 - 41 out of 102 pages.

4.3.5 Example: POSIX pthreads The POSIX standardization organization has developed a standard set of function calls for use of user-level threads. This library is called the pthread interface. Let's look at an example program which counts the number of lines in a list of files. This program will serve as an example for the remainder of this chapter. We shall first present the program without threads, and then rewrite it, starting a new thread for each file. The threaded version of the program has the possibility of reading several of the files in parallel and is in principle more efficient, whereas the non-threaded version must read the files sequentially.
Image of page 37
The non-threaded version of the program looks like this: // // Count the number of lines in a number of files, non threaded // version. // /////////////////////////////////////////////////////////////////////// / #include <iostream.h> #include <fstream.h> const int bufsize = 100; void ParseFile(char *); int LINECOUNT = 0; /********************************************************************** / main () { cout << "Single threaded parent...\n"; ParseFile("proc1"); ParseFile("proc2"); ParseFile("proc3"); ParseFile("proc4"); cout << "Number of lines = %d\n",LINECOUNT; } /********************************************************************** / void ParseFile(char *filename) { fstream file; char buffer[bufsize]; cout << "Trying to open " << filename << endl; file.open(filename, ios::in); if (! file) { cerr << "Couldn't open file\n"; return; } while (!file.eof()) { file.getline(buffer,bufsize); cout << filename << ":" <<buffer << endl; LINECOUNT++;
Image of page 38
} file.close(); } This program calls the function ParseFile() several times to open and count the number of lines in a series of files. The number of lines is held in a global variable called LINECOUNT . A global variable is, by definition, shared data. This will cause a problem when we try to parallelize the program using threads. Here is the threaded version: // // Count the number of lines in a number of files. // Illustrates use of multithreading. Note: run this program // several times to see how the threads get scheduled on the system. // Scheduling will be different each time since the system has lots // of threads running, which we do not see and these will affect the // scheduling of our program. // // Note that, on a multiprocessor system, this program has a potential // race condition to update the shared variable LINECOUNT, so we // must use a mutex to make a short critical section whenever accessing // this shared variable. // // This program uses POSIX threads (pthreads) // /////////////////////////////////////////////////////////////////////// #include <iostream.h> #include <fstream.h> #include <pthread.h> #include <sched.h> const int bufsize = 100; const int maxfiles = 4; void *ParseFile(char *); // Must be void *, defined in pthread.h ! int LINECOUNT = 0; pthread_mutex_t MUTEX = PTHREAD_MUTEX_INITIALIZER; /********************************************************************** / main () { pthread_t tid[maxfiles];; int i,ret; // Create a thread for each file ret = pthread_create(&(tid[0]), NULL, ParseFile,"proc1"); ret = pthread_create(&(tid[1]), NULL, ParseFile,"proc2"); ret = pthread_create(&(tid[2]), NULL, ParseFile,"proc3"); ret = pthread_create(&(tid[3]), NULL, ParseFile,"proc4"); cout << "Parent thread waiting...\n"; // If we don't wait for the threads, they will be killed // before they can start...
Image of page 39
for (i = 0; i < maxfiles; i++) { ret = pthread_join(tid[i],(void **)NULL); } cout << "Parent thread continuing\n"; cout << "Number of lines = " << LINECOUNT << endl; } /********************************************************************** / void *ParseFile(char *filename) { fstream file; char buffer[bufsize]; int ret; cout << "Trying to open " << filename << endl; file.open(filename, ios::in);
Image of page 40
Image of page 41

You've reached the end of your free preview.

Want to read all 102 pages?

  • One '20

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes