Thread: How to calculate the program running took?

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    48

    Question How to calculate the program running took?

    Hey!
    How can I make the user limited with time in a program
    for example if I am asking for an integar..and the user took more than 5 seconds to give me the value..I need to send him a message saying..TIME OUT you thought of the value in a long time...
    Any solutions or suggestions brillant peeps in here?? :P

  2. #2
    Registered User
    Join Date
    Jul 2008
    Posts
    26
    time() will give you the current time in seconds, so if you call it more than once, you can calculate the time since the first call, and show an error box when it's 5 seconds or more.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    Thanks
    will wait for lasernight and broli86 answers

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You can't do that in standard C to interrupt the waiting of input. You have to do that via OS/library - specific functions.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    thanks...but,I am still a beginner..i don't think I have the privillage to do this

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    OK. That means you can't do it. The best you can do is tell the user after they entered a value that they took too long.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    Quote Originally Posted by Mole42 View Post
    time() will give you the current time in seconds, so if you call it more than once, you can calculate the time since the first call, and show an error box when it's 5 seconds or more.
    Well..do you the full syntax for this..time() is an undefined function even when I include <io.h>

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Find a book or something. It's in time.h.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    I know this
    I just need to know the syntax to do that timing thing

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Then look it up.

    Get yourself a book, and a good reference (may even be a book).

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    So none of you know it...its ok...will be waiting for others and thanks for the advice I will look in books

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by DaniiChris View Post
    So none of you know it...its ok...will be waiting for others and thanks for the advice I will look in books
    Listen, kid. The regulars on this forum know a lot more now than you will probably ever learn if you don't drop this arrogant attitude that you're entitled to every little bit of information on this forum.

    Do we look like human compilers or books to you? We're not. We were once learning this stuff just like you, whether in books on our own, or as students, or however else. Many times newbies seem to think they are entitled to our knowledge simply because they are newbies and we are slightly more experienced. Well, that's stupid. We fought to get where we're at, putting up with hours upon hour of studying and practice, and you should be willing to put up with the same work to get the same gains.

    Don't get us wrong. We'd love to help out, but you're pathetically clueless. It's not your fault you don't know anything, but it's your fault if you are not willing to learn properly. You have to ask meaningful questions and not waste our time. Part of programming is about managing your resources effectively. You are not managing your usage of this forum or of your time effectively.

    This is the year 2008, and you can get all kinds of books, references, tutorials, and whatever else free right from the Internet. You should be taking advantage of this instead of pestering us to teach you how to do things you are clearly not ready to learn. I'm serious about this.

    If you're smart, you'll think about what I'm saying and give it some consideration. It's important and it seriously will make you a better programmer and better in general at handling yourself in other situations.

  13. #13
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Here's some code I just wrote (yeah, I had time and I tough it was an interesting problem, so I gave it a try; I didn't look for non-blocking IO function but instead I went another way) who ask the user for a number, but will only wait 5 seconds:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <time.h>
    #include <signal.h>
    #include <assert.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    
    const unsigned int MAX_TIME = 5;
    enum
    {
        OK = 0,
        TOO_SLOW
    } result;
    
    void sig_handler(int sig_num)
    {
        if (sig_num == SIGCHLD)
        {
            result = OK;
        }
    
        else if (sig_num == SIGALRM)
        {
            result = TOO_SLOW;
        }
    }
    
    int main()
    {
        pid_t chld_process;
    
        struct sigaction sa;
    
        int shm_id;
        void *data;
    
        assert((shm_id = shmget(IPC_PRIVATE, sizeof(int), 0600 | IPC_CREAT)) != -1);
        assert((data = shmat(shm_id, NULL, 0)) != (void*) -1);
    
        sa.sa_handler = sig_handler;
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = 0;
        assert(sigaction(SIGALRM, &sa, NULL) != -1);
        assert(sigaction(SIGCHLD, &sa, NULL) != -1);
    
        printf("Enter a number: ");
        fflush(stdout);
    
        assert((chld_process = fork()) != -1);
        if (chld_process == 0)
        {
            scanf("&#37;d", (int*) data);
            shmdt(data);
            exit(EXIT_SUCCESS);
        }
    
        alarm(MAX_TIME);
        pause();
        alarm(0);
    
        if (result == OK)
        {
            printf("Thanks for %d\n", * (int*) data);
        }
    
        else
        {
            kill(chld_process, SIGINT);
            printf("\nToo slow... moron.\n");
        }
    
        shmdt(data);
    
        return 0;
    }
    Of course, as you can see, it will only compile on POSIX and mostly-POSIX compliant system. I use only the basic stuff: shared data segments, process creation and signal. You know, this kind of stuff. I didn't put any comments since it's like so easy to understand.

    Last edited by foxman; 07-07-2008 at 09:46 AM. Reason: Wasn't killing the child process. Added the "kill(...)" line.
    I hate real numbers.

  14. #14
    Registered User
    Join Date
    Jul 2008
    Posts
    48
    Thanks for helping
    and MacGyver I understand your message and I am extremly sorry for any inconvience caused..I am just a very beginner and I don't know much about programming...and I am learning it alone,I don't have someone to explain and You know what,I am 12 ...
    well..I am sorry again
    promise to ask proper questions not silly ones and would try my best to make them clear....but,if they pathetic for you..don't answer just keep others who don't think they're pathetic to answer

    agina I AM TOTALLY SORRY!!
    I hope my apology is accepted

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Well, if you're really that young, that's great that you want to get into C, and I seriously congratulate you on the effort. Nevertheless, I still recommend you find a good book or reference of some sort. You will be wasting much of your time if you rely on people on forums to teach you how to go about this.

    There are a list of recommended C books here as well as many tutorials online that you can learn from. They do teach these things, you just have to know how to find these resources.

    My post to you wasn't to discourage you from posting here or from asking questions or from learning C. The post was to encourage you to put more independent research into your own questions and learning. Learn to use google very often for these things. Find and follow a good tutorial (this website has a collection of simple tutorials). Many, many times, when writing a program, I forget some detail or I very often discover there is something I just simply don't know that I need to look up somehow. Most of what I need can be found through google, either directly or indirectly. This ability, though incredibly simple, should never be underestimated. Researching a topic is important.

    And this is also very important: Programming is really about concepts, not syntax. As I said before, one such concept is about managing resources in order to problem-solve.

    The issue with teaching you all of these things isn't so much the idea of explaining it as much as the idea of explaining everything behind it. We'd be here for quite awhile if I was to try to explain how to do time-based I/O because (assuming this is Windows you're working on) I would have to explain aspects of the Windows API, I/O in general, and how you put it all together. This would require you to already know C, and from what it sounds, you're still pretty new to it. If I even tried to explain all of this, it would result in you probably not understanding a thing I was saying and just simply frustrating you.

    You have to start slowly, and focus on the concepts. The syntax and other details come with experience as you work on it all. You really need a book of some sort or tutorials to follow with and slowly build a good working knowledge of the language.

    The fact you were able to read through my post and respond the way you did appears to be a point in your favor. I think you may do quite well in the future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stop program from running twice?
    By Abda92 in forum C Programming
    Replies: 19
    Last Post: 03-17-2008, 01:35 PM
  2. Replies: 3
    Last Post: 09-05-2005, 08:57 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Why is my program running away?
    By badkitty in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2001, 04:27 PM
  5. Running program
    By muffin in forum C Programming
    Replies: 5
    Last Post: 08-30-2001, 10:57 AM