Thread: Alarm

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    11

    Alarm

    plz help me out guys..i need to built a alarm in which we have to create a interrupt(or a simple printf) after particular point of time also to take care that program in which alarm is called should not terminate before that particular point of time

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Are you just supposed to have the program wait until a certain time, and then print a message? How about calling Sleep() (for Windows) or sleep() (for *nix) and then printing your message?

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    11
    if we use sleep then the whole process will be sleeping until the particular point of time but my requirement is a timer has to run in the background without disturbing any process...

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Sleep counts time for you - so the "timer is running in the background"...
    Not whole process is sleeping - just the current thread
    And Sleep does not disturbs other processes...

    If you need the higher precision than the Sleep - you can use WaitForsingleObject on the current thread handle (in windows)

    If Sleep is not good for you - explain better what do you want to achieve...
    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

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    11
    Code:
    Void Alarm(int time)
    {
    // code to create a interrupt
    }
    
    main()
    {
    
    //some code goes here
    
    Alarm(10);
    
    FnafterAlarm();
    
    }
    My requirement is,

    1)once the control reaches the Alarm function we have to give some i/p (time) and the alarm function have to start a timer inpendentally and the control has to come out immediately after giving i/p.

    2)if suppose the control is in FnAfterAlarm() and the timer reaches it limit then it have to create that interrupt

    3)Also program should not terminate before that interrupt by Alarm().

    b4 that i am very weak in threads....

    hope this time i am very clear abt my requirement,if it is not enough i am ready to explain better
    Last edited by kky2k; 05-29-2007 at 11:45 PM.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    What O/S are you doing this for?

    Are you trying to recreate this?

    http://www.scit.wlv.ac.uk/cgi-bin/mansec?2+alarm

    If you're on Unix/Linux, you can simply fork() inside the Alarm() function and have the child sleep until the time has elapsed, and then call exit(). The parent meanwhile would return from the Alarm() function, do whatever it would like to do in other functions, and then call wait() for the child to finish. At that point the parent can terminate, too.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    11
    yes u r right i want to recreate that..is it possible to create that without using sleep()?

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    On windows you will need to CreateThread for this...
    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

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    11
    hi "vart" can u explain it further...

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I can't think of a better way to do it than to either fork() or create a new thread and just have the child process/thread sleep.

    Then again, maybe I just keep thinking about sleeping because I'm tired.

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    11
    ok "macGyver" thanks for ur contribution...anway i am parallely googling to find the solution

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mini Alarm System
    By peckitt99 in forum Windows Programming
    Replies: 4
    Last Post: 04-23-2008, 12:08 AM
  2. clarification: behavior of type_info::before
    By Sebastiani in forum C++ Programming
    Replies: 11
    Last Post: 04-04-2008, 06:53 PM
  3. alarm pipe
    By jacktibet in forum C Programming
    Replies: 2
    Last Post: 08-28-2003, 02:02 AM
  4. Replies: 23
    Last Post: 01-31-2003, 03:13 AM
  5. ANN: The Fourth Contest: Alarm Clock, sign up here
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 59
    Last Post: 08-10-2002, 12:24 AM