C shell script help

still a beginner in this c shell thing ..actually i'm trying to implement IPC problem (Producer/consumer)
using process,shared memory and signals but producer and consumer represented by one process
-----------------------------------------------------------------------
#include
#include
#include
#include

char buf[BSIZE];
int occupied;
int nextin;
int nextout;
mutex_t mutex;
cond_t more;
cond_t less;
} buffer_t;
buffer_t buffer;


void producer (buffer_t*b, char item)
{
pthread_mutex_lock (&b->mutex);

while (b-> occupied >= BSIZE)
pthread_cond_wait(&b->less, &b->mutex);

assert(b->oocupied < BSIZE);


b->buf[b->nextin++]= item;

b->nextin %= BSIZE;
b->occupied++;

/* now:either b->oocupied < BSIZE and b-> nextin is the index
of the next empty slot in the buffer,or b->occupied ==Bsize and
b->nextin is the index of the next (occupied) slot that will be
emptied by a consumer(such as b->nextin == b-> nextout) */

pthread_cond_signal(&b->more) ;

pthread_mutex_unlock(&b->mutex);

}

char consumer(buffer_t *b)
{
char item;
pthread_mutex_lock(&b->mutex);
while (b->occupied <= 0)
pthread_cond_wait(&b->more, &b->mutex);

assert(b->occupied>0);
item = b->buf[b->nextout++];
b->nextout %=Bsize;
b->occupied-- ;


/* now:either b->oocupied >0 and b-> nextout is the index
of the next occupied slot in the buffer,or b->occupied ==0 and
b->nextout is the index of the next (empty) slot that will be
filled by the producerr(such as b->nextout == b-> nextin) */

pthread_cond_signal(&b->less);
pthread_mutex_unlock(&b->mutex);
return(item);
}


Comments

  • : still a beginner in this c shell thing ..actually i'm trying to implement IPC problem (Producer/consumer)
    : using process,shared memory and signals but producer and consumer represented by one process
    : -----------------------------------------------------------------------
    : #include
    : #include
    : #include
    : #include
    :
    : char buf[BSIZE];
    : int occupied;
    : int nextin;
    : int nextout;
    : mutex_t mutex;
    : cond_t more;
    : cond_t less;
    : } buffer_t;
    : buffer_t buffer;
    :
    :
    : void producer (buffer_t*b, char item)
    : {
    : pthread_mutex_lock (&b->mutex);
    :
    : while (b-> occupied >= BSIZE)
    : pthread_cond_wait(&b->less, &b->mutex);
    :
    : assert(b->oocupied < BSIZE);
    :
    :
    : b->buf[b->nextin++]= item;
    :
    : b->nextin %= BSIZE;
    : b->occupied++;
    :
    : /* now:either b->oocupied < BSIZE and b-> nextin is the index
    : of the next empty slot in the buffer,or b->occupied ==Bsize and
    : b->nextin is the index of the next (occupied) slot that will be
    : emptied by a consumer(such as b->nextin == b-> nextout) */
    :
    : pthread_cond_signal(&b->more) ;
    :
    : pthread_mutex_unlock(&b->mutex);
    :
    : }
    :
    : char consumer(buffer_t *b)
    : {
    : char item;
    : pthread_mutex_lock(&b->mutex);
    : while (b->occupied <= 0)
    : pthread_cond_wait(&b->more, &b->mutex);
    :
    : assert(b->occupied>0);
    : item = b->buf[b->nextout++];
    : b->nextout %=Bsize;
    : b->occupied-- ;
    :
    :
    : /* now:either b->oocupied >0 and b-> nextout is the index
    : of the next occupied slot in the buffer,or b->occupied ==0 and
    : b->nextout is the index of the next (empty) slot that will be
    : filled by the producerr(such as b->nextout == b-> nextin) */
    :
    : pthread_cond_signal(&b->less);
    : pthread_mutex_unlock(&b->mutex);
    : return(item);
    : }
    :
    :
    :

    Hi,
    What u've written is a C program/code. Its not a shell script.
    What problems actually r u having with this code?


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion