2014-09-26 1 views
0

내 프로세스가 실행 완료되면 pthread_cancel을 사용하여 "타이머"스레드를 취소하고 싶습니다. 기본적으로 수정 된 터미널을 프로젝트로 만들고 있습니다. 한 터미널에서 sleep 300을 호출 할 때 다른 터미널을 열고 같은 프로그램을 실행하지만 "pkill -HUP sleep"을 호출하고 절전 호출과 타이머를 취소 할 수 있기를 원합니다. 그러나 타이머는 현재 솔루션으로 계속 실행됩니다.2 개의 실행중인 프로세스가있는 pthread_cancel

else if (pid > 0){ 
    pthread_t thread; 
    int threadError = pthread_create(&thread, NULL, timer, NULL); 
    if (threadError != 0){ 
     exit(1); 
    } 
    int status = 0; 
    wait(&status); 
    if (WIFEXITED(status)){ 
    // how do I change this to kill the other thread? 
     threadError = pthread_cancel(thread); 
     if (threadError != 0) 
     exit(1); 
     printf("Child exited with code %d\n", WEXITSTATUS(status)); 
    } 
    else{ // child 
    // run command and capture error with errorNumber 
    int errorNumber = execvp(argv[0], argv); 
    if (errorNumber == -1){ 
     logerror(5, "execvp", "Error running command."); 
     exit(1); 
    } 
    } 

타이머 기능도 포함합니다.

void * timer(void * nothing){ 
    //tracks seconds of execution 
    time_t start = time(NULL); 
    for(;;){ 
    sleep(UPDATETIME); //constant set to 1 
    printf("Elapsed time: %ld seconds\n", time(NULL) - start); 
    } 
    pthread_exit(0); 
} 

나는 pthread_cancel에 대한 매뉴얼 페이지 쏟아져 봤는데이 프로그램의 새 인스턴스가 실행중인 경우이 스레드를 취소 할 수있는 방법을 찾을 수 없습니다.

편집 : 사실이 pkill 명령이 pthread_cancel에 문제가있는 대신 내 자식 프로세스를 올바르게 종료하지 않는다고 가정합니다. 위 내용을 수정하여 내 자녀 과정을 포함시킵니다.

답변

0

pthread_join(); pthread_cancel()보다 낫다. 설명서 및 예제를 확인하십시오.

관련 문제