=)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, ¶m);
//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, ¶m);
pthread_setschedparam(thread1_id, SCHED_OTHER, ¶m);
//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, ¶m);
pthread_setschedparam(thread2_id, SCHED_OTHER, ¶m);
//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에 새로운 오전 ++ 내를 영어는별로 좋지는 않지만 이것이 왜 효과가 없는지 이해하려고 노력하고 싶습니다. 어떤 도움도 환영합니다!
'gdb [yourprogram]'을 실행하고 'r'을 누르십시오. 그러면 정확히 어디서 무엇이 잘못되었는지 알려야합니다. '-g' 옵션으로 프로그램을 컴파일해야 할 수도 있습니다. [gdb tutorial] (http://www.cs.cmu.edu/~gilpin/tutorial/)을 참조하십시오. (오류 메시지에서 나는 gcc를 사용하여 유닉스에 있다고 결론을 내렸다. 그렇다면 사용하고있는 것을 명시해라.) – nwp
@nwp 당신의 도움에 감사드립니다! 우분투의 CodeBlocks에서이 코드를 실행하고 있습니다! 그것은 모든 작업 =)하지만 CodeBlocks를 사용하지 않았다면, 나는 같은 것을 할 것입니다 ... 컴파일 : gcc -c nchars.c 실행 파일 : gcc -o nchars -lpthread nchars.o 일반적으로 사용하는 것으로 봅니다. (교수에 따르면 .........) – user3559945