Fırat Tuncer
Fırat Tuncer

Reputation: 85

How to control execution time in C

I have a C program which will print the prime numbers into a .txt file. I want program to ask me the execution time by minutes. Trying to solve with the piece of code.

#include <stdio.h>
#include <time.h>

int main()
{
    execute();
    return(0);
}

int execute(int minute)
{
    time_t now;
    time(&now);
    struct tm *tmp = localtime(&now);
    printf("How long you want to execute the program by minute? ");
    scanf("%d",&minute);
    printf("%d %02d %02d\n",tmp->tm_hour, tmp->tm_min+minute, tmp->tm_sec);
    return(minute);
}

and here also is the code that I want to run.

#include <stdio.h>

int isprime(int x);

int main(void)
{
        int AX_REGISTER, BX_REGISTER;
        FILE *number, *primes;

        int forever = 1;

        while (forever)
        {

                if ((number = fopen("lastnumber.txt", "r")) == NULL)
                {
                        fprintf(stderr, "Cannot read LASTNUMBER.txt file.\n");
                        return 1;
                }
                fscanf(number,"%d",&AX_REGISTER);
                fclose(number);

                BX_REGISTER=AX_REGISTER;
                AX_REGISTER=AX_REGISTER+1;

                if ((number = fopen("lastnumber.txt", "w")) == NULL)
                {
                        fprintf(stderr, "Cannot write LASTNUMBER.txt file.\n");
                        return 1;
                }
                fprintf(number,"%d\n",AX_REGISTER);
                fclose(number);

                if (isprime(BX_REGISTER))
                {

                        if ((primes = fopen("primes.txt", "a")) == NULL)
                        {
                                fprintf(stderr, "Cannot open PRIMES.txt file.\n");
                                return 1;
                        }

                        fprintf(primes,"%d\n",BX_REGISTER);
                        fclose(primes);
                }

        }
        return 0;
}

int isprime(int x)
{
        int i;

        for (i=2;i<x;i++)
        {
                if (x%i==0)
                        return 0;
        }
        return 1;
}

Since I am a newbie in C programming language I can not solve it by myself. Can someone help me?

Upvotes: 1

Views: 1729

Answers (2)

AlastairG
AlastairG

Reputation: 4314

Since you are running in a while loop, simply get the current time outside the while loop, add the minutes, and also get the time at the end of the while loop. Something like:

time_t now;
time_t end_time;

now = time();
end_time = now + (minutes * 60);

while(now < end_time)
{
    ...Do processing here...

    now = time();
}

One drawback of this method is that if isprime() takes a very very long time to run then you may overshoot the end time by quite a lot. One way to solve that is to pass the end_time value to isprime() and check it on the loop in that function too. Or just press Ctrl-C in the terminal where you are running the program :)

By all means write to the files in case the program crashes - you won't want to lose hours of work, but I suggest you only read the value from lastnumber.txt before the while loop and after that just hold it in memory. It will speed up your programs.

Also, unless running on Windows which has horrible file locking semantics, it may be worth holding the write file handles open the whole time to save cycles constantly opening and closing the files.

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 993901

Some platforms have an alarm() function that you can use to send a signal to your application after a specified number of seconds.

If you're not on Windows, man alarm should give you some information about this function. If you're on Windows, a different approach may be needed.


On another note, you should consider using variable names that mean something to the code you're writing. To a casual reader, the name BX_REGISTER means nothing. Perhaps consider changing its name to number_to_test (and AX_REGISTER to next_number or something).

Upvotes: 2

Related Questions