Вот мой код для user/sleep.c:
#include "kernel/types.h"
#include "user/user.h"
int
main(int argc, char *argv[])
{
if (argc < 2)
{
printf("You have forgotten to pass an argument.");
exit(1);
}
int arg = atoi(argv[1]);
sleep(arg);
exit(0);
// return 0;
}
Все в порядке, но с возвратом 0 вместо выхода (0) я получаю следующую ошибку:
usertrap(): unexpected scause 0x000000000000000d pid=4
sepc=0x00000000000000f6 stval=0x0000000000003008
Это почему?
Это потому, что xv6
не является стандартным в этом вопросе:
см. https://tnallen.people.clemson.edu/2019/03/04/intro-to-xv6.html
Returning after main is a special case that is not supported in XV6, so just remember to always exit() at the end of main instead of return. Otherwise, you will get a “trap 14,” which in this case is XV6-speak for “Segmentation Fault.” Finally, notice that our headers are different. There is no #include <stdio.h> as you might be used to. Again, this is because the standard library is different. Check out these headers to see what kind of user utilities you have available.
Так зачем нам понадобился exit()
в xv6?
Потому что, например, при создании программы с gcc
для Linux цепочка инструментов добавляет необходимый вызов exit
: см. https://en.wikipedia.org/wiki/Crt0
Короче говоря, в Linux ваша программа начинается не с main
, а с start
:
void start(void) {
/* get argc, argv, env) */
int r = main(argc, argv, envp); /* << start calls the actual main */
exit(r);
}
На xv6, напротив, реальная отправная точка main
, когда ОС пытается вернуться из main
, у нее нет адреса для извлечения, что вызывает segfault
@hyde
xv6
не является стандартным в этих вопросах. И параметры компиляции по умолчанию уже включают-Wall
и-Werror
. см. Makefile