def main(): if open_("console", O_RDWR) < 0: mknod("console", 1, 1) open_("console", O_RDWR) dup(0) # stdout dup(0) # stderr while True: printf(1, "init: starting sh\n") pid = fork() if pid < 0: printf(1, "init: fork failed\n") exit() if pid == 0: exec_("sh", argv) printf(1, "init: exec sh failed\n") exit() while True: wpid = wait() if wpid < 0 or wpid == pid: break print(1, "zombie!\n")
def forktest(): printf(1, "fork test\n") for n in range(1000): pid = fork() if pid < 0: break if pid == 0: exit_() if n == 1000: printf(1, "fork claimed to work 1000 times!\n") exit_() while n > 0: if wait() < 0: printf(1, "wait stopped early\n") exit_(1) n -= 1 if wait() != -1: printf(1, "wait got too many\n") exit_(1) printf(1, "fork test OK\n")
def main(argc, argv): if fork() > 0: sleep(5) # Let child exit before parent exit_()
def fork1(func, *argv): pid = fork(func, *argv) if pid == -1: panic("fork") return pid