2014-04-22 2 views
0

=)C++ 멀티 스레드 우선 순위 구현 내가 여기에 새로운 사용자 오전, 나는 C++에 새로운 오전, 그래서 내가 그것을 작동하는 것이 조금 어렵

실패 ... 그래서 내가 당신을 요구하고있다 얘들 아 몇 가지 질문! =)

내가 나를 이것으로 스레드 우선 순위를 구현하기 위해 요청 학교에 대한 작업, 일을 오전 :

이 제공
#include <pthread.h> 
#include <stdio.h> 
#include <sched.h> 

int sched_yield(void); 

// Parameters to print_function. 

struct char_print_parms{ 
    char character; // char to print 
    int count; // times to print 
}; 

void* char_print (void* parameters){ 
    int i; 
    struct char_print_parms* p; 

    p = (struct char_print_parms*) parameters; 
    for (i = 0; i < p->count; ++i){ 
     fputc (p->character, stderr); 
     sched_yield(); 
    } 
    return NULL; 

} 

int main(){ 

    pthread_t thread1_id,thread2_id; 
    struct char_print_parms thread1_args,thread2_args; 

// Create a new thread to print 200 x's. 

    thread1_args.character = 'x'; 
    thread1_args.count = 200; 
    pthread_create (&thread1_id, NULL, &char_print, &thread1_args); 

// Create a new thread to print 200 o's. 

    thread2_args.character = 'o'; 
    thread2_args.count = 200; 
    pthread_create (&thread2_id, NULL, 
    &char_print, &thread2_args); 

// main waits for the threads to complete 

    pthread_join(thread1_id, NULL); 
    pthread_join(thread2_id, NULL); 

    return 0; 

} 

이다 "oxoxoxo ..."등

목적은하는 것입니다 그것이 끝날 때까지 더 "o"를 얻으십시오. 내가 무슨 짓을

했다 : 끝에

#include <pthread.h> 
#include <stdio.h> 
#include <sched.h> 

int sched_yield(void); 

// Parameters to print_function. 

struct char_print_parms{ 
    char character; // char to print 
    int count; // times to print 
}; 

void* char_print (void* parameters){ 
    int i; 
    struct char_print_parms* p; 

    p = (struct char_print_parms*) parameters; 
    for (i = 0; i < p->count; ++i){ 
     fputc (p->character, stderr); 
     sched_yield(); 
    } 
    return NULL; 

} 

int main(){ 

    pthread_t thread1_id,thread2_id; 
    struct char_print_parms thread1_args,thread2_args; 


    //new code lines 
    struct sched_param param; 
    pthread_attr_t pta; 
    pthread_attr_init(&pta); 
    pthread_attr_getschedparam(&pta, &param); 
    //end of new code lines 

// Create a new thread to print 200 x's. 

    thread1_args.character = 'x'; 
    thread1_args.count = 200; 

    //more new code lines 
    param.sched_priority = 0; 
    pthread_attr_setschedparam(&pta, &param); 
    pthread_setschedparam(thread1_id, SCHED_OTHER, &param); 
    //end of more new code lines 

    pthread_create (&thread1_id, NULL, &char_print, &thread1_args); 



// Create a new thread to print 200 o's. 

    thread2_args.character = 'o'; 
    thread2_args.count = 200; 

    //more new code lines 2 
    param.sched_priority = 10; 
    pthread_attr_setschedparam(&pta, &param); 
    pthread_setschedparam(thread2_id, SCHED_OTHER, &param); 
    //end of more new code lines 2 

    pthread_create (&thread2_id, NULL, 
    &char_print, &thread2_args); 

// main waits for the threads to complete 

    pthread_join(thread1_id, NULL); 
    pthread_join(thread2_id, NULL); 

    return 0; 

} 

, 내가 컴파일하고 실행하려고하지만 오류가 나타납니다 다시 한번

Segmentation failed (core dumped)

을, 나는 C에 새로운 오전 ++ 내를 영어는별로 좋지는 않지만 이것이 왜 효과가 없는지 이해하려고 노력하고 싶습니다. 어떤 도움도 환영합니다!

+0

