Jump to content
Urch Forums

Sync. problem


AlbaLed

Recommended Posts

The basic structure of a processes with a critical section is shown below

while (TRUE)
{
enter_cs();
critical section
leave_cs();
rest of loop
}

Which of the following definitions of enter_cs and leave_cs provide a
solution to the mutual exclusion problem for two processes, P0 and P1?

[color=Red]Case 1.[/color]

int	turn = 0;

[color=brown]enter_cs()[/color]
{
while (turn == procno)
	;
}

[color=Blue]leave_cs()[/color]
{
turn = procno;
}

[color=Red]Case 2.[/color]

bool	in_crit_sect[2] = {FALSE, FALSE};

[color=brown]enter_cs()[/color]
{
while (in_crit_sect[otherprocno])
	;
in_crit_sect[procno] = TRUE;
}

[color=Blue]leave_cs()[/color]
{
in_crit_sect[procno] = FALSE;
}

[color=Red]Case 3.[/color]

bool	want_crit_sect[2] = {FALSE, FALSE};

[color=brown]enter_cs()[/color]
{
want_crit_sect[procno] = TRUE;
while (want_crit_sect[otherprocno])
	;
}

[color=Blue]leave_cs()[/color]
{
want_crit_sect[procno] = FALSE;
}

[color=Red]Case 4.[/color]

bool	want_crit_sect[2] = {FALSE, FALSE};
int	turn = 0;

[color=brown]enter_cs()[/color]
{
want_crit_sect[procno] = TRUE;
turn = procno;
while (want_crit_sect[otherprocno] && turn == procno)
	;
}

[color=Blue]leave_cs()[/color]
{
want_crit_sect[procno] = FALSE;
}

 

 

 

2. What are the relative advantages and disadvantages of using

semaphores and monitors for implementing process synchronization?

 

 

Taken from links in "sticky" thead

Link to comment
Share on other sites

I am not sure how to put it in terms of properties. I basically visualize a small test case for each one of them and bingo!

 

Let me try anyway. Basically, you need two things for mutual exclusion of two processes.

 

(1) One process must initialize its intent of getting into the CS.

 

(2) One process must get into the CS just if it is its turn to do so.

 

(1) is needed to "queue" the processes willing to get into the CS. (2) is needed to actually decide which one should go next.

 

If you implement just (2)-Case 1 or (1)-Case 3, you might easily get with two processes inside your CS.

 

Case 4: even if two processes get into enter_cs() at the same time, they will both set their intent to true and the value of turn set by one process will be overwritten by the other process. The turn value will then be used to determine which process get into the CS first.

 

Case 4 is basically Peterson's algorithm, right?

 

Wood

Link to comment
Share on other sites

Don't know the algorithm by its name but I've seen it everywhere.

A mutual exclusion mechanism should guarantee the following

 

1. Correctnes (duhh, only 1 process should be in the crit. sect. at one time)

2. Bounded waiting, no process should wait forever to enter its critical section (starvation)

3. Progress, if no process is in the CS and other processes want to enter their CS then one of them should succseed in entering its CS (no deadlocks can occur)

 

This can be used as a guide to test and algo., you can stop one process inbetween any two instruction and start the other where you left off (if running on a single cpu), and check whether any of the above is broken. Try it with other cases and you'll see what I mean.

Even-eventhough informal your description made some points, but I think you forgot the deadlock part.

Link to comment
Share on other sites

Here is what I found out

 

3 methods

 

1. Centralized mutex

one system acts as the "mutex master"(mm). A process cannot enter its CS unless it receives approval from mm, mm proceses requests using fcfs for example.

 

2. Distributed mutex

If one proc. want to enter CS, it sends out a timestamped message containing, pID and CS name. It enters CS upon approval from all other systems. Upon receiving a request, a system sends out an approval note iff it is not waiting to enter that particular CS or if the timestamp of the request is lower than its request for this CS. Otherwise it does send out a permission denied note. Timers can be used to determine if a node is down, if nothing is heard from it.

 

3. Token passing

Wait untill token comes than enter CS, when done release token

 

am I in stratosphere?

Link to comment
Share on other sites

hmmm well if you guys have galvin, thn algorithm 3 under "2 process solutions" in process synchronisation chapter looks like this algo apart frm difference at 2 places. heres the algo:

 

flag[0]=flag[1]=false

turn=0..1

 

now your algo has procno nd otherprocno (i,j)

 

flag=true;

turn=j; ---- heres one difference

while(flag[j] and trun=j) do noop; ---- heres another one

critical section

flag=false

remainder section

 

 

Link to comment
Share on other sites

Most likely!! :D

 

Anyway... Great job AlbaLed in finding these topics!! You've certainly implemented a hash table in your brain... in less than seconds you can find any answer! ;)

 

I was looking for something a little simpler such as: "semaphores and monitors cannot be used in distributed systems since their basic logic requires that the processes/threads share a common memory. Therefore, to implement the concept of mutual exclusion in distributed systems, some mechanism utilizing message passing is needed (all your three cases are specific techniques using message passing)."

 

Wood

Link to comment
Share on other sites

now your algo has procno nd otherprocno (i,j)

 

flag=true;

turn=j; ---- heres one difference

while(flag[j] and trun=j) do noop; ---- heres another one

critical section

flag=false

remainder section

 

Vinay, there's no difference between the two algos. The only difference is on the value of turn and its check. The idea is to set the value of turn to something and then check for that same value to see if nobody changed since you last set. If you set it to i, j, k, l, m, n, o, or p, it doesn't matter right? As long as you check for the same value on the while loop.

 

Wood

Link to comment
Share on other sites

wood message passing can be used even on uniproc, machines to ensure mutex, but not efficiently, on my OS class I had a hwk assingment to do just that, on xinu tho :), Vinay, get some rest before you hit this stuff again an again, with that rate you've got another 160 hrs to go, lol
Link to comment
Share on other sites

u mean

while (want_crit_sect[otherprocno] && turn == procno) ;

and

while(flag[j] and trun=j) do noop;

 

are same when procno is standing for I and otherprocno for j

 

if you substitute thm in first you get

while (want_crit_sect[j] && turn == i) ;

 

may be I m wrong but I also tried executing tht code.

if both p0 and p1 set thr flags true and thr turn to their ids thn both will not run.

Link to comment
Share on other sites

flag=true;

turn=j; ---- heres one difference

while(flag[j] and trun=j) do noop; ---- heres another one

critical section

flag=false

remainder section

 

Vinay this code is the same as the case 4, the only difference is that each process is saying it's the others turn. Processes cannot be deadlocked because turn can only have one value. The trick in the above code is that for process 1 i=0, j =1, and for process 2 i=1, j=0. think of j=other, i=myself.

Keep in mind that turn and the flag array are global vars.

 

The coding of case 4 is some what strange, maybe the guy who wrote it wanted to make it fairer or harder for his/her students. One process does not enter CS if the other process has already made a request, but if the other process set the turn variable after this process set it i.e. in current process turn=otherprocno, then this process will enter its CS. In general if a tie occurs turn is used to break the tie, because it can have only one value.

 

Does that help!?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...