MAGICJAR - EDITORIAL

I lovedddd this question a lot!!!

I cannot believe this…a few days before my operating systems sir teach me deadlock and oh great god i applied it to the problem and got an 100% points!!!

the editorial is as usual lovely <3 and this line " Now, to break this deadlock, we need one more jar." is pure love <3 :smiley:

thank you for your wonderful editorials @taran_1407 !! also, can you please tell when guessrt editorial will come? I try so hard but cannot do it :frowning:

Scheduler will not give less resources when there are enough for one process demand
Regards
Samuel

I don’t understand this task at all.

I mean I understand the solution, I just don’t understand why that solution is the way it is.

Like, what dictates how many jars that the chefs need ?

I just don’t get it and I’m really, really frustrated that I don’t get it.

“The junior chefs take some jars; let’s denote the number of jars taken by the i-th chef by ai. Any distribution of jars such that 0≤ai for each valid i and ∑Ni=1ai=J is possible.
At any time, if ai<Ai for each chef i that has not prepared their dish yet, the cooking session is a failure.
Otherwise, one of the chefs who have at least as many jars as the number of required ingredients prepares their dish and returns their jars to the kitchen.
Whenever some jars are returned to the kitchen, they are immediately taken by some chefs that have not prepared their dishes yet (possibly all the jars by one chef).
This process continues with chefs taking jars, cooking their dishes and returning jars, until no chef can cook their dish anymore or all chefs have cooked their dishes.”

I don’t get this part at all ,in any single possible way, how am I supposed to conclude how many jars are the minimum ??

The test cases don’t provide that, from a blind perspective, you need as many jars as the guy that requires the most jars, I don’t get how, let’s say 10 chefs need 28 jars, but 19 is somehow the solution, when if we follow the logic of “Chefs will return their jars when they finish” then the answer should be the amount of jars that the most demanding chef needs, for example if my most demanding chef needs 8 jars, then the answer should be 8, but it’s not… Why ?
If all the chefs return their jars, then if 10 chefs needs jars in this order : 2,4,6,8,2,2,1,1,1,1

Then the answer through some stupid logic should be 8, since all of the chefs will just return the jars when they finish, and the most demanding chef needs 8 of them, but the answer here is 19. Why would any chef need 19 jars ?

Also your solution links don’t work so I can’t even solve what’s wrong with my problem.

2 Likes

Scofield11
I exactly did the same thing as you did, i.e just gave the max number of jars as the answer which is wrong and I’m very much frustrated as you did and you did a great job in asking your view…Thanks for that.

VIJJU123
But after reading your solution i found my mistake, “the chefs are not willing to share their jars among themselves” so if there are just the number of jars needed by the chef with most demand that now doesn’t make sense… so just like no process can proceed in the OS ,no chef can cook their dish and this brings to the worst case where at least one of the chef could complete their tasks which is just one more than sum(ai)-N.
THANK you very much sir.

Hello VIJJU123 sir ,may i know the book name where the scheduler allaocates the less resources when there are enough resources,please help me i want to learn

Hello VIJJU123 sir ,thanks for your guidence, Bankers is for Dead Lock Avoidance,in our problem we are not considering ingrediants(sugar,salt…) so NO resource type.If i take the MAX value in the set of request ,that satisfies allocation condition for the First request,in any order
example:-2,4,6,8,2,2,1,1,1,1 ;MAX=8

Sir then this is least upper bound sir,why they ave asked minumum number of jars

This problem was a test of english understanding skills rather than coding

1 Like

Please confirm my understanding :
The distribution of jars is random, like, we can’t guarantee that who will get how much, so we choose a minimum number such that whatsoever the distribution, the session ends successfully.
The max logic is faulty as it will only work if chefs cooperate among themselves, or the maxm demanding chef can obtain all those jars for himself which is not guaranteed.

2 Likes

at any time, if ai < Ai for “each i” =>> that is at any time if all of the cooks have less than required resources then cooking session will fail…check the editorial again, the current solution will make sure that even in the worst case at least one cook have the required resources always to complete its cooking.

Consider the following test case: 1 4
If the second chef takes all 4 jars, cooks his dish, keeps them back and then the first chef takes only one of these, does this not violate the condition that ∑ai=J.

You might want to search for the concept of deadlock. Deadlock concept is frequently discussed in OS course. The basic funda is that, say each of N checks need A_i jars. Now, if we have \sum (A_i-1) jars, then there exists a configuration (or deadlock) where none of the chefs can proceed. However, if we add 1 more jar, we can prove that at least 1 of the chef will have his requirement fulfilled and will proceed (Hint: (A_i-1)!!). This frees more jars for other Chefs to use and hence is the minimum number of jars to guarantee avoiding a deadlock.

Why would any chef need 19 jars ?

The basic idea is, “If there exists a configuration where NO CHEF can proceed, then you need to increase jars.”

With 8 jars, there is such a deadlock.

2 Likes

Sum(ai) == J is only valid for initial distribution. In the given example req: 1 4 and the initial distribution will be 0 4 which is equal to J(4).

I think you should give Banker’s algorithm a read. This is frequently asked in exams like GATE etc.

"the chefs are not willing to share their jars among themselves

Take it as, as long as the process is running (i.e. Chef is cooking), he will not give his jar to some other Chef. Once he is done and jars are returned to common shelf, other Chefs can take it.

 just one more than sum(ai)-N

I also said that this is the answer. Read my first comment from second line.

Read this of the problem statement-

the kitchen initially so that the session would be successful regardless of how the junior chefs pick up the jars.

If you take just max number of jars, there will exist a configuration where a deadlock happens. In fact, such a question has also came in GATE exam, where it said that 3 processes required 4,5,4 tapes respectively - then what is the minimum number of tapes needed to avoid deadlock regardless of distribution of resources.

Ans was (4-1)+(5-1)+(4-1)+1=11 with same logic as in editorial.

1 Like

Hence, the basic concept is that, there exists a worst case configuration where each chef takes A_i-1 jars (where he needs A_i jars to complete cooking) and now a deadlock happens. However, if we add 1 more jar, then at least one of the Chef can now proceed with cooking, finish it, and thus freeing more jars for other Chefs to use. Hence \sum(A_i-1)+1 is the minimum jars needed so that deadlock never happens no matter how the jars are picked by the junior Chefs.

1 Like

Have you even read the question carefully?

the kitchen initially so that the session would be successful regardless of how the junior chefs pick up the jars.

MINIMUM jars such that all chefs complete cooking REGARDLESS of how they pick jars, which is equal to the least upper bound.

1 Like

Yes… Perfect.