openfilename iosin if file cerr Couldnt open filen return NULL while fileeof

Openfilename iosin if file cerr couldnt open filen

This preview shows page 40 - 42 out of 102 pages.

file.open(filename, ios::in); if (! file) { cerr << "Couldn't open file\n"; return NULL; } while (!file.eof()) { file.getline(buffer,bufsize); cout << filename << ":" <<buffer << endl; // Critical section ret = pthread_mutex_lock(&MUTEX); LINECOUNT++; ret = pthread_mutex_unlock(&MUTEX); // Try uncommenting this .... // Yield the process, to allow next thread to be run // sched_yield(); } file.close(); } In this version of the program, a separate thread is spawned for each file. First we call the function pthread_create() for each file we encounter. A new thread is spawned with a pointer to the function the thread should execute (in this case the same function for all threads), called ParseFile() , which reads lines from the respective files and increments the global variable LINECOUNT . Several things are important here. The main program is itself a thread. It is essential that we tell the main program to wait for the additional threads to join the main program before exiting, otherwise the main program will exit and kill all of the child threads immediately. Thread join-semantics are like wait-semantics for normal processes.
Image of page 40
Each of the threads updates the same global variable. Suppose now that two threads are running on different CPUs. It is possible that both threads would try to alter the value of the variable LINECOUNT simultaneously. This is called a race condition and can lead to unpredictable results. For this reason we use a mutex to lock the variable while it is being updated. We shall discuss this more in the next section. A final point to note is the commented out lines in the ParseFile() function. The call sched_yield() tells a running thread to give itself up to the scheduler, so that the next thread to be scheduled can run instead. This function can be used to switch between several threads. By calling this function after each line is read from the files, we can spread the the CPU time evenly between each thread. Actually, it is difficult to predict precisely which threads will be scheduled and when, because the threads in our program here are only a small number, compared to the total number of threads waiting to be scheduled by the system. The interaction with disk I/O can also have a complicated effect on the scheduling. On a single CPU system, threads are usually scheduled FCFS in a queue. If we yield after every instruction, it has the effect of simulating round-robin scheduling. 4.3.6 Example: LWPs in Solaris 1 Early solaris systems had user-level threads only, which were called light weight processes. Since the kernel was single threaded, only one user-level thread could run at any given time. To create a threaded process in solaris 1, one simply has to execute a LWP system call. The `lightweight processes library' then converts the normal process into a process descriptor plus a thread. Here is the simplest example /********************************************************************/ /* */ /* Creating a light weight process in SunOS 4.1.3 */ /* */ /********************************************************************/ #include <lwp/lwp.h> #include <lwp/stackdep.h> #define MINSTACKSZ 1024 #define STACKSIZE 1000 + MINSTACKSZ #define MAXPRIORITY 10 /*********************************************************************/ stkalign_t stack[STACKSIZE]; /*********************************************************************/ /* Zone 0 */ /*********************************************************************/ main () { thread_t tid; int task(); pod_setmaxpri(MAXPRIORITY);
Image of page 41
Image of page 42

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

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors