Home > Net >  Math Operations using Semaphores in C
Math Operations using Semaphores in C

Time:10-23

I'm learning how to program in C using threads and semaphores on Ubuntu. I want to make a program that does mathematical operations such as addition, mulit... I've declared two semaphores: mutex for mutual exculsion and prive (private sem), I've used the private one so that P4 wouldn't be executed, it doesn't matter what function works first as long as P4 is the last one. I got four functions: P1:

 void *P1(void *arg, int A, int B){
       sem_wait(&mutex);
       S1 = A   B;
       printf("S1= %d", S1);
       sem_post(&prive);
       pthread_exit(NULL);
       }

P2:

  void *P2(void *arg, int A){
       sem_wait(&mutex);
       S2 = A   4;
       printf("S2= %d", S2);
       sem_post(&prive);
       pthread_exit(NULL);
       }

P3:


       void *P3(void *arg, int S2){
       sem_wait(&mutex);
       S3 = S2 * 2;
       sem_post(&prive);
       pthread_exit(NULL);
       }

P4:

void *P4(void *arg, int S3, int S2){
       sem_wait(&prive);
       sem_wait(&prive);
       S4 = S2   S3;
       sem_post(&mutex);
       sem_post(&mutex);
       pthread_exit(NULL);
       }

I've made the program but it displays the following error message:

TP4_EXO3.c: In function ‘main’: TP4_EXO3.c:24:1: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default] pthread_create(&TID1, NULL, P1, NULL); ^ In file included from TP4_EXO3.c:3:0: /usr/include/pthread.h:244:12: note: expected ‘void * (*)(void )’ but argument is of type ‘void * ()(void *, int, int)’ extern int pthread_create (pthread_t *__restrict __newthread, //Same goes to TID2, TID3, TID4// ^

TP4_EXO3.c: At top level: TP4_EXO3.c:57:7: error: conflicting types for ‘P3’ void *P3(void *arg, int S2){ ^ TP4_EXO3.c:8:7: note: previous declaration of ‘P3’ was here void *P3(void *arg, int S1, int S2); ^

I want for my program to enter 2 vars and execute the functions, how to solve these problems?

       #include<stdio.h>
       #include<stdlib.h>
       #include<pthread.h>
       #include<semaphore.h>

       void *P1(void *arg, int A, int B);
       void *P2(void *arg, int A);
       void *P3(void *arg, int S1, int S2);
       void *P4(void *arg, int S3, int S2);

       sem_t mutex, prive;

       int S1 = 0, S2 = 0, S3 = 0, S4 = 0;
       int A = 15;
       int B = 10;

       int main(){
       pthread_t TID1, TID2, TID3, TID4;
       printf("Main: ");

       sem_init(&mutex, 0, 1);
       sem_init(&prive, 0, 0);

       pthread_create(&TID1, NULL, P1, NULL);
       pthread_create(&TID2, NULL, P2, NULL);
       pthread_create(&TID3, NULL, P3, NULL);
       pthread_create(&TID4, NULL, P4, NULL);


       pthread_join(TID1, NULL);
       pthread_join(TID2, NULL);
       pthread_join(TID3, NULL);
       pthread_join(TID4, NULL);

       sem_destroy(&mutex);
       sem_destroy(&prive);
       printf("S4= %d", S4);
       }

CodePudding user response:

The last parameter in these pthread_create calls

pthread_create(&TID1, NULL, P1, NULL);
pthread_create(&TID2, NULL, P2, NULL);
pthread_create(&TID3, NULL, P3, NULL);
pthread_create(&TID4, NULL, P4, NULL);

Should be a pointer to a struct containing whatever parameters you want that function to be able to access & use.

Example for your P1 function

struct P1_args {
    //void *arg;
    int A;
    int B;
};

Then in your void *P1(void *arg, int A, int B) function

void *P1(void *arg){
    struct P1_args *args = arg;
    sem_wait(&mutex);
    S1 = args->A   args->B;
    printf("S1= %d", S1);
    sem_post(&prive);
    pthread_exit(NULL);
}

Then in your main function you can do

// ...
struct P1_args p1args = { .A = 1, .B = 2 };
pthread_create(&TID1, NULL, P1, &p1args);
// ...
pthread_join(TID1, NULL);
// ...

As for your note about

TP4_EXO3.c: At top level: TP4_EXO3.c:57:7: error: conflicting types for ‘P3’ void *P3(void *arg, int S2){ ^ TP4_EXO3.c:8:7: note: previous declaration of ‘P3’ was here void *P3(void *arg, int S1, int S2); ^

You have P3 declared with different signatures.


I also see some possible problems with your use of mutex & semaphores. If I'm not wrong, your functions look like they will never go past the sem_wait calls. Could be wrong. If that is the case, use sem_try_wait.

  • Related