Thread: Simple process synchronization question

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    5

    Simple process synchronization question

    Hi,

    I have a relatively simple question about the best way to do process synchronization in C. I'm writing a multi-process application where I spawn multiple processes at the beginning using fork.

    Each of these processes then goes into a while loop until some criteria is met. I now want to synchronize those processes so that they start each loop iteration at about the same time. I basically want something like a barrier that has to be reached before starting a loop iteration.

    What is the easiest way to do that in C? I though about using semaphores, but not sure if this is the best solution.

    Thanks,
    Antriber

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You can do this with semaphores. One process does a sem_wait at the top of the loop, and the other process does a sem_post when it's ready to start the loop.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    5
    OK, thanks. And I assume this will also work with more than 2 processes.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by AnTriber View Post
    OK, thanks. And I assume this will also work with more than 2 processes.
    Yeah.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  2. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  3. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  4. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM