I have to do the program for the bounded buffer problem in UNIX C and what I have so far is the following code
Expand|Select|Wrap|Line Numbers
- #include"genSema.h"
- #include<stdlib.h>
- #include<stdio.h>
- #include<pthread.h>
- #include<sys/types.h>
- #include<sys/times.h>
- #include<unistd.h>
- int itCount=100;
- int N;
- int P_RAND_SEED = 1234;
- int C_RAND_SEED = 1234;
- #define FALSE 0;
- #define TRUE 1;
- int runFlag = TRUE;
- gen_semaphore *full;
- gen_semaphore *empty;
- void *producer(void *wp);
- void *consumer(void *wp);
- struct buffer_t{
- int buffer[10];
- unsigned int nextFull;
- unsigned int nextEmpty;
- }widgets;
- void* producer(void *wp)
- {
- char *message1;
- message1 = (char *)wp;
- printf("%s ", message1);
- int runFlag = TRUE;
- int timeToProduce=100000;
- int itCount;
- struct buffer_t *widgPtr;
- widgPtr = (struct buffer_t *)wp;
- srand(P_RAND_SEED);
- itCount=100;
- while(runFlag) {
- usleep(rand()%timeToProduce);
- P(empty);
- if((widgPtr->nextEmpty + 1)%N == widgPtr->nextFull)
- continue;
- widgPtr->buffer[widgPtr->nextEmpty] = itCount++;
- widgPtr->nextEmpty = (widgPtr->nextEmpty+1) % N;
- V(full);
- }
- printf("Producer: Terminating\\n");
- }
- void* consumer(void *wp)
- {
- char *message2;
- message2 = (char *)wp;
- printf("%s ", message2);
- int itCount;
- int timeToConsume=100000;
- struct buffer_t *widgPtr;
- widgPtr = (struct buffer_t *) wp;
- srand(C_RAND_SEED);
- int runFlag = TRUE;
- while(runFlag) {
- P(full);
- itCount = widgPtr->buffer[widgPtr->nextFull];
- widgPtr->nextFull = (widgPtr->nextFull+1) % N;
- usleep(rand()%timeToConsume);
- V(empty);
- }
- printf("Consumer: Terminating\n");
- }
- main()
- {
- int runTime = 2;
- int i;
- pthread_t prod_thrd;
- pthread_t cons_thrd;
- char *message1 = "producer running";
- char *message2 = "consumer running";
- empty = create_gen_semaphore(N);
- full = create_gen_semaphore(0);
- widgets.nextEmpty = 0;
- widgets.nextFull = 0;
- for(i = 0; i < N; i++)
- widgets.buffer[i] = empty;
- pthread_create(&prod_thrd, NULL, producer, message1);
- printf("consumer created at main");
- pthread_create(&cons_thrd, NULL, consumer, message2);
- printf("producer created at main");
- sleep(runTime);
- runFlag = FALSE;
- delete_gen_semaphore(empty);
- delete_gen_semaphore(full);
- printf("Main thread: Terminated\\n");
- exit(1);
- }
Expand|Select|Wrap|Line Numbers
- $ gcc -o prodcons prodcons.c
- prodcons.c: In function `main':
- prodcons.c:86: warning: assignment makes integer from pointer without a
- cast
- Undefined first referenced
- symbol in file
- P /var/tmp//cc6O6sjK.o
- V /var/tmp//cc6O6sjK.o
- create_gen_semaphore /var/tmp//cc6O6sjK.o
- delete_gen_semaphore /var/tmp//cc6O6sjK.o
- ld: fatal: Symbol referencing errors. No output written to prodcons
- collect2: ld returned 1 exit status
Please does anyone know what is that mean and why I'm getting this error. Another this that I would like to let you know about is that in the explanation for the code they tell you that "your job is to define all the italicized functions for thread management and syncronization."
of course the italicized functions are (create_gen_semaphore, delete_gen_semaphore, P, V)