Friday, April 19, 2024
HomeC ProgrammingGrasp On a Sec, Half I

Grasp On a Sec, Half I


I discovered to program on a microcomputer, a gizmo radically totally different in software program and {hardware} design from right this moment’s methods. An instance of this distinction is that should you wished to put in writing code that paused for a second, you wrote a for loop delay. Such a factor is obnoxiously impractical right this moment — which seems like a dare!


On my trusty TRS-80 Mannequin III — sure the one with the TV set for a monitor that frazzled my eyeballs — I knew that writing a FOR loop (in BASIC) counting from one to 1,000 would pause a program for one second. Foolish because it sounds, this method labored. And it was constant for all TRS-80 Mannequin III computer systems. Plenty of programmers used this trick.

The identical trick was employed within the early days of the PC, which was additionally technically a microcomputer. The PC’s processor was sooner, so the delay loops wanted to be longer. It wasn’t till multitasking working methods took over within the late Nineteen Nineties that writing a delay loop proved to be embarrassingly flawed.

Completely different processors have totally different speeds. Not solely that, however some applications could be interrupted by different processes. These advances in computing expertise are marvelous, however they render the delay loop ineffective. Additional, trendy pc {hardware} includes a system clock, which is much extra dependable for programming a pause.

Being curious and feeling nostalgic, I puzzled how massive a loop wanted to be on my present pc to pause program execution for one second?

Step one is to see how briskly the pc counts between seconds. To take action, I wrote a program that displays when seconds flip over. The next code fetches the present epoch time, or clock ticks in seconds, then waits for the worth to vary.

2023_03_18-Lesson-a.c

#embrace <stdio.h>
#embrace <time.h>

int principal()
{
    time_t now,then;

    /* receive clock tick Epoch worth */
    time(&now);
    printf("%ldn",now);

    /* pause one second */
    then = now;
    whereas(then==now)
        time(&now);
    printf("%ldn",now);

    return(0);
}

Variable now shops the present epoch time, which can also be saved in variable then. A whereas loop spins so long as each now and then are equal. Inside the loop, variable now is up to date with the present clock tick worth. When it modifications, the loop ends and the brand new time is output:

1677951318
1677951319

Whereas this code signifies when the system clock’s seconds flip over, it doesn’t actually time an actual second. That’s as a result of this system might begin between seconds. No, to time a full second, a second whereas loop is important. Right here is the up to date code:

2023_03_18-Lesson-b.c

#embrace <stdio.h>
#embrace <time.h>

int principal()
{
    time_t now,then;

    /* receive clock tick Epoch worth */
    time(&now);

    /* pause one second */
    then = now;
    whereas(then==now)
        time(&now);
    /* now a brand new second has begun
       begin the subsequent "full second" loop */
    printf("%ldn",now);
    then = now;
    whereas(then==now)
        time(&now);
    printf("%ldn",now);

    return(0);
}

The worth of now is output after the primarywhereas loop ends at the beginning of a brand new second. Then, after the second whereas loop stops, the subsequent clock tick worth is output:

1677951742
1677951743

Chances are you’ll encounter a slight pause earlier than the primary line is output. Nonetheless, the time between the primary line output and the subsequent is one full second (or as shut as it may be given different issues occurring within the system).

The subsequent step is to rely the variety of iterations for the second whereas loop. I cowl this code replace in subsequent week’s Lesson, together with the ultimate for loop code that delays the pc for one second — identical to the outdated days.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments