Release Date: 2025-01-27

Part 1: Readiness Exercise

This Readiness Exercise will be used to ascertain your competence in the course prerequisites.

No submission required, self-assesssment only. Please complete the test by 2025-01-30 3:30pm. We will start diving into the nitty-gritty details of Linux kernel internals (caution: it will be “dry”!).

If you are unable to answer most of the questions, you should consider dropping the class. Please take the “Computer Systems” course first before taking this course. LKP will be offered in future years so you will have a chance to take it if you’re really interested.

1. Explain why the output of the following code snippet is 0xEF.

int64_t v = 0xdeadbeef;
printf("%02x", ((char *)&v)[0]);

2. Explain why the expression, 1 > 0, is evaluated to 1 on 64-bit.

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

printf("char=%d, int=%d, long=%d", sizeof(char), sizeof(int), sizeof(long));

4. 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));
}

5. Explain why the output of the following code snippet is 0.

unsigned int i = 0;
printf("%u", i--);

6. 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;
}

7. Explain why the value of NULL (64-bit) is 0x0000000000000000.

8. Assuming the first printf results of the code is as follows, “1 = 0x7fffdfbf7f00”, explain why the rest of the output is as follows:

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;
}

9. Explain why the string, “hello world”, locate in .rodata section in the following code.

main() {
  const char *str = "hello world";
  printf("%s\n", str);
}

10. Explain why the variable ‘str’ locate in the stack.

main() {
  const char *str = "hello world";
  printf("%s\n", str);
}

11. Explain why the function ‘main’ locate in .text section.

main() {
  const char *str = "hello world";
  printf("%s\n", str);
}

12. Explain why the output of the following program is 5.

main() {
  char array[] = {1, 2, 3, 4, 5};
  int i = 4;
  printf("%d", array[i++]);
}

13. Explain why the output of the following program is 0x345.

#define PTXSHIFT 12
#define PTX(va) (((uint)(va) >> PTXSHIFT) & 0x3FF)

printf("0x%x", PTX(0x12345678))

14. 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));

15. Explain why “ASSERT(1 == 2, “error: should be equal”);” is the correct usage of the following macro.

#define ASSERT(a, b) do { switch (0) case 0: case (a): ; } while (0)

16. Explain why the expression, -1U > 0, evaluate is evaluated to 1 in x86.


Back to top

LKP: (Advanced) Linux Kernel Programming (Spring 2025) - Huaicheng Li