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:
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.
P1. receives a value of 20, as the value was altered by process P2 when P1 was asleep.
The value cannot be de开发者_运维百科termined.
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.
精彩评论