[SW사관학교 정글]68일차 TIL- systemcall 구현(halt, exit, create, remove)

김승덕·2022년 11월 25일
0

SW사관학교 정글 5기

목록 보기
108/150
post-thumbnail

Systemcall

이번에는 pintos project2중에 systemcall을 정리하고자 한다. 위키피디아의 정의에 따른 시스템 콜의 정의는 다음과 같다.

시스템 호출(system call)은 운영 체제의 커널이 제공하는 서비스에 대해, 응용 프로그램의 요청에 따라 커널에 접근하기 위한 인터페이스이다. 보통 C나 C++과 같은 고급 언어로 작성된 프로그램들은 직접 시스템 호출을 사용할 수 없기 때문에 고급 API를 통해 시스템 호출에 접근하게 하는 방법이다.

시스템 콜이란?

💡 유저 프로그램이 OS에 쉽게 접근할 수 있도록 만든 인터페이스

시스템 콜이 필요한 이유

우리는 컴퓨터에서 보통 응용프로그램을 쓴다. 사실 거의 응용프로그램을 쓴다고 보면된다. 하지만 이러한 응용프로그램이 컴퓨터 하드웨어를 직접 접근할수있도록 한다면 컴퓨터가 망가질 위험이 있다. 그래서 OS는 커널모드와 유저모드로 나누어서 응용프로그램이 직접 하드웨어를 접근하지 못하도록 막는다.

pintos에서 우리가 구현해야할 system call

  1. void halt(void)
  2. void exit (int status);
  3. bool create (const char *file, unsigned initial_size);
  4. bool remove (const char *file);
  5. int open (const char *file);
  6. int filesize (int fd);
  7. int read (int fd, void *buffer, unsigned size);
  8. int write (int fd, const void *buffer, unsigned size);
  9. void close (int fd);
  10. void seek (int fd, unsigned position);
  11. unsigned tell (int fd);
  12. int exec (const char *cmd_line);
  13. int wait (pid_t pid);
  14. tid_t fork(const char *thread_name, struct intr_frame *f);

이렇게나 많다… 위의 순서대로 구현하는것을 추천한다. file.c, filesys.c 에서 래퍼함수를 찾아서 사용하면 쉽게 구현이 가능한 함수들도 있다.

일반적으로 각각의 테스트케이스들이 존재하지만 테스트케이스가 없는 함수도 있고, wait, fork, exec의 경우 유기적이므로 테스트케이스를 돌릴때 모두 구현하고 돌려야한다.

void halt(void)

void halt(void)
{
	power_off();
}

Terminates Pintos by calling power_off() (declared in src/include/threads/init.h). This should be seldom used, because you lose some information about possible deadlock situations, etc.

컴퓨터를 종료하는 역할을 하는 시스템콜이다. 이 시스템콜은 power_off() 라는 함수를 사용하면 쉽게 구현이 가능하다.

void exit (int status);

void exit(int status)
{
	thread_current()->exit_status = status;
	printf("%s: exit(%d)\n", thread_name(), status);
	thread_exit();
}

Terminates the current user program, returning status to the kernel. If the process's parent waits for it (see below), this is the status that will be returned. Conventionally, a status of 0 indicates success and nonzero values indicate errors.

유저 프로그램을 종료하는 시스템콜이다. 현재 스레드의 상태를 인자로 받은 상태로 바꾸어주고 thread_exit() 함수를 사용하여 스레드를 종료시켜주면 된다. 그리고 printf() 를 통한 출력도 잊으면 안된다!

bool create (const char *file, unsigned initial_size);

bool create(const char *file, unsigned initial_size)
{
	check_address(file);
	if (filesys_create(file, initial_size))
		return true;
	else
		return false;
}

Creates a new file called file initially initial_size bytes in size. Returns true if successful, false otherwise. Creating a new file does not open it: opening the new file is a separate operation which would require a open system call.

파일을 생성하는 시스템콜이다. 성공일경우 true, 실패일경우 false를 리턴한다. 인자 file은 생성할 파일의 이름 및 경로 정보이고 initial_size는 생성할 파일의 크기를 의미한다.

bool remove (const char *file);

bool remove(const char *file)
{
	check_address(file);
	if (filesys_remove(file))
		return true;
	else
		return false;
}

Deletes the file called file. Returns true if successful, false otherwise. A file may be removed regardless of whether it is open or closed, and removing an open file does not close it. See Removing an Open File in FAQ for details.

파일을 삭제하는 시스템콜이다. 성공일경우 true, 실패일 경우 false를 리턴한다.

profile
오히려 좋아 😎

0개의 댓글