Aditya Singh
Aditya Singh

Reputation: 59

Trying to understand UNIX system calls on XV6

I'm trying to write a simple system call on XV6 (documentation available here and Github here) in order to understand how they're implemented. I've used these steps

  1. In syscall.c, declared extern int sys_hello(void) and added [SYS_hello] sys_hello into static int (*syscalls[])(void) array
  2. In syscall.h, defined SYS_hello as call number 22
  3. In user.h, declared the function prototype as int hello (void);
  4. In usys.S, added SYSCALL(hello) to the macro
  5. In sysproc.c, added the function sys_hello(void) at the bottom

    int sys_hello(void)
    {
      cprintf ("Hello World System Call\n");
      return 0;
    }
    
  6. Created hello.c which simply calls the hello() system call

  7. Added hello.c to the Makefile and ran the code

It worked as expected.

Now, my question is that it seems that the array in syscall.c matches the indexes of the commands with the system call numbers in syscall.h file However, if I move the hello position to the second spot in the syscall.c and let the system command number in syscall.h stay 22 the system command works as before. Where as, I expected that it'd break. Can you help me understand how the array syscall.c maps (if that's even the correct word) to the syscall.h system call number?

I'm fairly new to XV6 and C so please don't get mad at me if this question seems silly, I'm only trying to learn.

Here is the Github link to my fork of the XV6 repository if that helps: github.com/AdityaSingh/XV6

Upvotes: 4

Views: 1005

Answers (1)

Jahaja
Jahaja

Reputation: 3302

The array of syscalls is syscall.c makes use of the designated initialization syntax where you can specify at which index to assign the value.

static int (*syscalls[])(void) = {
   [SYS_fork]    sys_fork,
}

SYS_fork, defined as 1, specifies the index where to assign sys_fork. Thus the order of the elements doesn't matter using this syntax.

Upvotes: 3

Related Questions