hoi allen ik heb een probleem met deze code. Ik snap wel problemen maar hoe kan ik het verbeteren. ‘while loops’ dat producer en consumer synchroon communiceren.
Code:
#include <fcntl.h>
#include <semaphore.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
sem_t *mySemaphore ;
#define BUFFERSIZE 1000000
char boundedBuffer[BUFFERSIZE] ;
int filled = -1 , empty = -1 ;
void Produce(void *data) {
int i = 0 ;
while ( i < BUFFERSIZE ) {
boundedBuffer[++filled] = 'A' ;
i++ ;
}
}
void Consume(void *data) {
int i ;
while ( i < BUFFERSIZE) {
if ( boundedBuffer[++empty] != 'A' )
printf("Error consume != A\n"); ;
i++ ;
}
}
int main(int argc, char * argv[]) {
pthread_t p1, p2 ;
int l = 0 ;
mySemaphore = sem_open("MySemaphore",O_CREAT) ;
for ( l = 0 ; l < BUFFERSIZE; l++) boundedBuffer[l] = '.' ;
/* Create our threads */
pthread_create(&p1, NULL,(void *(*)(void *)) Produce, boundedBuffer);
pthread_create(&p2, NULL,(void *(*)(void *)) Consume, boundedBuffer);
/* Wait for our threads */
pthread_join(p1, NULL);
pthread_join(p2, NULL);
return 0 ;
}
Laatst bewerkt door een moderator: