Banker's algorithm solution for given system state - bankers-algorithm

I have final test in OS Course, in the morning tomorrow, and I got stuck with an problem.
Say we have 4 processes in our system: P1, P2, P3, P4, and 4 different resources: R1, R2, R3, R4.
Current state of the system shown in this picture.
The question is - "Is system in deadlock or not?" I solved few similar problems, so, I think, the system IS in deadlock, because there aren't enough available resources (for example P4 requests 2 instances of R3, but in available list exists only 1 resource of R3).
I'm little confused because answer of my practitioner is : system isn't in deadlock, and processes running in this order: P4->P3->P1->P2
Which Solution is Right?
Thanks.

Your instructor is correct. In particular, P4 already has (1 0 1 1), and thus needs only (0 0 1 0), which can be easily met by the available resources. After P4 is served the resources, it completes and releases everything it was using (including its Current Allocation). The OS can then send those resources through to the remaining processes (in the correct sequence), thus no deadlock.

According to the explanation here, P4->P3->P2->P1 wont work, because P2 will make R1 0 , and that can lead to deadlock of P2 goes in loop or is not available for a long time P1 waits for all that time as R1 is zero. ?
As After P3 and P4 are done, Avail Matrix becomes 3334 , which is enough to satisfy needs of P2 as well ?.

safe sequence is p3,p4,p1,p2
p1 false
p2 false
p3 true total 5555 - execute 1st
p4 true total 6566 - execute 2nd
loop cont
p1 true total 7678 - execute 3rd
p2 true total 9788 - execute 4th
safe sequence (p3,p4,p1,p2)

I think the system is in a deadlock because request p4 cannot be satisfied. We don't look at the allocation we look at the request. If it's not satisfied then it's a deadlock. Remember processes are allowed to hold some resources while requesting others

Related

Number of processes

I am preparing for an exam, doing already solved exercises. However I have trouble with one concerning processes, because I think I do it right, however, the answer does not match mine. It is the following problem:
Below is the code for a program named Agent_Smith.c. Including the initial parent process, how many Agent_Smith processes are created? Assume there are no errors.
https://imgur.com/NLvbzKn
I know that only child processes have pid=0, while their parent inherits their actual pid (!=0). So I am doing this: (assuming smith = fork( ); is fork1 ect.)
https://imgur.com/9sHNRP1
In the exercise it says that the processes including the initial parent are 12, but mine are 13 and I do not know where I do wrong. I think this is a mistake in the solution, however I am not sure so I wanted to ask. I am new to this website, so apologies if anything is done incorrectly.
I also find 12 processes. I do not really understand your drawing, but here is what I find if all the forks are numbered 1-6 in program order. Processes are numbered as they would be created in program order.
P0 is parent
P0 -> fork1 P1
P1 -> fork2 P2
-> fork3 P3
-> fork4 P4
P2 -> fork3 P5
-> fork4 P6
P3 -> fork4 P7
P5 -> fork4 P8
P0 -> fork5 P9
-> fork6 P10
P9 -> fork6 P11
So there 12 processes P0-P11.
By the way, never give a code as an image. There are ways to format them inline.
Let us denote parent by p0.
So first fork() will generate a new child, so we will have 2 process till now.
Parent will get a value greater than 0. So it will enter in the for loop, next 2 fork() will generate 3 new process. So till now we have 1 parent and 4 new process.
Now first child process will execute the else statement. Three fork() will generate total 8 process including first child and 7 new process. In the end we will get 1 parent, 1 child generated in starting 3 new process generated in if condition, 7 new process generated in else condition. So total 12 processes.

modify several bits at once without wrecking operation - 8051 ports

