Skip to content

Instantly share code, notes, and snippets.

@eLtronicsVilla
Created July 16, 2019 01:13
Show Gist options
  • Save eLtronicsVilla/6cadbac427be58779f737fb5f43231cf to your computer and use it in GitHub Desktop.
Save eLtronicsVilla/6cadbac427be58779f737fb5f43231cf to your computer and use it in GitHub Desktop.
Shared data between two process
#define buff_max 25
#define mod %
struct item{
// diffrent member of the produced data
// or consumed data
---------
}
// An array is needed for holding the items.
// This is the shared place which will be
// access by both process
// item shared_buff [ buff_max ];
// Two variables which will keep track of
// the indexes of the items produced by producer
// and consumer The free index points to
// the next free index. The full index points to
// the first full index.
int free_index = 0;
int full_index = 0;
item nextProduced;
while(1){
// check if there is no space
// for production.
// if so keep waiting.
while((free_index+1) mod buff_max == full_index);
shared_buff[free_index] = nextProduced;
free_index = (free_index + 1) mod buff_max;
}
item nextConsumed;
while(1){
// check if there is an available
// item for consumption.
// if not keep on waiting for
// get them produced.
while((free_index == full_index);
nextConsumed = shared_buff[full_index];
full_index = (full_index + 1) mod buff_max;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment