Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.2 kB
1
Indexable
Never
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <semaphore.h>
bool ifAempty, ifBempty;
int countA = 0;
int countB = 0;
int sizeA, sizeB;
sem_t semA, semB;
pthread_mutex_t mutex;
pthread_barrier_t barrier, barrierA, barrierB;
int driver=0;
void * rideShare(void *argv)
{
    char *args = (char*)argv;
    char team = *args;
    if(team == 'A')
    {
        pthread_mutex_lock(&mutex);
        countA++;
        if(countA == 2 && countB == 2)
        {
            for(int i = 0; i < 2; i++)
            {
                sem_post(&semA);
                sem_post(&semB);
            }
            countA= countA-2;
            countB= countB-2;
        }
        else if(countA == 4)
        {
            for(int i = 0; i < 4; i++)
            {
                sem_post(&semA);
            }
            countA= countA-4;
        }
        else if(countB ==4);
        {
            for(int i = 0; i < 4; i++)
            {
                sem_post(&semB);
            }
            countB= countB-4;
        }
        pthread_mutex_unlock(&mutex);
        printf("Thread ID: %ld , Team: %c, I am currently looking for a car\n", pthread_self(), team);
        //pthread_barrier_wait(&barrierA);
        sem_wait(&semA);
    }
    else if(team == 'B')
    {
        pthread_mutex_lock(&mutex);
        countB++;
        if(countA == 2 && countB == 2)
        {
            for(int i = 0; i < 2; i++)
            {
                sem_post(&semA);
                sem_post(&semB);
            }
            countA= countA-2;
            countB= countB-2;
        }
        else if(countB == 4)
        {
            for(int i = 0; i < 4; i++)
            {
                sem_post(&semB);
            }
            countB= countB-4;
        }
        else if(countA == 4);
        {
            for(int i = 0; i < 4; i++)
            {
                sem_post(&semA);
            }
            countA= countA-4;
        }
        pthread_mutex_unlock(&mutex);
        printf("Thread ID: %ld , Team: %c, I am currently looking for a car\n", pthread_self(), team);
        //pthread_barrier_wait(&barrierB);
        sem_wait(&semB);
    }
    if(team == 'A')
    {
        printf("Thread ID: %ld , Team: %c, I have found a spot on the car\n", pthread_self(), team);
        driver++;
        //countA--;
        if(driver% 4 == 0 && driver != 0)
        {
            printf("Thread ID: %ld , Team: %c, I am the captain and driving the car\n", pthread_self(), team);
        }
    }
    if(team == 'B')
    {
        printf("Thread ID: %ld , Team: %c, I have found a spot on the car\n", pthread_self(), team);
        driver++;
        //countB--;
        if(driver% 4 == 0 && driver != 0)
        {
            printf("Thread ID: %ld , Team: %c, I am the captain and driving the car\n", pthread_self(), team);
        }
    }
    pthread_barrier_wait(&barrier);
    
}
int main(int argc, char* argv[])
{
    pthread_t *thread_A , *thread_B;
    sizeA = atoi(argv[1]);
    sizeB = atoi(argv[2]);
    char A = 'A';
    char B = 'B';
    if(sizeA ==0)
    {
        ifAempty =true;
    }
    else if(sizeB == 0)
    {
        ifBempty = true;
    }
    pthread_barrier_init(&barrier, NULL, 4);
    pthread_barrier_init(&barrierA, NULL, 4);
    pthread_barrier_init(&barrierB, NULL, 4);
    sem_init(&semA, 0, 0);
    sem_init(&semB, 0, 0);
    if(sizeA % 2==0 && sizeB % 2 == 0 && (sizeA + sizeB)%4 == 0)
    {
        thread_A=(pthread_t *)malloc(sizeA* sizeof(pthread_t ));
        for(int i=0;i<sizeA;i++)
        {
            pthread_create(&thread_A[i],NULL,rideShare,&A);
        }

        thread_B=(pthread_t *)malloc(sizeB* sizeof(pthread_t ));
        for( int i=0;i<sizeB;i++)
        {
            pthread_create(&thread_B[i],NULL,rideShare,&B);
        }
        for(int i=0;i<sizeA;i++)
        {
            pthread_join(thread_A[i],NULL);
        }
        for(int i=0;i<sizeB;i++)
        {
            pthread_join(thread_B[i],NULL);
        }
    }
    sem_destroy(&semA);
    sem_destroy(&semB);
    pthread_barrier_destroy(&barrier);
    pthread_barrier_destroy(&barrierA);
    pthread_barrier_destroy(&barrierB);
    printf("The main terminates\n");
    return 0;
}