I'm trying to figure out if there's a simple way to modify several bits of an 8051 port at once.
I'll re-explain my dilemma more concrete style.
My application has Port 2 divided for two functions. Let's call them FA and FB.
FA relies on the output value of the low three bits, and FB relies on the remaining 5 bits but they can be I/O.
Because one bit in FB is an output bit that controls a clock, I don't want to modify any part of FB when I'm modifying FA and vice-versa.
The following kinds of commands won't work for me:
mov P2,#07h
mov P2,#80h
This is because such commands will overwrite the values for both functions possibly triggering one function at the wrong time.
I could get away with something like the following:
setb P2.7
setb P2.6
clr P2.5
clr P2.4
setb P2.3
setb P2.2
clr P2.1
clr P2.0
Because I know those commands can set individual bits one by one without affecting the remaining bits, but I'd like to try to set more than one bit at a time.
I read about read-modify-write and the internet tells me MOV is different from logic operations on port pins, so I'm going to try to take a guess here to see if I'm right based on my example:
Say for FB I want to output all logic highs and for FA I want to output all logic lows except that lowest bit=1. I want to operate on one function at a time.
I know this won't work since it sets everything at once:
mov P2, #0F9h
But somehow I think using logic on the ports might work, but I want to know if my thinking is correct. Let's assume in the two cases below that P2 has the value set to #0A1h via the mov command, and the pins of P2 are all connected to ground.
Is this correct?:
anl P2,#F8h ;value of P2 = #0A1h AND #F8h = #0A0h
orl P2,#F9h ;value of P2 = #0A0h OR #F9h = #0F9h
anl P2,#F0h ;value of P2 = #0F9h AND #F0h = #0F0h
Or is this more correct?:
anl P2,#F8h ;value of P2 = #00h AND #F8h = #00h
orl P2,#F9h ;value of P2 = #00h OR #F9h = #0F9h
anl P2,#F0h ;value of P2 = #00h AND #F0h = #00h
In the second example I started the equations with #00h because thats the value P2 would produce if I used MOV A,P2 and then wanted value for A, but I'm not sure if that value will be used in the anl and orl calculations.
So overall, my question is, can I use anl and orl to modify only certain bits of a port without disturbing the rest of the bits? or am I stuck with using bit manipulation commands like setb and clr?
First of all, I want to add the following aspect. The approach involving SETB and CLR instructions will require more machine cycles to execute, and might be infeasible in some cases (ie. clock generation, sync issues).
The 8051 ports are rather primitive compared to contemporary MCUs. The 'port' has a latch that stores the pin state and that latch tries to condition the output. This does not necessarily mean that the port is at desired state. For instance if it is driven low externally the latch may be high but the port will be low. This is exactly how we set a port to be an input.
When we MOVe data to a port, we write to the latch, when we MOVe from the port into a register, we read the actual state regardless of the state of the latch. Read-modify-write operations are an exception to this, as they specifically read the latch, perform desired operation and write back to the latch. Therefore the scenario given under "Is this correct?:" is the correct one.
If instead we were to MOV A, P2 and then execute anl P2,#F8h "Or is this more correct?:" would be correct.
As a result we can use logic operations to modify ports for the cases given in this question.

Bitcoin Blockchain - Verification process

