Skip to content Skip to navigation

Connexions

You are here: Home » Content » Concurrent Processes: Basic Issues: Homework Exercises

Navigation

Content Actions

  • Download module PDF
  • Add to ...
    Add the module to:
    • My Favorites
    • A lens
    • An external social bookmarking service
    • My Favorites (What is 'My Favorites'?)
      'My Favorites' is a special kind of lens which you can use to bookmark modules and collections directly in Connexions. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need a Connexions account to use 'My Favorites'.
    • A lens (What is a lens?)

      Definition of a lens

      Lenses

      A lens is a custom view of Connexions content. You can think of it as a fancy kind of list that will let you see Connexions through the eyes of organizations and people you trust.

      What is in a lens?

      Lens makers point to Connexions materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

      Who can create a lens?

      Any individual Connexions member, a community, or a respected organization.

    • External bookmarks
  • E-mail the authors

Recently Viewed

This feature requires Javascript to be enabled.

Concurrent Processes: Basic Issues: Homework Exercises

Module by: John Greiner, Ian Barland, Moshe Vardi

Exercise 1

Assume we have two concurrent threads/processes, each with simple straight-line code and sharing a global variable x. Assume that each individual statement is atomic.


 1  thread0:
 2  {
 3    x=0
 4    x=x+1
 5  }

 6  thread1:
 7  {
 8    x=0
 9    x=x+1
10    x=x+2
11  }
        

Without using SPIN, give one trace illustrating each of the possible final values of x.

Solution 1

There are three possible final values of x: 1, 3, 4.

Figure 1: For 1 and 3, the only trace for each is given. For 4, there are many traces, with one sample given.
Subfigure 1.1Subfigure 1.2Subfigure 1.3
Subfigure 1.1 (homework134_trace1.png)Subfigure 1.2 (homework134_trace3.png)Subfigure 1.3 (homework134_trace4.png)

Exercise 2

Answer the previous exercise, except with different atomicity. Assume that each variable lookup, variable assignment, and addition are atomic, as in a typical machine language. Specifically, use the following code:


 1  thread0:
 2  {
 3    x  =  0
 4    r0 =  x
 5    r0 += 1
 6    x  =  r0
 7  }

 8  thread1:
 9  {
10    x  =  0
11    r1 =  x
12    r1 += 1
13    x  =  r1
14    r1 =  x
15    r1 += 2
16    x  =  r1
17  }
          

Solution 2

The values 1, 3, and 4 are possible as before. It should be clear that values greater than 4 are impossible, since at most a total of 4 can ever be added. Similarly, values less than 1 are impossible, since at least 1 is added after the last variable lookup. So, we're left with the question of whether 2 is a possible result. It is, and one possible trace is given.

Figure 2: A trace resulting in the value 2.
Figure 2 (homework134_trace2.png)

Comments, questions, feedback, criticisms?

Send feedback