Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.0 kB
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <semaphore.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>

int teamAnumber;
int teamBnumber;
int countA = 0;
int countB = 0;
int count = 0;
int drivercount = 0;
int teama = 1;
int teamb = 2;
bool ifAempty = false;
bool ifBempty = false;
pthread_barrier_t barrier;
pthread_barrier_t barrierA;
pthread_barrier_t barrierB;
pthread_mutex_t lock;
//pthread_mutex_t lockB;
sem_t semA;
sem_t semB;
sem_t semC;
sem_t semD;

void* thread_func(void* arg){
    int* args = (int*) arg;
    int teamindicator = *args;


    if(teamindicator == 1){
        pthread_mutex_lock(&lock);
        //sem_wait(&semC);
        countA++;
        if(countA == 2 && countB == 2){
            sem_post(&semA);
            sem_post(&semA);
            sem_post(&semB);
            sem_post(&semB);
            countA -= 2;
            countB -= 2;
        }
        else if(countA == 4){
            sem_post(&semA);
            sem_post(&semA);
            sem_post(&semA);
            sem_post(&semA);
            countA -= 4;
        }
        else if(countB == 4){
            sem_post(&semB);
            sem_post(&semB);
            sem_post(&semB);
            sem_post(&semB);
            countB -= 4;
        }
        pthread_mutex_unlock(&lock);
        //sem_post(&semC);
        printf("Thread ID: %ld , Team: A, I am currently looking for a car\n", pthread_self());
        //pthread_barrier_wait(&)
        sem_wait(&semA);
    }

    if(teamindicator == 2){
        pthread_mutex_lock(&lock);
        //sem_wait(&semD);
        countB++;
        if(countA == 2 && countB == 2){
            sem_post(&semA);
            sem_post(&semA);
            sem_post(&semB);
            sem_post(&semB);
            countA -= 2;
            countB -= 2;
        }
        else if(countB == 4){
            sem_post(&semB);
            sem_post(&semB);
            sem_post(&semB);
            sem_post(&semB);
            countB -= 4;
        }
        else if(countA == 4){
            sem_post(&semA);
            sem_post(&semA);
            sem_post(&semA);
            sem_post(&semA);
            countA -= 4;
        }
        pthread_mutex_unlock(&lock);
        //sem_post(&semD);
        printf("Thread ID: %ld , Team: B, I am currently looking for a car\n", pthread_self());
        sem_wait(&semB);
    }

    if(teamindicator == 1){
        //countA--;
        printf("Thread ID: %ld , Team: A, I have found a spot on the car\n", pthread_self());
        drivercount++;
        if(drivercount % 4 == 0 && drivercount != 0){
            printf("Thread ID: %ld , Team: A, I am the captain and driving the car\n", pthread_self());
        }
        /*
        if(countB != 0){j
            sem_post(&semB);
        }
        else if(countA != 0){
            sem_post(&semA);
        }
        */
    }

    if(teamindicator == 2){
        //countB--;
        printf("Thread ID: %ld , Team: B, I have found a spot on the car\n", pthread_self());
        drivercount++;
        if(drivercount % 4 == 0 && drivercount != 0){
            printf("Thread ID: %ld , Team: B, I am the captain and driving the car\n", pthread_self());
        }
        /*
        if(countA != 0){
            sem_post(&semA);
        }
        else if(countB != 0){
            sem_post(&semB);
        }
        */
    }

    
    pthread_barrier_wait(&barrier);

}


int main(int argc, char* argv[]){

    pthread_t *tha;
    pthread_t *thb;
    teamAnumber = atoi(argv[1]);
    teamBnumber = atoi(argv[2]);
    //teamBnumber++;
    sem_init(&semA, 0, 0);
    sem_init(&semB, 0, 0);
    sem_init(&semC, 0, 1);
    sem_init(&semD, 0, 1);
    pthread_barrier_init(&barrier, NULL, 4);
    pthread_barrier_init(&barrierA, NULL, teamAnumber);
    pthread_barrier_init(&barrierB, NULL, teamBnumber);

    if(teamAnumber == 0){
        ifAempty = true;
    }
    if(teamBnumber == 0){
        ifBempty = true;
    }
    //printf("%d", teamBnumber);
    //if((teamAnumber % 2 == 0) && (teamBnumber % 2 == 1) && (teamAnumber + teamBnumber - 1 % 4 == 0)){
        tha=(pthread_t *)malloc(teamAnumber* sizeof(pthread_t ));
        for(int i=0;i<teamAnumber;i++)
        {
            pthread_create(&tha[i],NULL,thread_func,&teama);
        }

        thb=(pthread_t *)malloc(teamBnumber* sizeof(pthread_t ));
        for( int i=0;i<teamBnumber;i++)
        {
            pthread_create(&thb[i],NULL,thread_func,&teamb);
        }
        for(int i=0;i<teamAnumber;i++)
        {
            pthread_join(tha[i],NULL);
        }
        for(int i=0;i<teamBnumber;i++)
        {
            pthread_join(thb[i],NULL);
        }
    //}
    sem_destroy(&semA);
    sem_destroy(&semB);
    sem_destroy(&semC);
    sem_destroy(&semD);
    pthread_barrier_destroy(&barrier);
    pthread_barrier_destroy(&barrierA);
    pthread_barrier_destroy(&barrierB);

    printf("The main terminates \n");
    return 0;
}