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

Replace prak4NEW.cpp

parent 2e779f1a
......@@ -11,15 +11,16 @@
#include <unistd.h>
using namespace std;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int m,n,r,k,t;
int sumDoener = 0;
int m,n,r,k,t;//input parameter
int sumDoener = 0;// total doners are sold
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
sem_t semaphoreVerkauferReihe; //um zu kontrollieren , die genaue reihe von verkaufer create , vermeiden race condition
sem_t sem0;sem_t sem1;sem_t sem2;sem_t sem3;sem_t sem4;sem_t sem5;sem_t sem6;sem_t sem7;
std::vector<sem_t> listSem;
sem_t sem0;sem_t sem1;sem_t sem2;sem_t sem3;sem_t sem4;sem_t sem5;sem_t sem6;sem_t sem7;// es gibt immer Fehler wenn ich mit dynamische Erzeugung arbeitet
std::vector<sem_t> listSem;// here asume we ,that maximal 8 verkaufer gibt , es gibt nur 4 zutaten, deshalb ist 4 verkaufer ist ideale Arbeitumgebung
// every CS has a mutex
pthread_mutex_t Sauce= PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t Brot= PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t Salat= PTHREAD_MUTEX_INITIALIZER;
......@@ -29,10 +30,10 @@ int nSauce,nBrot,nSalat,nFleisch;
bool nichtFertig = true;
std::vector<bool> vectorCondition;
std::vector<std::vector<int>> vectorKundenBestellung;
std::vector<std::vector<int>> vectorKundenBestellung;// save , which verkaufer has done which Order
std::vector<pthread_mutex_t> listMutexVerkaufen;
std::vector<pthread_mutex_t> listMutexMakeDoner;
void *refill(void * time){
int wait = ((long)time)*t ;
printf("refill in %ld second \n",(long)time);
......@@ -123,10 +124,10 @@ void getFleisch(int anzahl,int time){
pthread_mutex_unlock(&Fleisch);
}
void *verkaufen(void *tid) {
printf("creat verkauferrrrr%ld\n",(long)tid);
sem_post(&semaphoreVerkauferReihe);
while (nichtFertig) {
while (!vectorCondition[(long)tid]) {
while (!vectorCondition[(long)tid]) { // that can we use semaphore instead
int max = 3;
int num;
for (int c = 0; c <= max; c++){
......@@ -150,12 +151,13 @@ void init_verkaufer(int m){
pthread_t verkaufer_threads[m];
int status;
long i;
listMutexVerkaufen.resize(m);listMutexMakeDoner.resize(m);
listMutexVerkaufen.resize(m);
for (i=0;i<m;i++) {
listMutexVerkaufen[i]= PTHREAD_MUTEX_INITIALIZER;
listMutexMakeDoner[i]= PTHREAD_MUTEX_INITIALIZER;
vectorCondition.push_back(0);
sem_wait(&semaphoreVerkauferReihe);
status = pthread_create(&verkaufer_threads[i],NULL,verkaufen,(void*)i);
if (status!=0) {
printf("error creating thread %ld\n",i);
......@@ -166,10 +168,11 @@ void init_verkaufer(int m){
}
void *kunden(void *tid) {
sem_post(&semaphoreKundenReihe);
//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
sem_post(&semaphoreKundenReihe);
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
......@@ -182,10 +185,11 @@ for (int i=0;i<m;i++) {
}
pthread_exit(NULL);
}
// es gibt Bug , das verkauferthread 0 ist niemal gerufen NUR MANCH MAL
void init_kunden(int k){
int status;
pthread_t kunden_threads[k];
auto start = chrono::steady_clock::now();
auto start = chrono::steady_clock::now();
for (long i=0;i<k;i++) {
......@@ -199,18 +203,20 @@ void init_kunden(int k){
}//if
}
for (int i=0;i<k;i++) {
if (i == k-1){
pthread_join(kunden_threads[i], NULL);// wait all kunden finish
auto end = chrono::steady_clock::now();
auto diff = end - start;
auto diff_sec = chrono::duration_cast<chrono::seconds>(diff);
std::cout << diff_sec.count() << ";" <<sumDoener << std::endl;
std::fstream out;
out.open("time.txt",std::ios::out|std::ios::app);
out << sumDoener << ";" << diff_sec.count() << "\n";
out.close();
nichtFertig = false;
}
pthread_join(kunden_threads[i], NULL);// wait all kunden finish
if (i == k-1){
//pthread_join(kunden_threads[i], NULL);// wait all kunden finish
// caculate time
auto end = chrono::steady_clock::now();
auto diff = end - start;
auto diff_sec = chrono::duration_cast<chrono::seconds>(diff);
std::cout << diff_sec.count() << ";" <<sumDoener << std::endl;
std::fstream out;
out.open("time.txt",std::ios::out|std::ios::app);
out << sumDoener << ";" << diff_sec.count() << "\n"; // save in text for caculating
out.close();
nichtFertig = false;// to end void* verkaufe
}
}
std::cout << "ENDDDDDDDDD" << std::endl;
}
......@@ -222,11 +228,13 @@ int main(int argc,char* argv[]) {
// t = 50000;
// n = 5;
// r = 5;
//init everfin
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;
sem_init(&sem0, 0, 0);sem_init(&sem1, 0, 0);sem_init(&sem2, 0, 0);sem_init(&sem3, 0, 0);sem_init(&sem4, 0, 0);
sem_init(&sem0, 0, 0);sem_init(&sem1, 0, 0);sem_init(&sem2, 0, 0);sem_init(&sem3, 0, 0);sem_init(&sem4, 0, 0); //init sem
listSem.push_back(sem0);listSem.push_back(sem1);listSem.push_back(sem2);listSem.push_back(sem3);listSem.push_back(sem4);
sem_init(&semaphoreVerkauferReihe, 0, 1);
init_verkaufer(m);
sem_init(&semaphoreVerkaufer, 0, m);
sem_init(&semaphoreKundenReihe, 0, 1);
......
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