Hi everyone, iam working on this program called consumer.c
This program is to demonstrate a modified version of the classical Producer/Consumer problem. This problem is as follows:
There is a store where both Workers (Producers) and Customers
(Consumers) exist. There is a cabinet in this store which all people can
access. However, when one person is using the cabinet, no one else can.
Workers produce objects to place within the cabinet, while Customers
buy those objects, thus removing them. If there is nothing in the
cabinet, then the Customer must wait until a Worker has created an object
and placed that object into the cabinet. However, if the cabinet is full,
then the Worker must wait until an object is bought before he/she can
restock the cabinet.
How can we simulate this behavior?
This are some errors i have so far, can you guys tell me if i am in the right direction. I hope i am but right now i am having some problems this are the errors i get (thanks you for your help):
Code:
user@linux-p3nb:~/program> gcc -o consumer consumer.c
consumer.c: In function ‘producer’:
consumer.c:6: error: ‘item’ undeclared (first use in this function)
consumer.c:6: error: (Each undeclared identifier is reported only once
consumer.c:6: error: for each function it appears in.)
consumer.c:7: error: expected ‘while’ before ‘wait’
consumer.c:8: error: ‘mutex’ undeclared (first use in this function)
consumer.c:11: error: ‘full’ undeclared (first use in this function)
consumer.c: In function ‘consumer’:
consumer.c:17: error: ‘full’ undeclared (first use in this function)
consumer.c:18: error: expected ‘while’ before ‘wait’
consumer.c:19: error: ‘item’ undeclared (first use in this function)
consumer.c:20: error: ‘mutex’ undeclared (first use in this function)
consumer.c:21: error: ‘empty’ undeclared (first use in this function)
consumer.c: At top level:
consumer.c:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘event_counter’
consumer.c:39: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘in’
consumer.c:41: error: redefinition of ‘producer’
consumer.c:4: error: previous definition of ‘producer’ was here
consumer.c: In function ‘producer’:
consumer.c:42: error: expected declaration specifiers or ‘...’ before numeric constant
consumer.c:44: error: ‘item’ undeclared (first use in this function)
consumer.c:45: error: expected ‘while’ before ‘sequence’
consumer.c:46: error: ‘out’ undeclared (first use in this function)
consumer.c:46: error: ‘num_buffers’ undeclared (first use in this function)
consumer.c:48: error: ‘in’ undeclared (first use in this function)
consumer.c: At top level:
consumer.c:52: error: redefinition of ‘consumer’
consumer.c:15: error: previous definition of ‘consumer’ was here
consumer.c: In function ‘consumer’:
consumer.c:53: error: expected declaration specifiers or ‘...’ before numeric constant
consumer.c:55: error: invalid lvalue in increment
consumer.c:56: error: expected ‘while’ before ‘in’
consumer.c:57: error: ‘item’ undeclared (first use in this function)
consumer.c:58: error: ‘out’ undeclared (first use in this function)
consumer.c:61:2: warning: no newline at end of file
user@linux-p3nb:~/program>
I am new to c programming and i got this example from a book and iam trying to get it running so it can be the base of my program. The problem is that i want to be sure of what iam doing and understanding it. I think the problem that i currently have is in the functions i have to declare the variables outside main or something... so that the functions can be read. Help me out please. Thanks for your help.
this is my code:
Code:
#include<stdio.h>
void producer ( void )
{
do
produce ( item );
wait ( empty ); // empty is semaphore
wait ( mutex ); // mutex is semaphore
put ( item );
signal ( mutex );
signal ( full );
while ( 1 );
}
void consumer ( void )
{
do
wait ( full );
wait ( mutex );
remove ( item );
signal ( mutex );
signal ( empty );
consume ( item );
while ( 1 );
}
class event_counter
{
int ec; // Event counter
public:
event_counter ( void ) // Default constructor
{
ec = 0;
}
int read ( void ) const { return ( ec ); }
void advance ( void ) { ec++; }
void await ( const int v ) { while ( ec < v ); }
};
extern event_counter in, out; // Shared event counters
void producer ( void )
{
int sequence ( 0 );
do
produce ( item );
sequence++;
out.await ( sequence - num_buffers );
put ( item );
in.advance();
while ( 1 );
}
void consumer ( void )
{
int sequence ( 0 );
do
sequence++;
in.await ( sequence );
remove ( item );
out.advance();
consume ( item );
while ( 1 );
}