azuki ooh

Greenhorn
+ Follow
since Nov 07, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by azuki ooh

This is one of the codes I got from my textbook. Can someone help me to understand why is the sem.acquire() and sem.release() necessary when we can keep track of which item is used or unused simply using the getAvailableItem() and markAsUnused()?



>
Suppose the transactions above were executed using two-phase locking and have reached the following deadlock state:
1 Transaction T1: start
2 Transaction T2: start
3 Transaction T3: start
4 Transaction T4: start
5 Transaction T1: read(A)
6 Transaction T1: read(B)
7 Transaction T1: read(C)
8 Transaction T1: read(D)
9 Transaction T2: read(E)
10 Transaction T2: write(E)
11 Transaction T2: read(A)
12 Transaction T3: read(F)
13 Transaction T3: write(F)
14 TransactionT3: read(B)
15 Transaction T4: read(D)

(a) T2
(b) T3
(c) T1
(d) T4

I'm thinking it should be T1. Because at Line 9, T2 waits E as it has been locked by T1.
(a) temp = temp + 1;
(b) X = temp;
(c) int temp = X;

I read somewhere that assignment operation are atomic. Incremental operation is not. So I conclude that (c) is atomic. Am I right?
If two threads execute the below method increment() concurrently, how many different final values of X are there? You may assume that initially X has value 0.

void increment()
{
int temp = x;
temp = temp + 1;
x = temp;
}

I think it's 2. Can someone please help to verify?

Thanks!
What is one advantage of using monitors over using semaphores?

(a) Mutual exclusion in monitor is automatic while mutual exclusion in semaphore needs to be coded explicitly.
(b) Shared variables are global to all processes in the monitor while shared variables are hidden in semaphores.
(c) Synchronisation in monitors is provided by condition variables while there are no condition variables in semaphores.
(d) Access to the monitor is through protected shared variables while access to shared variables in semaphores is not protected.
10 years ago
Bill is swiping his credit card in the supermarket. His wife is paying a bill online at home.
Bill sees this on the screen:
Your old balance is: $2000
Your debit is: $100
Your new balance is: $1850

His wife sees this on the screen:
Your old balance is: $2000
Your debit is: $50
Your new balance is: $1950

Which one of the following could have occurred?

(a) race condition
(b) condition synchronisation
(c) starvation
(d) mutual exclusion

I think this is a race condition. Am I right?
The following program (ReaderOrWriter class) that implements the readers-writers problem with semaphores.

The main program (not shown) creates many reader and writer threads, starts each of them, and then waits forever for them to terminate by calling each thread’s join() method.



What values should each of the semaphores be initialized to?
s: ??
okToRead: ??
okToWriteL ??

How can I tell what is the semaphore value just by looking at the codes?

Assume a reader is currently using the buffer. If there are both waiting writers and waiting readers, will a writer or a reader get access to the buffer first?

Is it possible for readers to starve with this code?

Can the semaphore okToRead have a value greater than 1?

Can the semaphore okToWrite have a value greater than 1?


Manage to solve this. Posting this to share with those who are reading this forum to learn something.















>
Why does my method getAServing() gets interrupted even though I already have synchronized keyword?







My output:

Cook has put 1 missionaries into pot
there are 1 in pot now.
Savage 3 getting a serving to eat.
There are no more missionaries in the pot!
There are no more missionaries in the pot!
There are no more missionaries in the pot!
There are no more missionaries in the pot!
There are no more missionaries in the pot!
There are no more missionaries in the pot!
There are no more missionaries in the pot!
There are no more missionaries in the pot!
There are no more missionaries in the pot!
Savage 3 finished eating.
Cook has put 1 missionaries into pot
there are 1 in pot now.
Savage 1 getting a serving to eat.
Cook has put 1 missionaries into pot
there are 1 in pot now.
Savage 7 getting a serving to eat.
Cook has put 1 missionaries into pot
there are 1 in pot now.
Savage 9 getting a serving to eat.
Cook has put 1 missionaries into pot
there are 1 in pot now.
Savage 5 getting a serving to eat.
Savage 7 finished eating.
Savage 9 finished eating.
Savage 1 finished eating.
Savage 5 finished eating.
Cook has put 2 missionaries into pot
there are 2 in pot now.
Savage 2 getting a serving to eat.
Savage 2 finished eating.
Cook has put 3 missionaries into pot
there are 3 in pot now.
Savage 4 getting a serving to eat.
Savage 4 finished eating.
Cook has put 4 missionaries into pot
there are 4 in pot now.
Savage 10 getting a serving to eat.
Savage 10 finished eating.
Cook has put 5 missionaries into pot
there are 5 in pot now.
Savage 8 getting a serving to eat.
Cook has put 5 missionaries into pot
there are 5 in pot now.
Savage 6 getting a serving to eat.
Savage 8 finished eating.
Savage 6 finished eating.
There are 4 missionaries left in the pot.




I thought when a Savage thread run, it calls the getAServing. when it finds that numOfServings[0]==0, it should complete
numOfServings[0]+=c.putServingsInPot(); goes on the next line until the method is complete with numOfServings[0]--; All these is in a synchronized method.
But my output seems like before the assignment to numOfServings[0], the next thread run n check that numOfServings[0]==0 and print "There are no more missionaries in the pot!"

But I have already used synchronized.

