Find the answer to your Linux question:
Results 1 to 3 of 3
i am trying to understand how i can lock processes to let other processes finish their job. this must be in the C language but i have no clue as ...
  1. #1
    Just Joined!
    Join Date
    Sep 2008
    Posts
    11

    need help with locking processes

    i am trying to understand how i can lock processes to let other processes finish their job. this must be in the C language but i have no clue as to where i must start and no clue as to what must be added like the shell script..can anyone help me please. my first assignment is to write a program that implements locking,and the 2nd is the one that does not implement locking.help

  2. #2
    Just Joined!
    Join Date
    Sep 2008
    Posts
    11

    still lost

    Is my question to ambigouis?im trying to use petersons algorithm to implement spin locking.

    #define FALSE 0
    #define TRUE 1
    #define N 2 /* number of processes */

    int turn; /* whose turn is it? */
    int interested[N]; /* all values initially 0 (FALSE)*/
    void enter_region(int process) /* process is 0 or 1 */
    {
    int other; /* number of the other process */
    other = 1 - process; /* the opposite of process */
    interested[process] = TRUE; /* show that you are interested */
    turn = process; /* set flag */
    while (turn == process && interested[other] == TRUE) /* null statement */;
    }
    void leave_region(int process) /* process: who is leaving */
    {
    interested[process] = FALSE; /* indicate departure from critical region */
    }

    not sure how to use it properly or implement into a actual working code for my project.

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    One way is with semaphores. Do this at the command line:
    Code:
    man semget
    man semop
    man semctl
    But I don't like this mechanism, because there is a limit to the number of semaphores that can exist in the system at one time, so if your program dies before you can destroy a semaphore, you begin to have "semaphore leak".

    A better way is to create a file for just the purpose of locking, and use advisory record locking (even though you're not interested in actually writing to or reading from the file). The advantage of this is that if your program dies, then you automatically close the file and release your lock on it. You can do this zillions of times without running out of anything. For more information:
    Code:
    man fcntl
    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...