Page 1 of 4 123 ... LastLast
Results 1 to 10 of 32

Thread: Made a C alarm program, wanting some feedback on it

  1. #1
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    Made a C alarm program, wanting some feedback on it

    I just finished making a C program that works as a rudimentary(emphasis on "rudimentary") alarm clock, you start the alarm process by specifying the amount of time that should be elapsed before sounding the alarm, it also has a GTK UI that allows the user to easily end the process without having to do a kill. The code is given below:-
    Code:
    #include<stdio.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<string.h>
    #include<time.h>
    #include<gtk/gtk.h>
    #include<sys/types.h>
    #include<signal.h>
    
    /* We need this global variable to hold the child's PID 
     * so that it can be accessed by the stop_app() procedure.
     */
    pid_t child;
    
    void alarm_daemon(){
      int hour, minute;
      long int curr_time,end_time;
    
      printf("Enter the amount of time to be elapsed to start the alarm in HH:MM:- ");
      scanf("%d:%d",&hour,&minute);
    
      end_time = hour * 60 * 60;
      end_time += minute * 60;
      end_time += (long int) time(NULL);
    
      for(;;){
        curr_time = (long int) time(NULL);
    
        if(curr_time >= end_time)  break;
        
        sleep(900);
      }
    }
    
    void alarm_bell(){
      for(;;){
        if(system("echo \"Yo\"")){
          printf("Fix your system dawg!");
          break;
        }
        sleep(1);
      }
    }
    
    void stop_app(){
      kill(child,SIGKILL);
      gtk_main_quit();
    }
    
    gboolean delete_event_handler(GtkWidget *widget, GdkEvent *event,
    			      gpointer user_data)
    {
      return (FALSE);
    }
    
    
    void gtk_front(int argc, char *argv[]){
     GtkWidget *gtk_test, *vbox, *sleepy_label, *stop_button, *separator;
      gtk_init(&argc,&argv);
      gtk_test = gtk_window_new(GTK_WINDOW_TOPLEVEL);
      vbox = gtk_vbox_new(TRUE,10);
      sleepy_label = gtk_label_new("Got up yet?");
      stop_button = gtk_button_new_with_label("Yes");
      separator = gtk_hseparator_new();
    
      gtk_window_set_title(GTK_WINDOW(gtk_test),"Alaaarm!");
    
      gtk_container_set_border_width(GTK_CONTAINER(gtk_test),50);
    
      gtk_window_set_policy(GTK_WINDOW(gtk_test),FALSE,FALSE,FALSE);
    
      gtk_signal_connect(GTK_OBJECT(stop_button),"clicked",stop_app,NULL);
      gtk_signal_connect(GTK_OBJECT(gtk_test),"delete-event",GTK_SIGNAL_FUNC(delete_event_handler),NULL);
      gtk_signal_connect(GTK_OBJECT(gtk_test),"destroy",GTK_SIGNAL_FUNC(stop_app),NULL);
    
      gtk_container_add(GTK_CONTAINER(gtk_test),vbox);
      gtk_box_pack_start_defaults(GTK_BOX(vbox),sleepy_label);
      gtk_box_pack_start_defaults(GTK_BOX(vbox),separator);
      gtk_box_pack_start_defaults(GTK_BOX(vbox),stop_button);
    
      gtk_widget_show_all(gtk_test);
      gtk_main();
      return;
    }
    
    int main(int argc,char *argv[]){
      alarm_daemon();
    
      /* We fork the process so that such a situation occurs:-
       * One process is the inifinite loop process that keeps doing a certain action
       * again and again, sort of like a real world alarm clock(Child).
       * 
       * The other process is the GUI to the alarm clock and allows the user to
       * terminate the entire alarm by pressing the "OK" button(Parent).
       */
      child = fork();
    
      // The child process is delegated the alarm_bell().
      if (!child) alarm_bell();
      // The parent process is given the GUI task.
      else if (child) gtk_front(argc,argv);
    
      // Once the user presses the "OK" button, we start a sequence of events, each spaced by a 1 second interval and then end the process.
      sleep(1);
      system("echo \"what\"");
      sleep(1);
      system("echo \"Yo\"");
      sleep(1);
      system("echo \"Hello\"");
      sleep(1);
    
      return 0;
    }
    My own concerns are marked in red. Any feedback, criticism or suggestions to the code are more than welcome(which is why I posted it in the first place).
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

  2. #2
    Join Date
    Apr 2008
    Beans
    175

    Re: Made a C alarm program, wanting some feedback on it

    That's pretty neat.

    How well does GTK work with C, I rarely find any source code that uses C with GTK as opposed to C++?
    "Freedom is the freedom to say that two plus two equals four, if that is granted, then all else follows" - 1984

    Programming is part memorization, part implementation, and part imagination.

  3. #3
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Made a C alarm program, wanting some feedback on it

    Quote Originally Posted by loganwm View Post
    That's pretty neat.

    How well does GTK work with C, I rarely find any source code that uses C with GTK as opposed to C++?
    Well, considering that GTK+ was originally derived from C, I would imagine that people do use GTK+ with C, although I can agree with you when you say that most people prefer to use C++ and Python, I would think that people on the GNOME project would most usually prefer to use C with GTK+ than C++ with GTK+.

    Edit:- To answer your question a bit more directly, GTK+ should work very well with C, probably more so than with any other languages.
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

  4. #4
    Join Date
    Apr 2008
    Beans
    175

    Re: Made a C alarm program, wanting some feedback on it

    Quote Originally Posted by PmDematagoda View Post
    Well, considering that GTK+ was originally derived from C, I would imagine that people do use GTK+ with C, although I can agree with you when you say that most people prefer to use C++ and Python, I would think that people on the GNOME project would most usually prefer to use C with GTK+ than C++ with GTK+.

    Edit:- To answer your question a bit more directly, GTK+ should work very well with C, probably more so than with any other languages.
    Once I get some more work done on my Music Player I might write up a small optional interface with GTK. Check out the latest Code courtesy of 2AM programming.
    "Freedom is the freedom to say that two plus two equals four, if that is granted, then all else follows" - 1984

    Programming is part memorization, part implementation, and part imagination.

  5. #5
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Made a C alarm program, wanting some feedback on it

    Quote Originally Posted by loganwm View Post
    Once I get some more work done on my Music Player I might write up a small optional interface with GTK. Check out the latest Code courtesy of 2AM programming.
    I just saw it, it's pretty cool the way you use the ncurses interface to get the keyboard hits. Looks like a very nice program.
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

  6. #6
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Made a C alarm program, wanting some feedback on it

    Come on, anyone? I wouldn't mind even if the criticism or problem was with a way I used a variable, just any feedback on this would be nice.

    Even questions I don't mind.
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

  7. #7
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Made a C alarm program, wanting some feedback on it

    the way you use a global var makes sense. what doesn't make sense is what happens if you can't fork

    as for general program design ... I have to say that it's crap. there is something called alarm() which would tell the system to send an interupt to your program once the timer expires.
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  8. #8
    Join Date
    Jul 2006
    Location
    Hertfordshire
    Beans
    454
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Re: Made a C alarm program, wanting some feedback on it

    Quote Originally Posted by slavik View Post
    the way you use a global var makes sense. what doesn't make sense is what happens if you can't fork
    Makes sense to me, if the program can't fork, alarm bells should be ringing

    Quote Originally Posted by slavik View Post
    as for general program design ... I have to say that it's crap. there is something called alarm() which would tell the system to send an interupt to your program once the timer expires.
    I wouldn't go as far as to say the design was crap. Perhaps the OP was deliberately avoiding alarm() for learning purposes? But your point is correct, using alarm() to pass an interrupt would be neater than using fork().

  9. #9
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Made a C alarm program, wanting some feedback on it

    Quote Originally Posted by SeanHodges View Post
    Makes sense to me, if the program can't fork, alarm bells should be ringing



    I wouldn't go as far as to say the design was crap. Perhaps the OP was deliberately avoiding alarm() for learning purposes? But your point is correct, using alarm() to pass an interrupt would be neater than using fork().
    Lol, to be honest, I am learning, however the main reason I didn't use alarm() in the first place was because I had no idea that it existed, hence the fork() and an attempt at reinventing the wheel.

    Thanks for the point out slavik, I'll take a look at alarm() now.
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

  10. #10
    Join Date
    Aug 2007
    Location
    Kottawa, Sri Lanka
    Beans
    7,387
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Made a C alarm program, wanting some feedback on it

    Quote Originally Posted by slavik View Post
    the way you use a global var makes sense. what doesn't make sense is what happens if you can't fork

    as for general program design ... I have to say that it's crap. there is something called alarm() which would tell the system to send an interupt to your program once the timer expires.
    About the fork, according to my design, there are 2 parts, the CLI which keeps cycling infinitely to wake the user, and the GUI which allows the user to control the program without having to resort to a kill. So since I cannot(don't know is more like it) keep a command cycling once we initialise the GTK UI, I had to fork the process and the child one thing and the parent the other.

    And about alarm(), am I to understand that I can use sigaction() to do something(like start the alarm) once we receive the SIGALRM signal? If so, should I use a while do loop until alarm() reaches 0? Or should I use something else?
    Think carefully before executing commands containing "rm", especially "sudo rm -rf ", if you require more information concerning this matter, read this.
    I am an experimenter, give me the most stable OS and I can make it unstable in a few hours.

    C == seriously fast == FTW!

Page 1 of 4 123 ... LastLast

Bookmarks

Posting Permissions

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