ex1 - Readiness

  1. Explain why the output of the following code snippet is "char=1, int=4, long=8" in x86 (64-bit).

    printf("char=%d, int=%d, long=%d", \
    sizeof(char), sizeof(int), sizeof(long));
    
  2. Explain why the output of the following code snippet is "st0 = 8, st1 = 8".

    struct st0 {
      int x;
      char y;
    };
    struct st1 {
      int x;
      char y;
      char z;
    };
    int main()
    {
      printf("st0 = %d, st1 = %d\n",
      sizeof(struct st0), sizeof(struct st1));
    }
    
  3. Explain why the output of the following code snippet is "i=5, j=10".

    int main()
    {
      int i, j, *p, *q;
      p = &i;
      q = &j;
      *p = 5;
      *q = *p + i;
      printf("i = %d, j = %d\n", i, j);
      return 0;
    }
    
  4. Explain why the output of the following code is 0x124000.

    #define PGSIZE 4096
    #define CONVERT(sz) (((sz)+PGSIZE-1) & ~(PGSIZE-1))
    
    printf("0x%x", CONVERT(0x123456));
    
  5. Assuming the first printf results "1 = 0x7fffdfbf7f00", explain why the rest of the output is as follows:

    2 = 0x7fffdfbf7f04
    3 = 0x7fffdfbf7f00
    4 = 0x7fffdfbf7f14
    main() {
    int x[5];
      printf("1 = %p\n", x);
      printf("2 = %p\n", x+1);
      printf("3 = %p\n", &x);
      printf("4 = %p\n", &x+1);
      return 0;
    }
    
  6. In GDB, what does the following sequence of commands do?

    (gdb) break main
    (gdb) run
    (gdb) next
    (gdb) print argc
    
  7. Briefly explain what the following Makefile snippet does:

    KERNOBJS = bio.o console.o exec.o file.o fs.o ide.o ioapic.o
    ...
    kernel: $(KERNOBJS) entry.o kernel.ld
        $(LD) $(LDFLAGS) -T kernel.ld -o kernel entry.o $(KERNOBJS)
    
  8. Consider the following C program:

    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    int main(int argc, char *argv[]) {
      printf("parent: pid = %d\n", getpid());
    
      pid_t pid = fork();
    
      if (pid == 0) {
        // child process
        printf("child: pid = %d, parent pid = %d\n", getpid(), getppid());
        char *args[] = {"/bin/echo", "hello from exec", NULL};
        execv(args[0], args);
        printf("this line should not be printed\n");
      } else {
        // parent process
        wait(NULL);
        printf("parent: child has exited\n");
      }
    
      return 0;
    }
    
  • 8.1 Explain why "this line should not be printed" is not printed?

  • 8.2 What happens to the child process after execv is called?

  • 8.3 What is the role of wait(NULL) in the parent process?

  1. In a tmux session, open two terminal panes side-by-side.

Use one pane to run xv6 with make qemu-nox, and use the other pane to locate the source code of the xv6 kernel's main() function using cscope.

Take a screenshot showing both the running xv6 kernel and cscope side by side, and upload it.