Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

363
Vistas
Why return does not exit a process in xv6?

Here is my code for 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;
}

All is fine but with the return 0 instead of exit(0), I get the following error:

usertrap(): unexpected scause 0x000000000000000d pid=4
            sepc=0x00000000000000f6 stval=0x0000000000003008

Why is that?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It's because xv6 is not standard on this point:  

see 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.


So why did we need exit() in xv6?

Because, when building a program with gcc for linux for instance, the tool chain add the required exit call: see https://en.wikipedia.org/wiki/Crt0

In short, on linux, your program don't start at main but at start:

void start(void) {
    /* get argc, argv, env) */

    int r = main(argc, argv, envp);  /*  << start calls the actual main */

    exit(r);
}

On xv6, on contrary, the real starting point is main when OS try to return from main, it has no address to pop what cause a segfault

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda