Sync Process A and B with Semaphore

This is a discussion on Sync Process A and B with Semaphore within the Linux Programming forums, part of the Platform Specific Boards category; hi i've done some pseudo code basically I need to get the thing to run in ABABABAB here is what ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    68

    Sync Process A and B with Semaphore

    hi

    i've done some pseudo code

    basically I need to get the thing to run in ABABABAB

    here is what i've come up with
    Code:
    Initialise Semaphore
    semaphore = sync
    
    Process A
    Begin
    Down(sync)
    doA
    Up(sync)
    End
    
    Process B
    Begin
    Down (sync)
    doB
    Up (sync)
    End
    can you check if its correct.
    -Ti22-

  2. #2
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Ti22
    basically I need to get the thing to run in ABABABAB.
    This is very likely to be the output, but I don't think it is really guaranteed. If an interrupt kicks in after A leaves the critical section and another right before B can enter it's own section, A might enter again. Mutual exclusion is guaranteed, but ABAB... not. Especially when you start to add non-critical code to A or B outside sem.

    You could use some variable, i.e. "next" and set it in each process' critical section to the id of the other process. It would have to be guarded and you'd have to find some way for each process to wait on next becoming it's own id (without the use of busy waiting I guess).
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    68
    so if i did
    Code:
    Initialise Semaphore
    semaphore = sync
    int count = 0
    
    Process A
    Begin
    if count = 0 (Down(sync))
    doA
    count++
    if count = 1 (Up(sync)_
    End
    
    Process B
    Begin
    if count = 1 (Down (sync))
    doB
    count --
    if count = 0 (Up (sync))
    End
    -Ti22-

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Try it, looks good to me though.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    68
    we weren't exactly taught to program in linux just writing shell scripts and he just told us about semaphores and all that,

    I know C but how would I go about trying something like that?

    just make it out put text in the console?

    i don't even know how to apply semaphores in C like how to do Up(sync) Down(Sync)

    we weren't taught all that stuff just the pseudo code.

    for the project we're not supposed to write a fully working code, but if u can tell me how or point me to a URL i'll give it a shot.
    -Ti22-

  6. #6
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Ti22
    so if i did
    Code:
    Initialise Semaphore
    semaphore = sync
    int count = 0
    
    Process A
    Begin
    if count = 0 (Down(sync))
    doA
    count++
    if count = 1 (Up(sync)_
    End
    
    Process B
    Begin
    if count = 1 (Down (sync))
    doB
    count --
    if count = 0 (Up (sync))
    End
    That first line should probably be
    Code:
    semaphore sync = 1
    for a binary semaphore. Also, does that "if count = 0..." start some kind of if-block or does it only conditionalize Down(sync)?

    If so, what happens if A runs twice? The first run, sync would not block this process (but the other) because it started as 1, downed to 0, everything ok. doA was protected, count becomes 1, the semaphore is upped to 1 again. Fine.
    A runs again, b is out for lunch. count is 1 so down(sync) is not even invoked. Critical section is entered unprotected, count is increased to 2. 2 is not 1 so sync is not upped and now still 1.
    Now count is 2, neither process will invoke any semaphore instructions, doA and doB can be entered freely. End of world :-(

    Or maybe I'm just misunderstanding that pseudo code
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Well for how to implement them in c, it depends on which operating system you are using. If you are using any posix operating system, just search google for posix semaphores, it brings up a lot of hits.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed

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