Thread: Sample Semaphore program on linux

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    32

    Sample Semaphore program on linux

    Dear All,

    I am looking for a Sample Semaphore program on linux, Plz help me

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by ashok449 View Post
    Dear All,

    I am looking for a Sample Semaphore program on linux, Plz help me
    GIFY.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    32
    I have an Idea on Semaphores but dont know how to apply on a real time application.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    is this semaphore signalling? thats a cool little programming idea for a laugh, morse code translator would be good too..

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ashok449 View Post
    I have an Idea on Semaphores but dont know how to apply on a real time application.
    What sort of "real time" are we talking here?

    Using Linux as a base for strict real-time applications is like using a Ford Transit to enter a Formula 3 race - it hasn't got a chance.

    On the other hand, if you are just after some "decent human perceived realtime" then I suppose it would work.

    What exactly is the problem with applying semaphores on a realtime application - it would be better if you discussed at a "bigger picture" level what you want to achieve, because that would allow us to give you sensible advice.

    --
    Mats
    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.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    is this semaphore signalling? thats a cool little programming idea for a laugh, morse code translator would be good too..
    More likely ashok449 is referring to the programming technique rather than the communication method.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    32
    Ok, here is some code I found on net. Plz tell me how this works.


    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/sem.h>
    
    #define KEY (1492)
    void main()
    {
       int id;  
       struct sembuf operations[1];
       
       int retval; 
    
       
       id = semget(KEY, 1, 0666);
       if(id < 0)
      
       {
          fprintf(stderr, "Program sema cannot find semaphore, exiting.\n");
          exit(0);
       }
    
       /* Do a semaphore V-operation. */
       printf("Program sema about to do a V-operation. \n");
    
       
        operations[0].sem_num = 0;
        /* Which operation? Add 1 to semaphore value : */
        operations[0].sem_op = 1;
        /* Set the flag so we will wait : */   
        operations[0].sem_flg = 0;
    
    
        /* So do the operation! */
        retval = semop(id, operations, 1);
    
        if(retval == 0)
        {
           printf("Successful V-operation by program sema.\n");
        }
        else
        {
           printf("sema: V-operation did not succeed.\n");
    	perror("REASON");
        }
    }
    thanks in advance ...

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I personally think that code is pretty clear [even tho' I've never seen that piece of code before] - can you perhaps clarify which part of the code you don't understand.

    --
    Mats
    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.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    32
    retval = semop(id, operations, 1);


    if retval ==0 then semop is successfull which means I need to place my code here, If so how to aquire/release again.


    Code:
    if(retval ===0)
    {
      // semop success
    do some stuff .....
    // how to release or aquire lock 
    
    operations[0].sem_num = 0;
        /* Which operation? Add 1 to semaphore value : */
        operations[0].sem_op = -1;
        /* Set the flag so we will wait : */   
        operations[0].sem_flg = 0;
    
    semop(id, operations, 1); //  is this corrrect
    
    }
    if retval ==0 then semop is successfull which means I need to place my code here, If so how to aquire/release again.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That looks about right, yes.

    --
    Mats
    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.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    32
    Now, Anyone can give me a small real-time example on semaphore

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Seriously?

    Doesn't google work!? I'll have to quote brewbuck on this -- GIFY

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    32
    zacs7 and brewbuck, you see in this thread I almost provide everything except a clarification which I require. Thats all I need and thats all about a forum is.


    If getting sample code from google is enough then what is the requirement of a forum.

    Forum means expressing yourself and getting advices.


    Anyhow thanks for everything...

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by ashok449 View Post
    Forum means expressing yourself and getting advices.
    So you expressed youself and got a very good advice (go search the web). Now it is upto you if you will follow the advice and learn something or ignore it and stay as uneducated as you are now.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a sample program for basketball game
    By Marrah_janine in forum C++ Programming
    Replies: 5
    Last Post: 12-08-2006, 04:07 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. newbie : compiling a C++ program in linux
    By gemini_shooter in forum C++ Programming
    Replies: 5
    Last Post: 06-23-2005, 02:45 PM
  4. A C++ program in Solaris that cannot be ported to Linux
    By tvenki in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2004, 12:13 PM
  5. simple program on linux
    By finnepower in forum Linux Programming
    Replies: 10
    Last Post: 05-29-2004, 03:10 PM