-1
void cleanupHandler(void *arg) { 
    printf("In the cleanup handler\n");
}
void *Thread(void *string) { 
    int i;
    int o_state;
    int o_type;
    pthread_cleanup_push(cleanupHandler, NULL);
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &o_state);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &o_type);
    puts("1 Hello World");
    pthread_setcancelstate(o_state, &o_state);
    puts("2 Hello World");
    pthread_cleanup_pop(0);
    pthread_exit(NULL);
}
int main() { 
    pthread_t th;
    int rc;
    rc = pthread_create(&th, NULL, Thread, NULL);
    pthread_cancel(th);
    pthread_exit(NULL);
}

このコードの出力がどうなるか、どのような順序で発生するかを考えていました。はい、これは 6 時間後に行う試験の練習問題です。どんな助けでも大歓迎です。私の大学のすべての TA は各自の最終試験で忙しいため、今日はオフィス アワーはありません。

ありがとう

4

2 に答える 2

0

Here are the man pages you need to understand the problem they will be putting on the exam (which most certainly won't be the exact problem above.) So you need to understand what each of those functions does.

In this question (and presumably the similar one that will be on your exam), you need to enumerate all possible interleavings of calls to these functions between the two threads.

于 2013-05-14T15:31:47.887 に答える
-2
1 Hello World
2 Hello World

コンパイルして実行しないのはなぜですか?このバージョンはコンパイルして実行します。

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void cleanupHandler(void *arg) {
    printf("In the cleanup handler\n");
}
void* Thread(void *string) {
    int i;
    int o_state;
    int o_type;
    sleep(1);
    pthread_cleanup_push(cleanupHandler, NULL);

    sleep(1);//Note that when you uncomment lines, you should uncomment them in order.
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &o_state);
    sleep(1);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &o_type);
    puts("1 Hello World");
    sleep(1);
    pthread_setcancelstate(o_state, &o_state);
    sleep(1);
    puts("2 Hello World");
    pthread_cleanup_pop(0);
    pthread_exit(NULL);
}

int main() {
    pthread_t th;
    int rc;
    rc = pthread_create(&th, NULL, Thread, NULL);
    pthread_cancel(th);
    pthread_exit(NULL);
}
于 2013-05-14T15:28:31.493 に答える