Lab Solution

Assignment Task: sleep (Lab1-w1)

Objective: Implement a user-level program that pauses for a user-specified number of ticks.

1. Full Code Solution (user/sleep.c)

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

int main(int argc, char *argv[]) {
  // If the user forgets to pass an argument, print an error message[cite: 25].
  if (argc < 2) {
    fprintf(2, "Error: missing argument for sleep\n");
    exit(1);
  }

  // Convert command-line string argument to integer using atoi[cite: 26].
  int ticks = atoi(argv[1]);

  // Use the system call "pause"[cite: 27].
  pause(ticks);

  // Call exit(0) when done[cite: 29].
  exit(0);
}

2. Setup Instructions

  • Modify Makefile: Add _sleep to the UPROGS list to ensure the program compiles and is available in the xv6 shell.

  • Compilation: Execute make qemu in your terminal.

Last updated