Codes

1. Seconds to Ticks Conversion

This version converts seconds to ticks based on the assumption that 10 ticks equal 1 second.

#include "kernel/types.h"
#include "user/user.h"

int main(int argc, char *argv[]) {
    if (argc != 2) {
        printf("Error: Missing argument\n");
        exit(1);
    }
    int seconds = atoi(argv[1]);
    int ticks = seconds * 10; // Assume 10 ticks = 1s
    pause(ticks);
    exit(0);
}

2. Strict Argument Validation

Ensures exactly one argument is provided, printing a specific usage message otherwise.

#include "kernel/types.h"
#include "user/user.h"

int main(int argc, char *argv[]) {
    if (argc != 2) {
        fprintf(2, "Usage: sleep [ticks]\n");
        exit(1);
    }
    pause(atoi(argv[1]));
    exit(0);
}

3. Ticks Countdown

Prints the remaining duration every 10 ticks during the sleep period.

4. Positive Integer Validation

Verifies that the user input is a non-negative integer.

5. Sleep with Custom Wake Message

Takes a second argument to display a message once the sleep finishes.

6. Duration Verification with uptime()

Uses uptime() to measure the exact number of ticks that passed during the pause call.

7. Iterative Loop of pause(1)

Instead of one long pause, this uses a loop to pause one tick at a time.

8. Default Sleep Duration

If no argument is passed, it defaults to 10 ticks instead of exiting with an error.

9. Periodic Sleep (Interval and Count)

Repeats a sleep interval for a specified number of cycles.

10. Process Identification

Prints the Process ID (PID) before entering the sleep state.

Last updated