hello!!!
I want to Create a program that receives a number N as an argument and starts N threads each displaying one of a number from 1 to N synchronously (with named or anonymous semaphores of choice) to produce an ordered sequence (12 ... N) *.
here is my code
int N;
void* f0(int k, sem_t *mutex) {
for(int i = 0; i < 100; i++) {
if(k < N-1){
sem_wait(&mutex[k]);
printf("%d\n",k+1);
k = k + 1;
sem_post(&mutex[k]);
}
else{
sem_wait(&mutex[N-1]);
printf("%d\n",k+1);
k = N-k-1;
sem_post(&mutex[k]);
}
}
return NULL;
}
int main(int argc, char *argv[]) {
N = atoi(argv[1]);
int *value;
value= malloc(sizeof(int)*N);
sem_t *mutex;
mutex =malloc(sizeof(sem_t)*N);
pthread_t pid[N];
for (int i =0;i<N;i++){
if(i==0){
value[i]=1;
}
else{
value[i]=0;
}
}
for(int i=0;i<N;i++){
sem_init(&mutex[i],0,value[i]);
}
int m =0;
while(m<N){
pthread_create(&pid[m], NULL, f0(m,&mutex),0);
m=m+1;
printf("m = %d\n", m );
}
printf("heyG\n");
for (int i=0;i<N;i++){
pthread_join(pid[i],0);
}
for (int i=0;i<N;i++){
sem_destroy(&mutex[i]);
}
return EXIT_SUCCESS;
}
What I have tried:
I get this warning and I want to fix it
copie.c: In function ‘main’:
copie.c:60:44: warning: passing argument 2 of ‘f0’ from incompatible pointer type [-Wincompatible-pointer-types]
pthread_create(&pid[m], NULL, f0(m,&mutex),0);
^
copie.c:11:7: note: expected ‘sem_t * {aka union <anonymous> *}’ but argument is of type ‘sem_t ** {aka union <anonymous> **}’
void* f0(int k, sem_t *mutex) {