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

Add new file

parent 1d77dd6d
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <vector>
#include <time.h>
#include <semaphore.h>
#include <chrono>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <queue>
using namespace std;
int m,n,r,k,t;//input parameter
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;
//pthread_mutex_t kunde= PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t verkaufer= PTHREAD_MUTEX_INITIALIZER;
int nSauce,nBrot,nSalat,nFleisch;
sem_t semaphoreKundenReihe; //um zu kontrollieren , die genaue reihe von Kunden werden bestellen , vermeiden race condition
sem_t semaphoreVerkauferReihe; // wenn es mindest eine freie Verkaufer gibt , darf die Kunden bestellen
sem_t semaphoreVerkaufer; // wenn es mindest eine freie Verkaufer gibt , darf die Kunden bestellen
struct Bestellung{
int id;
int anzahl;
sem_t *pSem;
};
queue<Bestellung> warteSchlange;
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 *kunden(void *tid) {
//sem_post(&semaphoreKundenReihe);
// do something
sem_t sema;
sem_init(&sema, 0, 0);
Bestellung bs;
bs.id = (long)tid;
bs.pSem = &sema;
bs.anzahl = random()%10 +1;
warteSchlange.push(bs);
sem_post(&semaphoreKundenReihe);
sem_post(&semaphoreVerkaufer);
sem_wait(bs.pSem);
printf("Doener fertig fuer kunde :%d \n ",(long)tid);
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];
for (long i=0;i<k;i++) {
//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
printf(" creating thread %ld\n",i);
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
}
}
void *verkaufen(void *tid) {
printf("creat verkaufer %ld\n",(long)tid);
sem_post(&semaphoreVerkauferReihe);
while (1) {
//while(warteSchlange.empty());
sem_wait(&semaphoreVerkaufer);
pthread_mutex_lock(&verkaufer);
Bestellung myOrder = warteSchlange.front();
warteSchlange.pop();
pthread_mutex_unlock(&verkaufer);
int num = myOrder.anzahl;
getFleisch(num,t);getBrot(num,t);getSalat(num,t);getSauce(num,t);
printf("Doener fertig !!! \n ");
printf("Verkaufer Nr %d fertig gemacht fuer kunde id :%d \n ",(long)tid,myOrder.id);
sem_post(myOrder.pSem);
}
pthread_exit(NULL);
}
void init_verkaufer(int m){
pthread_t verkaufer_threads[m];
for (int i=0;i<m;i++) {
printf("waiting\n");
sem_wait(&semaphoreVerkauferReihe);
int status = pthread_create(&verkaufer_threads[i],NULL,verkaufen,(void*)i);
if (status!=0) {
printf("error creating thread %ld\n",i);
exit(-1);
}//if
}// for
}
//int main(int argc,char* argv[]) {
int main() {
// k = 14;m=3;
// 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]) ;
m = 5; n = 5; r = 5; k =25; t = 40000;
nSauce = n;nBrot =n;nFleisch=n;nSalat = n;
sem_init(&semaphoreVerkauferReihe, 0, 1);
sem_init(&semaphoreKundenReihe, 0, 1);
sem_init(&semaphoreVerkaufer, 0, 0);
init_verkaufer(m);
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