所以我整天忙于这个项目,我正在检查我所遇到的错误,并且在修复它们时,我来到了最后一部分,即警告。
我得到的警告是相同的:从兼容的指针类型传递”的参数1 [默认启用] 注意:预期为'union sem_t *',但参数的类型为'void *()(void )'
请帮帮我。我已经坚持了几个小时。预先感谢!
我在客户和理发师中都从sem_init中删除了&,但是没有用。
#include <unistd.h> //Provides API for POSIX(or UNIX) OS for system calls
#include <stdio.h> //Standard I/O Routines
#include <stdlib.h> //For exit() and rand()
#include <pthread.h> //Threading APIs
#include <semaphore.h> //Semaphore APIs
#define MAX 30 //Maximum no. of customers for simulation
#define MAX_BARBERS 1000
sem_t customer; //Semaphore
sem_t barber; //Semaphore
sem_t mutex; //Semaphore for providing mutially exclusive access
void *barbers(void *param); //Thread Function
void *customers(void *param); //Thread Function
int num_chairs;
int barbers_total;
int customers_total;
int serveMeNext; //Index to choose a candidate for cutting hair
void wait(); //Randomized delay function
int main(int argc, char *argv[]){
pthread_t barberid[MAX_BARBERS];
pthread_t customerid[MAX]; //Thread declaration
int i = 0;
if(argc != 4){
printf("Enter 3 arguments (Number of chairs, barbers, customers)\n");
exit(0);
}
num_chairs = atoi(argv[1]);
barbers_total = atoi(argv[2]);
customers_total = atoi(argv[3]);
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
/*Semaphore initialization*/
sem_init(&mutex,0,1);
sem_init(customers,0,0);
sem_init(barbers,0,0);
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
/*Barber thread initialization*/
printf("!!Barber Shop Opens!!\n");
for(i = 0; i <= barbers_total; i++){ //Creation of barbers
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
pthread_create(&barberid[i], NULL, barbers, (void*)&i);
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
sleep(1);
}
/*Customer thread initialization*/
for(i = 0; i <= customers_total; i++){ //Creation of Customer Threads
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
pthread_create(&customerid[i],NULL,customers,(void*)&i);
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
wait(); //Create customers in random interval
}
for(i = 0; i <= MAX; i++) //Waiting till all customers are dealt with
pthread_join(customerid[i],NULL);
printf("!!Barber Shop Closes!!\n");
exit(EXIT_SUCCESS); //Exit abandoning infinite loop of barber thread
}
void *customers(void *param){ /*Customer Process*/
int mySeat, B;
static int count = 0; //Counter of No. of customers
int numberOfFreeSeats = num_chairs; //Counter for Vacant seats in waiting room
int seatPocket[num_chairs]; //To exchange pid between customer and barber
int sitHereNext = 0; //Index for next legitimate seat
serveMeNext = 0;
sem_wait(&mutex); //Lock mutex to protect seat changes
count++; //Arrival of customer
printf("Customer-%d entered shop. ",count);
if(numberOfFreeSeats > 0){
--numberOfFreeSeats; //Sit on chairs on waiting room
printf("Customer-%d Sits In Waiting Room.\n",count);
sitHereNext = (++sitHereNext) % num_chairs; //Choose a vacant chair to sit
mySeat = sitHereNext;
seatPocket[mySeat] = count;
sem_post(&mutex); //Release the seat change mutex
sem_post(barbers); //Wake up one barber
sem_wait(customers); //Join queue of sleeping customers
sem_wait(&mutex); //Lock mutex to protect seat changes
B = seatPocket[mySeat]; //Barber replaces customer PID with his own PID
numberOfFreeSeats++; //Stand Up and Go to Barber Room
sem_post(&mutex); //Release the seat change mutex
/*Customer is having hair cut by barber 'B'*/
}else{
sem_post(&mutex); //Release the mutex and customer leaves without haircut
printf("Customer-%d Finds No Seat & Leaves.\n",count);
}
pthread_exit(0);
}
void *barbers(void *param){ /*Barber Process*/
int index = *(int *)(param);
int myNext, C;
int worktime;
int seatPocket[num_chairs]; //To exchange pid between customer and barber
printf("Barber-%d joins shop. ",index);
while(1){ /*Infinite loop*/
printf("Barber-%d Gone To Sleep.\n",index);
sem_wait(barbers); //Join queue of sleeping barbers
sem_wait(&mutex); //Lock mutex to protect seat changes
serveMeNext = (++serveMeNext) % MAX; //Select next customermyNext = serveMeNext;
C = seatPocket[myNext]; //Get selected customer's PID
seatPocket[myNext] = pthread_self(); //Leave own PID for customer
sem_post(&mutex);
sem_post(customers); //Call selected customer
/*Barber is cutting hair of customer 'C'*/
printf("Barber-%d Wakes Up & Is Cutting Hair Of Customer-%d.\n",index,C);
worktime = (rand() % 3) + 1;
printf("Barber-%d Finished.\n",index);
sleep(worktime);
}
}
void wait(){ /*Generates random number between 50000 to 250000*/
int x = rand() % (250000 - 50000 + 1) + 50000;
srand(time(NULL));
usleep(x); //usleep halts execution in specified miliseconds
}
由于我一直在做的研究,我认为我错的地方标有“”。
答案 0 :(得分:1)
从警告中可以明显看出,您正在传递一个指向函数的指针,而不是它所期望的sem_t
类型。
您似乎有customer
和barber
的错字。
sem_init
必须是
sem_init(&customer,0,0);
sem_init(&barber,0,0);
您的sem_post
必须是
sem_post(&customer);
sem_post(&barber);
此外,不要忘记用-lpthread
或-pthread
编译它