Cześć,
Mam mały problem, aczkolwiek chce napisać program z wykorzystaniem wątków i biblioteki pthreads.
Szczegół w tym, że wymyśliłem sobie takie zadanie:
"Mamy 10 profesorów i niekończącą się liczbę studentów, każdy student musi odwiedzić czterech różnych prowadzących by zebrać od nich wpis do indexu, maxymalnie może chodzić 10 studentów i jak liczba ich się zmniejsza to na miejsce tego, który zebrał już wpis wskakuje nowy. Oczywistym jest, ze u jednego profesora aktualnie może być jeden student odbierający wpis. Jeżeli student natrafi na zajętego profesora to idzie do kolejnego by nie czekać a jak już musi czekać to czeka aż się zwolni."
Moim problemem jest to, że wymyśliłem sobie zadanie próbuję się za nie zabrać od godziny 16 (dnia 06.01) i nie wiem jak mam to zrobić.
Nie widzę odwzorowania w wątkach...
Student czy profesor jest wątkiem ? raz tak mi pasuje a raz tak. a najlepiej by było jak by byli nim obaj... tylko jak w tedy mieć nie ograniczoną liczbę studentów ?
na chwilę obecną mam coś takiego:
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
extern "C"
{
#include <pthread.h>
}
using namespace std;
struct index //deklaracja struktury
{
double przedmiot1;
double przedmiot2;
double przedmiot3;
double przedmiot4;
};
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condition_var = PTHREAD_COND_INITIALIZER;
void * Student(void * arg);
void * Profesor(void * arg);
int b; /* buffer size = 1; */
int turn=0;
int st_online = 0; //ilosc studentow zbierajacych wpisy
int main()
{
srand (time(NULL));
pthread_t student_1,student_2,student_3,student_4,student_5,student_6,student_7,student_8,student_9;
index notes=
{
1,
3,
4,
1
};
//Tworzenie watkow studentow (ktorzy bd zbierac pisy)
pthread_create( &student_1, NULL, &Student, ¬es);
pthread_create( &student_2, NULL, &Student, ¬es);
pthread_create( &student_3, NULL, &Student, ¬es);
pthread_create( &student_4, NULL, &Student, ¬es);
pthread_create( &student_5, NULL, &Student, ¬es);
pthread_create( &student_6, NULL, &Student, ¬es);
pthread_create( &student_7, NULL, &Student, ¬es);
pthread_create( &student_8, NULL, &Student, ¬es);
pthread_create( &student_9, NULL, &Student, ¬es);
pthread_join( student_1, NULL);
cout << "Koniec Programu " << endl;
return 0;
}
void add_buffer(int i){
st_online = i;
}
int get_buffer(){
return st_online ;
}
void Profesor(index &a)
{
a=
{
(rand() % 6 + 2),
(rand() % 6 + 2),
(rand() % 6 + 2),
(rand() % 6 + 2)
}; cout <<"Profesor mode";
}
void *Student(void * arg)
{
add_buffer(1);
index notes_studenta;
notes_studenta=*((index*)arg);
pthread_mutex_lock( &count_mutex );
Profesor(notes_studenta);
//pthread_mutex_lock( &count_mutex );
//pthread_mutex_unlock( &count_mutex );
cout <<"I'm a student\n";
cout << notes_studenta.przedmiot1 << endl;
pthread_mutex_unlock( &count_mutex );
pthread_exit(NULL);
}
Jakieś pomysły jakieś rady ?
co kol wiek... Jak wy to widzicie kto jest wątkiem a kto czym ?
Jak to odwzorować w wątkach ?