作業系統›Ch4 行程與執行緒管理
第 42 題/共 70 題
◀ OS 42/70
42. fork()、Process Creation、Output Ordering
#OS-04-042中fork()Process CreationOutput Ordering

Consider the following C code. Which outputs are possible?

int main() {
  printf("A");
  if (fork() == 0) {
    usleep(((rand() % 5) + 1) * 10000); /* wait for a random time */
    printf("B");
    if (fork() == 0) {
      usleep(((rand() % 5) + 1) * 10000); /* wait for a random time */
      printf("C");
    }
    else {
      wait(NULL);
      printf("D");
    }
  }
  else {
    usleep(((rand() % 5) + 1) * 10000); /* wait for a random time */
    printf("E");
    wait(NULL);
  }
  return 0;
}
📄 交大111
跳轉到第題
▤完整推導請見《WH 資工筆記 · 作業系統》Ch4 行程與執行緒管理
本章題號 · 41–60 / 70