As the title states, basically my question is about the blockchain verification. I know whats a block chain and basically understood how mining is working, except once simple thing.
Let's say we have 2 guys, Bob and Adam.
Blockchain:
|1|-|2|-|3|-{4} - Bob Chain
|1|-|2|-|3|-{4} - Adam Chain
Assume both Bob and Adam found a new block, but it will not be verified until someone finds a next block. So my questions is whats is happening in a situation if Adam will find a block |5| first. Will Bob get his reward for finding a block? Or it means if Adam found one block, he has to find the next one which is extremely difficult without a huge network of computational resources in order to verify his previous block |4| and get reward for block 4 of 12.5 bitcoins, because the nodes will only accept the longest blockchain? I hope I clearly illustrated the picture. I've tried to find the answer in different videos and materials but somehow this aspect was put aside. If my assumption is true, it means there is no way how can a single person to earn anything from mining without a huge network ?
First of all, in Bitcoin when someone creates a block, he broadcasta it to the rest ot the network. As you said, if there are two people that create the block at the same time, they will broadcast it. So, you will get two blocks at the same time. Although you save both blocks, you will try to mine one of them. After some time, one of the two branches will be longer, so you will delete the second one.
The miners of the Blockchain will create some blocks and a branch will be longer after some time.
In Blockchain, a block is considered well when it has 100 blocks (I d0n't know exactly how many) above of it. So, the reward is taken after 100 blocks, not before.
Who out of Adam or Bob gets the reward depends on whose block remains as part of the 'Best chain' eventually. This in turn partly depends on consensus rules and partly on how the things transpired. This is explained as follows
Adam and Bob claimed that they found the block first by broadcasting to peers almost at same time.
Let's presume that a peer named 'ITWala' saw these 2 blocks which would be at same height. Assuming Adam's block reached ITWala's node first. So this would lead to something known as 'forking' in block chain terminology and is quite normal to occur.
**Chain status on ITWala's node leading to forking **
Block1 --> Block 2 --> Block 3 --> Block 4 (Adam's Block)
|
|--- Block 4 (Bob's Block)
One of following can happen:
Case 1 - For sake of simplicity, assume that Bob is the only one making claim for block 5. Now 'ITWala' receives block 5. He tries to make the chain longer by trying to fit at one end of the fork created from Block 4 from Adam. It does not fit because the hash of the previous block would not match.
Result
Fork at the end of Adam's block is discarded. Fork with Bob's block becomes active chain and thus Bob becomes the winner of 4 as well as 5 for award.
Case 2:
Block 5 is created by 'ITWala' or some peer of ITWala who synced up with copy on ITWala's node.
Result: In this case, ITWala would use Adam's block to activate the best chain as it arrived first making him the winner for block 4. Block 5 is awarded to ITWala
There can be more combinations. However point here is that the block that remains in best chain wins the award.

Is this TLA+ specification correct?

So far, i have done a little bit of code for my project, but don't know whether its true or false. Can u guys see my code?? First at all, i should post the requirement for better understanding..
In computer science, mutual exclusion refers to the requirement of ensuring that no two processes or threads are in their critical section at the same time. A critical section refers to a period of time when the process accesses a shared resource, such as shared memory. The problem of mutual exclusion was first identified and solved by Edsger W. Dijkstra in 1965 in his paper titled: Solution of a problem in concurrent programming control.
For visual description of a problem see Figure. In the linked list the removal of a node is done by changing the “next” pointer of the preceding node to point to the subsequent node (e.g., if node i is being removed then the “next” pointer of node i-1 will be changed to point to node i+1). In an execution where such a linked list is being shared between multiple processes, two processes may attempt to remove two different nodes simultaneously resulting in the following problem: let nodes i and i+1 be the nodes to be removed; furthermore, let neither of them be the head nor the tail; the next pointer of node i-1 will be changed to point to node i+1 and the next pointer of node i will be changed to point to node i+2. Although both removal operations complete successfully, node i+1 remains in the list since i-1 was made to point to i+1 skipping node i (which was the node that reflected the removal of i+1 by having it's next pointer set to i+2). This problem can be avoided using mutual exclusion to ensure that simultaneous updates to the same part of the list cannot occur.
This is my code :
EXTENDS Naturals
CONSTANT Unlocked, Locked
VARIABLE P1,P2
TypeInvariant == /\ P1 \in {Unlocked,Locked}
/\ P2 \in {Unlocked,Locked}
Init == /\ TypeInvariant
/\ P1 = Unlocked
Remove1 == P2' = Locked
Remove2 == P1' = Locked
Next == Remove1 \/ Remove2
Spec == Init /\ [][Next]_<<P1,P2>>
THEOREM Spec => []TypeInvariant
First, what are you trying to model? It seems the only thing you are interested in proving as a theorem is the type invariant. I would have thought you want to prove something about mutual exclusion and not just the possible values of P1 and P2. For example, do you want to prove that P1 and P2 are never both locked at the same time?
MutualExclusion == ~(P1 = Locked /\ P2 = Locked)
THEOREM MutualExclusionTheorem == Spec => []MutualExclusion
Also, I wouldn't put TypeInvariant in the definition of Init. Your Init should contain the starting value(s) of your variables P1 and P2. Then you have a theorem,
THEOREM InitTypeInvariant == Init => TypeInvariant
and prove it. This theorem will be used in the proof of the theorem Spec.

State based testing(state charts) & transition sequences

I am really stuck with some state based testing concepts...
I am trying to calculate some checking sequences that will cover all transitions from each state and i have the answers but i dont understand them:
alt text http://www.gam3r.co.uk/1m.jpg
Now the answers i have are :
alt text http://www.gam3r.co.uk/2m.jpg
I dont understand it at all. For example say we want to check transition a/x from s1, wouldnt we do ab only? As we are already in s1, we do a/x to test the transition to s2, then b to check we are in the previous right state(s1)? I cant understand why it is aba or even bb for s1...
Can anyone talk me through it?
Thanks
There are 2 events available in each of 4 states, giving 8 transitions, which the author has decided to test in 8 separate test sequences. Each sequence (except the S1 sequences - apparently the machine's initial state is S1) needs to drive the machine to the target state and then perform either event a or event b.
The sequences he has chosen are sufficient, in that each transition is covered. However, they are not unique, and - as you have observed - not minimal.
A more obvious choice would be:
a b
ab aa
aaa aab
ba bb
I don't understand the author's purpose in adding superfluous transitions at the end of each sequence. The system is a Mealy machine - the behaviour of the machine is uniquely determined by the current state and event. There is no memory of the path leading to the current state; therefore the author's extra transitions give no additional coverage and serve only to confuse.
You are also correct that you could cover the all transitions with a shorter set of paths through the graph. However, I would be disinclined to do that. Clarity is more important than optimization for test code.