Skip to content Skip to navigation

OpenStax-CNX

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

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

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

What is in a lens?

Lens makers point to 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 member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
  • Rice Digital Scholarship

    This module is included in aLens by: Digital Scholarship at Rice UniversityAs a part of collection: "Model Checking Concurrent Programs"

    Click the "Rice Digital Scholarship" link to see all content affiliated with them.

Also in these lenses

  • eScience, eResearch and Computational Problem Solving

    This module is included inLens: eScience, eResearch and Computational Problem Solving
    By: Jan E. OdegardAs a part of collection: "Model Checking Concurrent Programs"

    Click the "eScience, eResearch and Computational Problem Solving" link to see all content selected in this lens.

Recently Viewed

This feature requires Javascript to be enabled.
 

Concurrent Processes: Basic Issues: Homework Exercises

Module by: John Greiner, Ian Barland, Moshe Vardi. E-mail the authors

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

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.
(a) (b) (c)
Figure 1(a) (homework134_trace1.png)Figure 1(b) (homework134_trace3.png)Figure 1(c) (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

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)

Content actions

Download module as:

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

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

What is in a lens?

Lens makers point to 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 member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks