It's UWAweek 30 (2nd semester, week 1)

help2002

This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.
Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.
Displaying selected article
Showing 1 of 1168 articles.
Currently 3 other people reading this forum.


 UWA week 35 (2nd semester, week 6) ↓
SVG not supported

Login to reply

👍?
helpful
3:53am Tue 29th Aug, Christopher M.

ANONYMOUS wrote:
> I heard Chris say in one of the lectures/workshops that one of the ways you could advance time was to increment the clock by 1usec at a time and check what's happening at that point. However, this would mean for things like longsleep, there would be 5 million operations of checking the state of just one process, which is very slow. And if there were 10 longsleeps, then every process would need to be checked 5 million times each, which would be even slower. Have I misunderstood what he meant by incrementing 1usec at a time? Or is incrementing by 1usec just *one* of the ways you can do it, but perhaps not the best?
Your thinking and the stated numbers are correct, but the statement '...which is very slow' is not true. How long will it take to perform 5 million (integer) comparisons on a laptop capable of 2 billion instructions per second? Your laptop, itself, can answer the question with a simple program (an experiment). Actually, each iteration through the loop also requires a test, so the timing is really an overestimate. There are other, less simple, approaches which advance a clock, other than by 1usec each time, by determining (guaranteeing) what will happen next, but it may take more effort to determine that. But keep in mind that the speed of your project is not being assessed, that you're being assessed on your understanding of the problem, not a super-efficient implementation.
#include <stdio.h>
#include <inttypes.h>
#include <sys/time.h>

//  RETURNS THE CURRENT TIME, AS A NUMBER OF MILLISECONDS, IN A 64-bit INTEGER
int64_t milliseconds(void)
{
    struct timeval  now;

    gettimeofday( &now, NULL );     // timezone not required, so we pass NULL
    return ((int64_t)now.tv_sec * 1000) + now.tv_usec/1000;
}

int main(int argc, char *argv[])
{
    int x = 3;

    int64_t start   = milliseconds();

    for(int i=0 ; i<5000000 ; ++i) {
        if(i == x) {
            ;                       // do nothing
        }
    }
    printf("%llimsec\n", milliseconds() - start);
    return 0;
}

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  7:24AM Jul 25 2024
Privacy policy