본문 바로가기
TIL/OS & Linux

MIT 6.828 - 1. Lab 01: Xv6 and Unix utilities - sleep 코드 및 해석

by 왁왁s 2022. 11. 5.

MIT 6.828 - 1. Lab 01: Xv6 and Unix utilities - sleep

 

 

문제 (Problems)

sleep (easy)

Implement the UNIX program sleep for xv6; your sleep should pause for a user-specified number of ticks. A tick is a notion of time defined by the xv6 kernel, namely the time between two interrupts from the timer chip. Your solution should be in the file user/sleep.c.

Some hints:

  • Before you start coding, read Chapter 1 of the xv6 book.
  • Look at some of the other programs in user/ (e.g., user/echo.c, user/grep.c, and user/rm.c) to see how you can obtain the command-line arguments passed to a program.
  • If the user forgets to pass an argument, sleep should print an error message.
  • The command-line argument is passed as a string; you can convert it to an integer using atoi (see user/ulib.c).
  • Use the system call sleep.
  • See kernel/sysproc.c for the xv6 kernel code that implements the sleep system call (look for sys_sleep), user/user.h for the C definition of sleep callable from a user program, and user/usys.S for the assembler code that jumps from user code into the kernel for sleep.
  • main should call exit(0) when it is done.
  • Add your sleep program to UPROGS in Makefile; once you've done that, make qemu will compile your program and you'll be able to run it from the xv6 shell.
  • Look at Kernighan and Ritchie's book The C programming language (second edition) (K&R) to learn about C.

Run the program from the xv6 shell:

      $ make qemu
      ...
      init: starting sh
      $ sleep 10
      (nothing happens for a little while)
      $
    

Your solution is correct if your program pauses when run as shown above. Run make grade to see if you indeed pass the sleep tests.

Note that make grade runs all tests, including the ones for the assignments below. If you want to run the grade tests for one assignment, type:

     $ ./grade-lab-util sleep

 

This will run the grade tests that match "sleep". Or, you can type:

     $ make GRADEFLAGS=sleep grade

which does the same.

 

 


 

코드(Code) - sleep.c 파일

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

int main(int argc, char *argv[])
{
	 if(argc != 2)
	 {
		  fprintf(2, "usage: sleep [ms] \n");
		   exit();
	 }

	 int sleep_time = atoi(argv[1]);
      	 sleep(sleep_time);

	 exit();
}

 


 

로직(logic) 설명

User가 $ sleep 10 을 입력했을 때 10에 해당되는 부분은 argv[1]에 저장이 된다. 이는 문자 스트링 값으로 가져오는데, 이를 c언어의 atoi( ) 함수를 활용해 문자열을 정수로 변환한다. sleep_time이라는 변수에 user로부터 입력 받은 값에 해당 되는 argv[1]을 담고 sleep을 system call 한다. Sleep에 대한 system call은 user/user.h에 sleep이 정의되어 있어 user/user.h를 include한다.

 


요약(Summary)

1. User input “sleep 10”
2. 10을 받아 atoi를 사용해 정수로 변환 후 sleep 시스템 콜 호출

 


구현 결과(Result)

댓글