Commit c2ff7c0b authored by Dong Hung Pham's avatar Dong Hung Pham 🤡
Browse files

p4

parent e54c009a
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <vector>
#include <time.h>
#include <semaphore.h>
#include <unistd.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int m,n,r,k,t;
sem_t semaphoreVerkaufer; // wenn es mindest eine freie Verkaufer gibt , darf die Kunden bestellen
sem_t semaphoreKundenReihe; //um zu kontrollieren , die genaue reihe von Kunden werden bestellen , vermeiden race condition
//pthread_mutex_t listMutexVerkaufen[3];
//pthread_mutex_t listMutexMakeDoner[3];
//pthread_cond_t listCondVerkaufer[3];
//pthread_cond_t listCondKunden[14];
pthread_mutex_t Sauce= PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t Brot= PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t Salat= PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t Fleisch= PTHREAD_MUTEX_INITIALIZER;
int nSauce,nBrot,nSalat,nFleisch;
bool nichtFertig = true;
std::vector<bool> vectorCondition;
std::vector<std::vector<int>> vectorKundenBestellung;
std::vector<pthread_mutex_t> listMutexVerkaufen;
std::vector<pthread_mutex_t> listMutexMakeDoner;
std::vector<pthread_cond_t> listCondVerkaufer;
std::vector<pthread_cond_t> listCondKunden;
void *refill(void * time){
int wait = ((long)time)*t ;
printf("refill in %ld second \n",(long)time);
usleep(wait);
pthread_exit(NULL);
}
void getSauce(int anzahl,int time){
pthread_mutex_lock(&Sauce);
int count = 0;
while (count != anzahl) {
//printf("make a sauce \n");
if (nSauce == 0){
//printf("need refill sauce \n");
long rate = n/r;
pthread_t refillThread;
int status = pthread_create(&refillThread,NULL,refill,(void*)rate);
pthread_join(refillThread, NULL);
nSauce = n;
}
count++;
usleep(time);
nSauce--;
}
printf("fertig sauce \n");
pthread_mutex_unlock(&Sauce);
}
void getBrot(int anzahl,int time){
pthread_mutex_lock(&Brot);
int count = 0;
while (count != anzahl) {
//printf("make a brot \n");
if (nBrot == 0){
//printf("need refill brot \n");
long rate = n/r;
pthread_t refillThread;
int status = pthread_create(&refillThread,NULL,refill,(void*)rate);
pthread_join(refillThread, NULL);
nBrot = n;
}
count++;
usleep(time);
nBrot--;
}
printf("fertig brot \n");
pthread_mutex_unlock(&Brot);
}
void getSalat(int anzahl,int time){
pthread_mutex_lock(&Salat);
int count = 0;
while (count != anzahl) {
//printf("make a salat \n");
if (nSalat == 0){
//printf("need refill salat \n");
long rate = n/r;
pthread_t refillThread;
int status = pthread_create(&refillThread,NULL,refill,(void*)rate);
pthread_join(refillThread, NULL);
nSalat = n;
}
count++;
usleep(time);
nSalat--;
}
printf("fertig salat \n");
pthread_mutex_unlock(&Salat);
}
void getFleisch(int anzahl,int time){
pthread_mutex_lock(&Fleisch);
int count = 0;
while (count != anzahl) {
// printf("make a ffleisch \n");
if (nFleisch == 0){
//printf("need refill fleisch \n");
long rate = n/r;
pthread_t refillThread;
int status = pthread_create(&refillThread,NULL,refill,(void*)rate);
pthread_join(refillThread, NULL);
nFleisch = n;
}
count++;
usleep(time);
nFleisch--;
}
printf("fertig fleisch \n");
pthread_mutex_unlock(&Fleisch);
}
void *verkaufen(void *tid) {
while (nichtFertig) {
while (!vectorCondition[(long)tid]) { // co the co loi o daz
pthread_mutex_lock(& listMutexMakeDoner[(long)tid]);
pthread_cond_wait(&listCondVerkaufer[(long)tid], &listMutexVerkaufen[(long)tid]); //wait a kunde come and wakeup this thread
int max = 3;
int num;
for (int c = 0; c <= max; c++){
num = random()%10;
}
//sleep((long)num);
//printf("time: %ld\n",(long)num);
getFleisch(num,t);getBrot(num,t);getSalat(num,t);getSauce(num,t);
vectorCondition[(long)tid] = 0;
printf("Doener fertig verkaufer Thread No: %ld fuer kunde :%d , with time%ld \n ",(long)tid,vectorKundenBestellung[(long)tid].back(),(long)num);
pthread_cond_signal(&listCondKunden[vectorKundenBestellung[(long)tid].back()]);
pthread_mutex_unlock(& listMutexMakeDoner[(long)tid]);
}
}
pthread_exit(NULL);
}
void init_verkaufer(int m){
vectorKundenBestellung.resize(m); // save id der Kunden , vector«1» ist verkaufer 1, die neuste bestellung ist vector«1».back()
pthread_t verkaufer_threads[m];
int status;
long i;
listMutexVerkaufen.resize(m);listMutexMakeDoner.resize(m);listCondVerkaufer.resize(m);
for (i=0;i<m;i++) {
listMutexVerkaufen[i]= PTHREAD_MUTEX_INITIALIZER;
listMutexMakeDoner[i]= PTHREAD_MUTEX_INITIALIZER;
listCondVerkaufer[i] = PTHREAD_COND_INITIALIZER;
// listMutexVerkaufen.push_back(PTHREAD_MUTEX_INITIALIZER);
// listMutexMakeDoner.push_back(PTHREAD_MUTEX_INITIALIZER);
// listCondVerkaufer.push_back(PTHREAD_COND_INITIALIZER);
vectorCondition.push_back(0);
status = pthread_create(&verkaufer_threads[i],NULL,verkaufen,(void*)i);
if (status!=0) {
printf("error creating thread %ld\n",i);
exit(-1);
}//if
}// for
}
void *kunden(void *tid) {
sem_post(&semaphoreKundenReihe);
// do something
for (int i=0;i<m;i++) {
if (pthread_mutex_trylock(& listMutexVerkaufen[i]) == 0){ // try to lock a verkaufer thread , if sucess, then lock it
printf("kunden Thread No: %ld\n",(long)tid);
vectorKundenBestellung[i].push_back((long)tid);//long(tid) kunde come and orde at (i) verkaufer
vectorCondition[i] = 1;//to announce , that thread verkaufer (i) got a job
pthread_cond_signal(&listCondVerkaufer[i]);//wake up verkaufer i
pthread_cond_wait(&listCondKunden[(long)tid], &listMutexVerkaufen[i]);// wait the verkaufer wake up till finish
pthread_mutex_unlock(& listMutexVerkaufen[i]); //unlock this thread
sem_post(&semaphoreVerkaufer);// sem_post , ready for next kunden
break;
}
}
pthread_exit(NULL);
}
void init_kunden(int k){
int status;
listCondKunden.resize(k);
pthread_t kunden_threads[k];
for (long i=0;i<k;i++) {
listCondKunden[i] = PTHREAD_COND_INITIALIZER;
//listCondKunden.push_back(PTHREAD_COND_INITIALIZER);
sem_wait(&semaphoreKundenReihe);// wait in reihe, darf nur maximal 1 person jede mal gehen, um condtion race zu vermeiden
sem_wait(&semaphoreVerkaufer);// wait in reihe, darf 1 person gehen, wenn es einen freie verkaufer gibt
status = pthread_create(&kunden_threads[i],NULL,kunden,(void*)i);
if (status!=0) {
printf("error creating thread %ld\n",i);
exit(-1);
}//if
}
for (int i=0;i<k;i++) {
pthread_join(kunden_threads[i], NULL);// wait all kunden finish
}
nichtFertig = false;
}
//imbiss <m> <n> <r> <k> <t>
// g++ 2.cpp -o 2 -pthread
int main(int argc,char* argv[]) {
// k = 14;m=3;
// t = 50000;
// n = 5;
// r = 5;
m = atoi(argv[1]); n = atoi(argv[2]); r = atoi(argv[3]); k = atoi(argv[4]);t =atoi(argv[5]) ;
printf("check %d\n",m);
nSauce = n;nBrot =n;nFleisch=n;nSalat = n;
init_verkaufer(m);
sem_init(&semaphoreVerkaufer, 0, m);
sem_init(&semaphoreKundenReihe, 0, 1);
init_kunden(k);
return 0;
}//main
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment