semaphores

This is a discussion on semaphores within the C++ Programming forums, part of the General Programming Boards category; this a machine problem given to us, and i dont have any deep experience in programming, would anyone could help ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    4

    Post semaphores

    this a machine problem given to us, and i dont have any deep experience in programming, would anyone could help me on this...help badly need..thanks

    Synchronization Problem:
    In a cake factory, there are D machines making dough, IS machines making strawberry icing, VS machines making vanilla icing, and CS machine making chocolate icing. These machines are able to make 1 unit of dough/icing at different speed. The dough produced and placed in the universal dough pool, and each icing made by each type of icing machine are placed on separate icing pools (all strawberry icing made by each strawberry icing machine places all machines are placed on a different pool).

    The factory employs chefs with different experience. The chocolate chef requires 1 unit of dough and 2 units of chocolate icing; the strawberry chef requires 1 unit of dough, 1 unit of strawberry and vanilla icing; the wedding chef requires 3 units of dough and 1 unit of each icing.

    The chef will first gather the ingredients required to bake the cake. Once the ingredients are gathered the chef then waits in line to use one of N ovens in the factory. When the cake is ready, the oven signals the chef to bring the cake out of the oven and decorate it.

    System requirements:
    The solution must allow as much concurrency as possible avoiding deadlocks and starvations. The system must allow the user to input the values of the variables D, IS, S, CS, N. The status of each chef must also be visible, whether they are waiting for an ingredient (system must specify which ingredient), waiting for an oven, or waiting for the cake inside the oven. The system must also be able to show the status of the dough and icing pools, and be able to show the contents of each oven (identify which chef’s cake is inside the oven).

    -Try to solve the synchronization problem using semaphores and monitors.
    -implement features mention in the system requirements

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    A semaphore is like a value that is set when a resource is being accessed. For instance, if a file is being accessed by a thread the thread might set being_accessed to true. Then another thread that wants access to the resource knows to wait until being_accessed is false, so that they don't access it at the same time.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    Wow. that's quiet the project. Especially because you are being asked to make the factory as efficient as possible. Ok, anyways....

    No one here is going to write the code for you. But we will surely help once you start writting it yourself. What you absolutely must do is to put down those specifications on paper.

    Draw the factory. The chefs, the machines, the ovens, the different pools as rectangles clearly labeled. Connect them with lines showing their relationships. The vanilla ice machine connecting to the ice pool for instance and the chefs connecting to the machines and ovens... Draw the whole factory like this.

    Once you are done you will have a much clearer picture of the problem, will be able to start coding the skeleton of your program and will have a general view of how the objects will communicate between each other.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    http://en.wikipedia.org/wiki/Dining_...ophers_problem

    Is this homework? Do you understand the problem at all? You gave us the question, but you haven't shown us your attempt yet.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    4

    Question

    well i've tried doing it but its clearly imposible for me, the proffesor request us to do a fine and course grain solution...


    for the dough and icing a created a buffer pool having a 10 slots...

    then the three chef as a order, the first one is the wedding chef, then the starberry and lastly is the chocolate chef

    what i did was use a p(mutex) to siganal the the other chef then get the required ingredients, different other p(await) to the buffer pools for me to know if it is full or if they have the ingrdients.
    then if the pool has the ingedient minus to the total number of the buffer slot.

    then p(await) to other pools to get the required ingredients...
    then in the oven part implanning to use v(mutex) first to siganl the oven...

    i could write a pseudo code but i cant put down the logic in to the code...could anyone help me on this...


    thanks

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    This is the code for the dough machine:
    Code:
    dought()
    {
    p(dough_pool_empty);
    
    buf_dough[rear] = new_dough;
    rear = rear % 1;
    
    v(dough_pool_full);
    }
    
    for the icing its similar
    
    then for the chocolate chef
    
    p(dough_pool_full)
    new_dough = buf dough[front];
    front = front %1;
    p(CS_pool_full)
    new_CS = buf_cs[front];
    front = front % 2; // since that the chef needs 2 units of chocolate icing, i place 2 here,         
                                   kindy check if it is correct to do so....thanks
    p(mutex)
    v(oven_use)
    bake_in_oven();
    p(oven_use_empty)
    v(dough_pool_empty)
    v(cs_pool_empty)
    }
    please help me...the deadline is near
    thanks...

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    rear = rear % 1;
    This is the same as
    Code:
    rear = 0;
    Think about it. I don't think that's what you want. (If it is maybe you should just assign zero directly.)

    Code:
    front = front % 2; // since that the chef needs 2 units of chocolate icing, i place 2 here,         
                                   kindy check if it is correct to do so....thanks
    You should search for and read up on the modulo operator: http://www.cprogramming.com/tutorial/modulus.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Semaphores, need advice on implementation.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2009, 01:54 AM
  2. semaphores
    By Dr Spud in forum C Programming
    Replies: 7
    Last Post: 09-22-2007, 01:45 PM
  3. Semaphores Problems
    By mhelal in forum Linux Programming
    Replies: 2
    Last Post: 05-06-2007, 11:36 PM
  4. Semaphores
    By Jules in forum C Programming
    Replies: 4
    Last Post: 01-18-2004, 01:58 PM
  5. Semaphores
    By edk in forum C++ Programming
    Replies: 1
    Last Post: 11-25-2001, 03:55 PM

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