자식 프로세스 완료 대기

Beginner

This tutorial is from open-source community. Access the source code

소개

이 실습에서는 process::Child가 종료될 때까지 기다리는 방법을 배울 수 있습니다. Child::wait를 호출하면 process::ExitStatus를 반환합니다.

참고: 실습에서 파일 이름을 지정하지 않으면 원하는 파일 이름을 사용할 수 있습니다. 예를 들어 main.rs를 사용하고 rustc main.rs && ./main으로 컴파일 및 실행할 수 있습니다.

대기

process::Child가 종료될 때까지 기다리려면 Child::wait를 호출해야 합니다. 이 함수는 process::ExitStatus를 반환합니다.

use std::process::Command;

fn main() {
    let mut child = Command::new("sleep").arg("5").spawn().unwrap();
    let _result = child.wait().unwrap();

    println!("reached end of main");
}
$ rustc wait.rs && ./wait
## `wait`는 `sleep 5` 명령이 완료될 때까지 5 초 동안 실행됩니다.
reached end of main

요약

축하합니다! 대기 실습을 완료했습니다. LabEx 에서 더 많은 실습을 통해 기술을 향상시킬 수 있습니다.