This becomes a lwp here printfDone Now

This becomes a lwp here printfdone now

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

/* This becomes a lwp here */ lwp_create(&tid,task,MAXPRIORITY,0,STKTOP(stack),0); printf("Done! - Now other threads can run...\n"); } /*********************************************************************/ /* Zone 1 */ /*********************************************************************/
Image of page 41
task () { printf("Task: next thread after main()!\n"); } Here is an example program containing several threads which wait for each other. /********************************************************************/ /* */ /* Creating a light weight process in sunos 4.1.3 (Solaris 1) */ /* */ /* Yielding to other processes */ /* */ /********************************************************************/ #include <lwp/lwp.h> #include <lwp/stackdep.h> #define MINSTACKSZ 1024 #define STACKCACHE 1000 #define STACKSIZE STACKCACHE + MINSTACKSZ #define MAXPRIORITY 10 #define MINPRIORITY 1 /*********************************************************************/ stkalign_t stack[STACKSIZE]; /*********************************************************************/ /* Zone 0 */ /*********************************************************************/ main () { thread_t tid_main; thread_t tid_prog1; thread_t tid_prog2; int prog1(), prog2(); lwp_self(&tid_main); /* Get main's tid */ lwp_setstkcache(STACKCACHE,3); /* Make a cache for each prog */ lwp_create(&tid_prog1,prog1,MINPRIORITY,0,lwp_newstk(),0); lwp_create(&tid_prog2,prog2,MINPRIORITY,0,lwp_newstk(),0); printf("One "); lwp_yield(THREADNULL); printf("Four "); lwp_yield(tid_prog2); printf("Six "); exit(0); }
Image of page 42
/*********************************************************************/ /* Zone 1,2.. */ /*********************************************************************/ prog1 () { printf("Two "); if (lwp_yield(THREADNULL) < 0) { lwp_perror("Bad yield"); return; } printf("Seven \n"); } /*********************************************************************/ prog2 () { printf("Three "); lwp_yield(THREADNULL); printf("Five "); } 4.4 Synchronization of processes and threads When two or more processes work on the same data simultaneously strange things can happen. We have already seen one example in the threaded file reader in previous section: when two parallel threads attempt to update the same variable simultaneously, the result is unpredictable. The value of the variable afterwards depends on which of the two threads was the last one to change the value. This is called a race condition . The value depends on which of the threads wins the race to update the variable. What we need in a multitasking system is a way of making such situations predictable. This is called serialization . 4.4.1 Problems with sharing for processes It is not only threads which need to be synchronized. Suppose one user is running a script program and editing the program simultaneously. The script is read in line by line. During the execution of the script, the user adds four lines to the beginning of the file and saves the file. Suddenly, when the next line of the executing script gets read, the pointer to the next line points to the wrong location and it reads in the same line it already read in four lines ago! Everything in the program is suddenly shifted by four lines, without the process execting the script knowing about it. This example (which can actually happen in the UNIX shell) may or may not turn out to be serious - clearly, in general, it can be quite catastrophic. It is a problem of synchronization on the part of the user and the filesystem 4.1 . We must consider programs which share data.
Image of page 43
1. When do we need to prevent programs from accessing data simultaneously? If there are 100 processes which want to read from a file, this will cause no problems because the data themselves are not changed by a read operation. A problem only arises if more than one of the parties wants to modify the data.
Image of page 44
Image of page 45

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