RajSanpui
RajSanpui

Reputation: 12064

Shared library used by two processes

Somebody asked me this question:

Two processes P1 and P2 are using a shared library (UNIX system). The shared library has a global variable G1, and a getval( ) and setval( ) function which gets and sets the value of G1.

Here is the sequence of events:

P1:Calls setval(10)  P1:Goes to sleep P2: Calls setval(20) P2:Goes to sleep P1:awake from sleep P1: Calls val=getval( )

Now what will be the value of val? Which P1 receives? Is it 10 or 20?

What will be your answer, with explanation. Choices are:

  1. val=10, this is because every process executes and has its own address space although multiple processes are using the same shared library. So, although G1 is a global variable, its value will be unique for every process.

  2. P1. receives a value of 20, as the value was altered by process P2 when P1 was asleep.

  3. The value cannot be determined.

  4. You can add any other answer if you wish apart from these 4 choices.

Well, 1. was my answer. Do you agree?

P1 and P2 do not co-operate and are independent processes

Upvotes: 1

Views: 1311

Answers (1)

glglgl
glglgl

Reputation: 91028

val=10. Indeed, every process and has its (not it's, "it's" is "it is") own address space. The library has no data space by itself.

Upvotes: 3

Related Questions