=)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 ...» и т.д.
Цель состоит в том, чтобы получите больше «о», пока он не закончится.
Что я сделал:
#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/). (Из сообщения об ошибке я пришел к выводу, что вы используете Unix с помощью gcc, если нет, то укажите, что вы используете.) – nwp
@nwp Спасибо за вашу помощь! Я запускаю этот код на CodeBlocks, на Ubuntu! Он делает все, что работает =) Но если бы я не использовал CodeBlocks, я бы сделал что-то вроде ... Скомпилировать: gcc -c nchars.c Исполняемый файл: gcc -o nchars -lpthread nchars.o Обычно он использует то, что я вижу (по словам профессора .........) – user3559945