'gdb [yourprogram]'을 실행하고 'r'을 누르십시오. 그러면 정확히 어디서 무엇이 잘못되었는지 알려야합니다. '-g' 옵션으로 프로그램을 컴파일해야 할 수도 있습니다. [gdb tutorial] (http://www.cs.cmu.edu/~gilpin/tutorial/)을 참조하십시오. (오류 메시지에서 나는 gcc를 사용하여 유닉스에 있다고 결론을 내렸다. 그렇다면 사용하고있는 것을 명시해라.) – nwp

+0

@nwp 당신의 도움에 감사드립니다! 우분투의 CodeBlocks에서이 코드를 실행하고 있습니다! 그것은 모든 작업 =)하지만 CodeBlocks를 사용하지 않았다면, 나는 같은 것을 할 것입니다 ... 컴파일 : gcc -c nchars.c 실행 파일 : gcc -o nchars -lpthread nchars.o 일반적으로 사용하는 것으로 봅니다. (교수에 따르면 .........) – user3559945

답변

0

pthread_setschedparam을 호출하면 스레드 ID 변수가 아직 초기화되지 않았습니다. 따라서 미확인 스레드에서 매개 변수를 변경하려고합니다.

우선 순위를 변경하는 가장 쉬운 방법은 스레드 자체에서 우선 순위를 변경하는 것입니다.


초기화되지 않은 로컬 변수는 명시 적으로 초기화 될 때까지 해당 값이 불확정합니다. 초기화되지 않은 지역 변수를 사용하면 undefined behavior이됩니다.

pthread_setschedparam에있는 예제를 보면 pthread_self으로 호출되어 자신의 스레드 우선 순위를 설정하는 것을 볼 수 있습니다. 이것을 사용하여 우선 순위를 포함하는 스레드에 전달하는 구조체의 필드를 추가하거나 우선 순위를 설정하고 실제 스레드 함수를 호출하는 래퍼 스레드 함수를 가질 수 있습니다.

+0

당신의 도움에 감사드립니다 요아킴! =) 나는 당신이 의미하는 바를 이해했다고 생각합니다. 따라서 코드 순서를 변경하여 해당 부분을 스레드 생성 아래에 두었습니다. 하지만 "우선 순위를 변경하는 가장 쉬운 방법은 스레드 자체에서 우선 순위를 변경하는 것입니다.", 그게 내가 0을 이해한다는 것을 깨닫게했다 ... LOL – user3559945

+0

@ user3559945 몇 가지 힌트가있는 답변이 업데이트 됨. –

0

먼저 pthread_create (&thread1_id, NULL, &char_print, &thread1_args);을 호출하여 thread1_id 스레드를 생성 한 다음이 스레드의 우선 순위를 설정할 수 있습니다. 나는 코드를 수정하고 잘 동작한다.

thread1_args.character = 'x'; 
thread1_args.count = 200; 
pthread_create (&thread1_id, NULL, &char_print, &thread1_args); 

//more new code lines 
param.sched_priority = 0; 
pthread_attr_setschedparam(&pta, &param); 
pthread_setschedparam(thread1_id, SCHED_OTHER, &param); 

// Create a new thread to print 200 o's. 
thread2_args.character = 'o'; 
thread2_args.count = 200; 
pthread_create (&thread2_id, NULL, &char_print, &thread2_args); 

//more new code lines 2 
param.sched_priority = 10; 
pthread_attr_setschedparam(&pta, &param); 
pthread_setschedparam(thread2_id, SCHED_OTHER, &param); 

이 링크는 https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_MRG/2/html/Realtime_Reference_Guide/chap-Realtime_Reference_Guide-Priorities_and_policies.html입니다.

이 코드를 테스트하지만 출력은 매번 다릅니다.

+0

시간과 답장을 보내 주셔서 감사합니다!) 시도했지만 결과는 이전과 동일합니다. – user3559945

+0

@ user3559945 답변을 업데이트합니다 : – BlackMamba

+0

친구에게 감사드립니다. 테스트했지만 다시 한 번 결과가 항상 동일하게 나타납니다. 내가 잘못 생각한 것 같습니까? – user3559945