Number of processes - process

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.

Related

CGAL:how to divide a segment into many little segments and output the points into a file

I have a line segment defined by two points P1 and P2.
I now need to divide its into many parts. For example, 50 Parts.
and output the points into a file from P1 to P2.
Honestly, it makes no sense to use CGAL, if that is really all what you want.
It's just: P1 + i * (P2 - P1)/50, for i=0..50

Banker's algorithm solution for given system state

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

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.

Process creation tree for this code?

What's the process creation tree for this code (assuming all forks succeed)?
if(fork())
fork();
n = 3;
for(i=1;i<n;++i)
{
if(pid = fork())
break;
}
This is what I tried:
[0]
|
/\
[1] [1]
| |
[2] [2]
| |
[3] [3]
|
[4]
But that wasn't even one of the choices! Any help is appreciated.
First line:
if (fork())
After this fork, there are two processes, the parent and the child. In the parent, fork() returns nonzero. In the child, fork() returns zero. This means that the next line will run only in the parent.
fork();
The parent forks again. Now there are three processes, and all three will run the rest of the code.
for(i=1;i<n;++i)
{
if(pid = fork())
break;
}
First, i=1. Each of the three processes creates a child, sees a nonzero result, and breaks out of the loop. So the original three processes are done. But the children will keep going.
Next time through the loop, i=2. The same thing happens. Each of the three child process forks off a child of its own, sees a nonzero result, and breaks. The three new children will keep going.
And so on up to i=n-1.
I'll spare you my attempts at ASCII art, but the resulting tree looks a bit like a certain familiar piece of silverware...

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.