0

I came across a code with following condition

(number[i],i)<(number[j],j)

Can someone tell what does this condition mean or checks?

i,j are variables and number is array

Here is the whole code-

do{
    choosing[i]=true;
    number[i]=max(mumber[0],number[1],...,number[n-1])+1;
    choosing[i]=false;
    for(j=0;j<n;j++){
    while(choosing[j]);
    while((number[j]!=0)&&(number[j],j)<(number[i],i));}

critical section

number[i]=0;}while(1);

It was a critical section problem in multiple processes. Initialisations-

bool choosing[n];
int number[n];

This is what the book wrote about the condition-

monster
  • 808
  • 10
  • 23
  • 4
    Read about the *comma* operator. Though it's not clear to me what motivated the author to write this code. – Eugene Sh. Mar 02 '17 at 16:33
  • 1
    It's equivalent to `i < j`. I can't think of any good reason to have the array references in there, except to obfuscate the code. – Barmar Mar 02 '17 at 16:34
  • What kind of code was that? – Jabberwocky Mar 02 '17 at 16:35
  • It was a multiple process solution code – monster Mar 02 '17 at 16:36
  • 1
    Although this question is technically answered by explaining the comma operator, I doubt that will satisfy the OP, since he will still be wondering what the point of this code is (as we all are). – Benjamin Lindley Mar 02 '17 at 16:36
  • If it is C++ code, then it is possible that the class of `number` overrides the `[]` operator. In that event, the `number[i]` and `number[j]` could have a *bona fide* useful purpose, for some value of "useful". – John Bollinger Mar 02 '17 at 16:38
  • For that matter, I guess the comma operator could be overridden, too. (snarky comment suppressed.) – John Bollinger Mar 02 '17 at 16:43
  • It could be an odd attempt at bounds checking i and j by trying to force a bad-access if either is greater than the length of number – Alex Zywicki Mar 02 '17 at 16:43
  • @monster could you post the relevant parts of the code, that is the code around `(number[i],i)<(number[j],j)` and the declaration of `number`. And please tell us if it's C or C++. The answer you will possibly get depends on that. – Jabberwocky Mar 02 '17 at 16:43
  • @monster quoting you: _i and j are variables and number is array_ : variables of what type, and an array of what? (yes, the answer may also depend on this). – Jabberwocky Mar 02 '17 at 16:45
  • Yep.. given the update this code makes a little sense. – Eugene Sh. Mar 02 '17 at 16:54
  • BTW, this looks like a part of some multithreaded or otherwise concurrent code (given it is polling some seemingly non-changing variable `choosing[j]`. – Eugene Sh. Mar 02 '17 at 17:00
  • @EugeneSh. still not sure about the actual purpose of `(number[i],i)` . – Jabberwocky Mar 02 '17 at 17:03
  • @MichaelWalz Looks to me like some dirty WA for some obscure problem like caching and/or optimization, forcing the compiler to spit out some "working" sequence. – Eugene Sh. Mar 02 '17 at 17:15
  • @monster what kind of software is this running on what kind of hardware? – Jabberwocky Mar 02 '17 at 17:18
  • It was actually in my book of Operating System Concepts i have attached the page writing about the condition in question – monster Mar 02 '17 at 17:28
  • @monster _i have attached the page writing about the condition in question_: no you haven't, check your question. – Jabberwocky Mar 03 '17 at 07:49

0 Answers0