NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

/**
* the producer produces goods
* the producer will produce an item / information and
* place it in a bound-buffer for the consumer
*
* the consumer consumes the goods and typically does something with them.
*
* The bounded buffer assumes
* a fixed buffer size.
*
* In this case, the consumer must wait if the buffer is empty,
* and the producer must wait if the buffer is full
*
* bounded buffer could be used to enable processes to share memory (which is the solution)
*
* In this case, the consumer must wait if the buffer is empty,
* and the producer must wait if the buffer is full.
*
* unbounded buffer has unlimited size
*
* To allow producer and consumer processes to run concurrently, we must have
available a buffer of items that can be filled by the producer and emptied by
the consumer. This buffer will reside in a region of memory that is shared by
the producer and consumer processes. A producer can produce one item while
the consumer is consuming another item. The producer and consumer must
be synchronized, so that the consumer does not try to consume an item that
has not yet been produced
*/

int producer() {
while (true) {
/* produce an item in next produced */
while (counter == BUFFER SIZE); /* do nothing */
buffer[in] = next produced;
in = (in + 1) % BUFFER SIZE;
counter++; // every time we add new item to buffer
}
}

int consumer() {
while (true) {
while (counter == 0); /* do nothing */
next consumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
counter--; // remove item from buffer
/* consume the item in next consumed */
}
}

// what if the producer & consumer attempt to access the same data concurrently ?
int exCode(/* max size buffer size - 1 */) {
/* We leave it as an exercise for you to provide a solution in which
BUFFER SIZE items can be in the buffer at the same time. */

/* producer code */
item next produced; // new item to be produced is stored here
while (true) {
/* produce an item in next produced */
while (((in + 1) % BUFFER SIZE) == out); /* do nothing */
buffer[in] = next produced;
in = (in + 1) % BUFFER SIZE;
}

/* consumer code */
item next consumed; // next item to be consumed is stored here
while (true) {
while (in == out)
; /* do nothing */
next consumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
/* consume the item in next consumed */
}

/* bounded buffer code ( seperate region of memory ) */
/* The shared buffer is implemented as a circular array with two logical pointers:
in and out. The variable in points to the next free position in the buffer; out
points to the first full position in the buffer. The buffer is empty when in ==
out; the buffer is full when ((in + 1) % BUFFER SIZE) == out.
*/
#define BUFFER SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER SIZE];
// two logical ptrs
int in = 0;
int out = 0;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.