开发者

Shared library used by two processes

开发者 https://www.devze.com 2023-03-29 03:10 出处:网络
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 s

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 de开发者_运维百科termined.

  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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消