Consumer program

This is a discussion on Consumer program within the C Programming forums, part of the General Programming Boards category; Hi everyone, iam working on this program called consumer.c This program is to demonstrate a modified version of the classical ...

  1. #1
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Exclamation Consumer program

    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 );
    }
    Wise_ron
    One man's constant is another man's variable

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,853
    So where is
    - the indentation
    - a whole bunch of { } around blocks of statements

    Just another "write the whole thing and dump it on a message board for some other sap to fix" post.

    Write it in small amounts and compile often. Then it will tell you you're screwing up when you've only made a few mistakes rather than a whole lot of mistakes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    class??? C++ or C?

    int main()????

    How many times do you think you need to declare one function?

  4. #4
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    ok this is part1 to have the program shorter

    Iam doing this program in C, and now i have broken it up into a smaller program.
    Code:
     
    #include<stdio.h>
    
    int main()
    {
    
    	void producer ( void )
    	{
    	do
    	produce ( item );
    	wait ( empty ); // empty is semaphore
    	wait ( mutex ); // mutex is semaphore
    	put ( item );
    	signal ( mutex );
    	signal ( full );
    	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 );
    	}	
    
    }
    this are my errors, what is wrong and how can i fix it?

    Code:
    consumer.c:2: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘wait’
    consumer.c: In function ‘producer’:
    consumer.c:10: error: ‘item’ undeclared (first use in this function)
    consumer.c:10: error: (Each undeclared identifier is reported only once
    consumer.c:10: error: for each function it appears in.)
    consumer.c:11: error: expected ‘while’ before ‘wait’
    consumer.c:12: error: ‘mutex’ undeclared (first use in this function)
    consumer.c:15: error: ‘full’ undeclared (first use in this function)
    consumer.c: In function ‘consumer’:
    consumer.c:21: error: ‘full’ undeclared (first use in this function)
    consumer.c:22: error: expected ‘while’ before ‘wait’
    consumer.c:23: error: ‘item’ undeclared (first use in this function)
    consumer.c:24: error: ‘mutex’ undeclared (first use in this function)
    consumer.c:25: error: ‘empty’ undeclared (first use in this function)
    consumer.c: In function ‘main’:
    consumer.c:30: error: ‘class’ undeclared (first use in this function)
    consumer.c:30: error: expected ‘;’ before ‘event_counter’
    consumer.c:43: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘in’
    consumer.c:43: error: ‘in’ undeclared (first use in this function)
    consumer.c:43: error: ‘out’ undeclared (first use in this function)
    consumer.c:56: error: redefinition of ‘consumer’
    consumer.c:19: error: previous definition of ‘consumer’ was here
    consumer.c: In function ‘consumer’:
    consumer.c:57: error: expected declaration specifiers or ‘...’ before numeric constant
    consumer.c:59: error: invalid lvalue in increment
    consumer.c:60: error: expected ‘while’ before ‘in’
    consumer.c:61: error: ‘item’ undeclared (first use in this function)
    consumer.c:66:2: warning: no newline at end of file
    Wise_ron
    One man's constant is another man's variable

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    class????????

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,853
    Use more indent levels
    Code:
    void producer ( void )
    {
    	do {
    	produce ( item );
    	wait ( empty ); // empty is semaphore
    	wait ( mutex ); // mutex is semaphore
    	put ( item );
    	signal ( mutex );
    	signal ( full );
    	} while ( 1 );
    }
    Extra { } in red.
    Also, functions must be declared outside other functions.

    Start a new file with just a blank main() and compile it.
    Then add ONE function to it and compile again. DO NOT add more code until that first one compiles OK.
    Add each remaining function in turn until you have the whole code, which compiles.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The program you posted is a C++ program, and uses classes. You're on the wrong forum, here.

    Rename your file with a .cpp extension, and moter over to the C++ forum, for help to make this program work.

    Adak

  8. #8
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    If you are compiling this on Linux, you need to include a few include files.

    e.g. for wait(2)

    #include <sys/types.h>
    #include <sys/wait.h>

  9. #9
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    For signal(2)

    #include <signal.h>

  10. #10
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Part 1 function producer

    Thanks for your help. Iam only working with the function as you sayed, iam getting some errors... can you guys help me out.

    Code:
    #include<stdio.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <signal.h>
    
    int main()
    
    {
    	void producer ( void )
      {
    	do 
    	produce ( item );
    	wait ( empty ); // empty is semaphore
    	wait ( mutex ); // mutex is semaphore
    	put ( item );
    	signal ( mutex );
    	signal ( full );
             while ( 1 );
      }
    }
    Code:
    part1.c: In function ‘producer’:
    part1.c:12: error: ‘item’ undeclared (first use in this function)
    part1.c:12: error: (Each undeclared identifier is reported only once
    part1.c:12: error: for each function it appears in.)
    part1.c:13: error: expected ‘while’ before ‘wait’
    part1.c:14: error: ‘mutex’ undeclared (first use in this function)
    part1.c:16: error: too few arguments to function ‘signal’
    part1.c:17: error: ‘full’ undeclared (first use in this function)
    part1.c:17: error: too few arguments to function ‘signal’
    Wise_ron
    One man's constant is another man's variable

  11. #11
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Where are item, mutex and full declared/defined? You seem to be missing a piece of code.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,853
    When did you come over all helpless (see your previous posts).
    I mean, you managed to declare variables before using them, write functions etc etc.

    This is just pseudo-code slapped into a compiler!

    Just read the damn error messages and pay attention.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21