The correct output supposed to look like this:
Cook has put 5 missionaries into pot
Savage 0 getting a serving to eat
Savage 3 getting a serving to eat
Savage 5 getting a serving to eat
Savage 1 getting a serving to eat
Savage 9 getting a serving to eat
There are no more missionaries in the pot!
Cook has put 2 missionaries into pot
Savage 5 finished eating
Savage 3 finished eating
Savage 0 finished eating
Savage 1 finished eating
Savage 9 finished eating
Savage 7 getting a serving to eat
Savage 8 getting a serving to eat
There are no more missionaries in the pot!
Cook has put 5 missionaries into pot
Savage 8 finished eating
Savage 7 finished eating
Savage 6 getting a serving to eat
Savage 4 getting a serving to eat
Savage 2 getting a serving to eat
Savage 4 finished eating
Savage 6 finished eating
Savage 2 finished eating
There are 2 missionaries left in the pot








>
I have been trying to walk through the execution, but I might have understand it in a wrong direction.
Here's my understanding:

In main thread:
Create a cook object
Create a numOfServing to hold an integer
Create 10 Savage object
Start running the Savage
Savage object will run 2 times.
In each run, it check the value in numOfServing.
If it's 0, then it call the putServingsInPot() in cook, then it goes into wait.
so processing is now at putServingsInPot(). And the end of this method, it calls the notifyAll to wake up the thread that's waiting.
so Savage thread continue, it checks and find that numOfServing is now >0, so it process the part after the while loop.

This sounds correct to me. But the output is wrong. Obviously my understanding is wrong. But what is wrong? Can someone tell me please?

I think I need a join() in the main to make sure the all other thread ends before the main thread. But the program just got "hang", the threads simple wait and nothing happen. That is why I comment out the join part in the main().
I was hinted by lecturer that the Cook is not a thread - it is a shared resource since any of the savages can wake up the cook. The cook also does not need to know how many servings are in the pot, since its responsibility is just to cook.

Hence, I have modified my program as such:








When I run this, the debugger tells me that my main thread finished and there are still some other threads waiting. I don't understand the flow of process. Can someone help me understand the flow of process here and why there are still threads waiting?
>
Thanks Anayonkar and Manish for your comments. I am using int[] instead of int for numOfServings because I need to pass as reference. If I use int I would be passing as value. Yes, I am hoping to learn as much as I can from this forum. I am not expect ready answer as that will not help me to enhanced my skill. Yes, thanks for pointing out the missing join(). I need to wait for all the Threads to finish before printing the ending. I am also not sure if I need a Cook class. Looking at the question, it seems like only Savage run concurrently. Method in cook is only invoked when numOfServing = 0 and during this, Savage is at blocked state. So does this mean Cook can just be a normal class, no run() method needed.

I was also thinking if I should create another class, say, Pot. And have getServingFromPot() and putServingToPot() in this class. But according to the question, putServingsInPot() is suppose to be in Cook class.
I am suppose to write a program to simulate this situation.

A tribe of savages eats communal dinners from a large pot that can hold M servings of stewed missionaries. When a savage wants to eat, he helps himself from the pot, unless it is empty. If the pot is empty, the savage wakes up the cook and then waits until the cook has refilled the pot.

Admin class: This class creates the cook and generates arrival of 10 savages. The class also keeps track of the number of servings in the pot (using an array of size 1) and prints the number of servings left in the pot after all the savages have eaten.

Cook class: This class models the cooking done by the cook.
It should have a method putServingsInPot() that performs the following:

  • generate a random number M (between 1 and 5 inclusive)
  • show a message the cook has put M missionaries into the pot.
  • include a timing delay to represent the cooking relative to M.



  • This method is invoked only when the pot is empty.

    Savage class: This class models the behaviour of the savages.
    It should have a suitable method that performs the following:
    get a serving from the pot

  • include a timing delay to represent the time spent eating
  • deduct one serving from the pot
  • show messages that the savage starts eating and has finished eating



  • When getting a serving from the pot, if the pot is empty, the cook’s method putServingsInPot() needs to be invoked.

    These are my codes but the output is totally wrong. But I just can't understand where goes wrong Please help!










    The output suppose to be like this:

    Cook has put 5 missionaries into pot
    Savage 0 getting a serving to eat
    Savage 3 getting a serving to eat
    Savage 5 getting a serving to eat
    Savage 1 getting a serving to eat
    Savage 9 getting a serving to eat
    There are no more missionaries in the pot!
    Cook has put 2 missionaries into pot
    Savage 5 finished eating
    Savage 3 finished eating
    Savage 0 finished eating
    Savage 1 finished eating
    Savage 9 finished eating
    Savage 7 getting a serving to eat
    Savage 8 getting a serving to eat
    There are no more missionaries in the pot!
    Cook has put 5 missionaries into pot
    Savage 8 finished eating
    Savage 7 finished eating
    Savage 6 getting a serving to eat
    Savage 4 getting a serving to eat
    Savage 2 getting a serving to eat
    Savage 4 finished eating
    Savage 6 finished eating
    Savage 2 finished eating
    There are 2 missionaries in the pot
    >

    But mine is like this:

    ending...There are 0 missionaries left in the pot
    Cook has put 4 missionaries into pot.
    Cook has put 2 missionaries into pot.
    Cook has put 2 missionaries into pot.
    Cook has put 2 missionaries into pot.
    Cook has put 5 missionaries into pot.
    Cook has put 4 missionaries into pot.
    >