Explanation of Code

This is a discussion on Explanation of Code within the C Programming forums, part of the General Programming Boards category; I am having trouble seeing what this code is doing. I keep getting flag[i] as the only on going into ...

  1. #1
    Registered User
    Join Date
    Nov 2006

    Question Explanation of Code

    I am having trouble seeing what this code is doing. I keep getting flag[i] as the only on going into the critical section, which obviously means I am missing something! This isnt homework and I didn't write this code, we are discussing it in class, but I need a better understanding of what is going on! Please explain! Thanks!

    So all elements of flag are initially idle, and inital value of turn is between 0 and n-1 (this is what is throwing me off I think.) The processes share:

    enum pstate {idle, want_int, in_cs};
    pstate flag[n];
    int turn;

    do {
             flag[i] - want_in;
             j = turn;
            while( j != i){
                    if (flag[j] != idle)
                         j = turn;
                         j = (j + 1) % n;
            flag[i] = in_cs;
            j = 0;
            while ((j<n) && (j == i || flag[j] != in_cs))
             if ((j >=n) && (turn == i || flag[turn] == idle))
    //critical section
    j = (turn + 1) % n;
    while (flag[j] == idle)
           j = (j + 1) % n;
    turn = j;
    flag[i]= idle;
    //remainder section
    } while (TRUE)

  2. #2
    Join Date
    Dec 2007
    In your nightmares
    Is this all the code, you are missing some stuff. Also the last line ends the do and should just contain the '}'. Where is turn assigned a value? j = turn; just reserves a space in memory and does not guarantee an initial value of 0. try declaring turn similar to the following:
    int turn = 0;
    Also, what parts do you understand? mabye a debugger or deskchecking it might help...
    long time no C; //seige
    You miss 100% of the people you don't C;
    if (language != LANG_C && language != LANG_CPP)

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
             flag[i] - want_in;
    This is probably a typo: It doesn't do anything [well, in theory, it calculates flag[i] - want_in and then throws away the result - but that is "doing nothing"].

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 08:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  3. Code Explanation
    By dmkanz07 in forum C Programming
    Replies: 1
    Last Post: 03-27-2007, 09:24 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21