Question: I've made these 3 files for implementing a bounded buffer but when trying to run the code on linux, I keep getting a Segmentation fault

I've made these 3 files for implementing a bounded buffer but when trying to run the code on linux, I keep getting a Segmentation fault (core dumped) error and I can't figure out where in the code this error is occuring. I believe it is because memory is not being allocated but I can't figure out where.

1. Header File

#ifndef __BBUFF_H__ #define __BBUFF_H__

#include

#define BUFFER_SIZE 10

/* Structure for a message */ struct bb_msg {

/* Thread ID */ int t_id;

/* Message ID */ int m_id; };

/* Initiailze a message structure */ void bb_init_msg(struct bb_msg *msg, int t_id, int m_id);

/* Copy the source message to the destination message */ void bb_copy_msg(struct bb_msg * source, struct bb_msg * destination);

/* Display the contents of a message along with the id of the receiver */ void bb_display_msg(struct bb_msg *msg, int receiver);

/* Structure for a bounded buffer */ struct bbuff { /* Array of messages */ struct bb_msg messages[BUFFER_SIZE];

/* Number of messages sent */ unsigned int in;

/* Number of messages received */ unsigned int out;

/* Lock to protect shared data */ pthread_mutex_t lock; };

/* Initialize the bounded buffer */ void bb_init(struct bbuff * buffer);

/* Send a message to a bounded buffer */ void bb_send(struct bbuff * buffer, struct bb_msg * message);

/* Receive a message from a bounded buffer */ void bb_receive(struct bbuff * buffer, struct bb_msg * message);

#endif

2. This program tests a bounded buffer by creating multiple sending and receiving threads

#include #include #include "bbuff.h"

/* Structure to hold thread arguments */ struct t_args {

/* Thread ID */ int t_id;

/* Number of messages to send/receive */ int num_msgs;

/* Bounded buffer to use */ struct bbuff * buffer; };

/* Initialize t_args structure */ void t_args_init(struct t_args *args, int t_id, int num_msgs, struct bbuff * buffer);

/* Function for sending thread to execute */ void * send_msgs(void * args);

/* Function for receiving thread to execute */ void * receive_msgs(void * args);

/* Main function */ int main(int argc, char *argv[]) { pthread_t sender1; pthread_t sender2; pthread_t sender3;

pthread_t receive1; pthread_t receive2; pthread_t receive3; pthread_t receive4;

struct bbuff buffer; bb_init(&buffer); struct t_args send_args1; struct t_args send_args2; struct t_args send_args3;

struct t_args receive_args1; struct t_args receive_args2; struct t_args receive_args3; struct t_args receive_args4;

t_args_init(&send_args1, 0, 8, &buffer); t_args_init(&send_args1, 1, 8, &buffer); t_args_init(&send_args1, 2, 8, &buffer);

t_args_init(&receive_args1, 0, 6, &buffer); t_args_init(&receive_args1, 1, 6, &buffer); t_args_init(&receive_args1, 2, 6, &buffer); t_args_init(&receive_args1, 3, 6, &buffer);

pthread_create(&sender1, NULL, send_msgs, &send_args1); pthread_create(&sender2, NULL, send_msgs, &send_args2); pthread_create(&sender3, NULL, send_msgs, &send_args3);

pthread_create(&receive1, NULL, receive_msgs, &receive_args1); pthread_create(&receive2, NULL, receive_msgs, &receive_args2); pthread_create(&receive3, NULL, receive_msgs, &receive_args3); pthread_create(&receive4, NULL, receive_msgs, &receive_args4);

pthread_join(sender1, NULL); pthread_join(sender2, NULL); pthread_join(sender3, NULL);

pthread_join(receive1, NULL); pthread_join(receive2, NULL); pthread_join(receive3, NULL); pthread_join(receive4, NULL); }

/* Initialize t_args structure */ void t_args_init(struct t_args *args, int t_id, int num_msgs, struct bbuff * buffer) { args->t_id = t_id; args->num_msgs = num_msgs; args->buffer = buffer; }

/* Function for sending thread to execute */ void * send_msgs(void * args) { struct t_args * real_args = (struct t_args *) args; struct bb_msg* message; int m_id; for( m_id = 0; m_id < real_args->num_msgs; m_id++) {

bb_init_msg(message, real_args->t_id, m_id); bb_send(real_args->buffer, message); } return NULL; }

/* Function for receiving thread to execute */ void * receive_msgs(void * args) { struct t_args * real_args = (struct t_args *) args; struct bb_msg* message; int m_id; for(m_id = 0; m_id< real_args->num_msgs; m_id++) {

bb_receive(real_args->buffer, message); bb_display_msg(message, real_args->t_id); } return NULL; }

3. Source file for bounded buffer implementation

#include #include #include "bbuff.h"

/* Initiailze a message structure */ void bb_init_msg(struct bb_msg *msg, int t_id, int m_id) { msg->t_id = t_id; msg->m_id = m_id; }

/* Copy the source message to the destination message */ void bb_copy_msg(struct bb_msg * source, struct bb_msg * destination) { *destination = *source; }

/* Display the contents of a message along with the id of the receiver */ void bb_display_msg(struct bb_msg *msg, int receiver) { printf("Sending Thread: %d, message: %d, receiving: %d", msg->t_id, msg->m_id, receiver); }

/* Initialize the bounded buffer */ void bb_init(struct bbuff * buffer) { buffer->in = 0; buffer->out = 0; pthread_mutex_init(&buffer->lock, NULL); }

/* Send a message to a bounded buffer */ void bb_send(struct bbuff * buffer, struct bb_msg * message) { pthread_mutex_lock(&buffer->lock); while(buffer->in - buffer->out == BUFFER_SIZE){ pthread_mutex_unlock(&buffer->lock); pthread_mutex_lock(&buffer->lock); } bb_copy_msg(message, &buffer->messages[buffer->in % BUFFER_SIZE]); buffer->in++; pthread_mutex_unlock(&buffer->lock); }

/* Receive a message from a bounded buffer */ void bb_receive(struct bbuff *buffer, struct bb_msg * message) { pthread_mutex_lock(&buffer->lock); while(buffer->in == buffer->out){ pthread_mutex_unlock(&buffer->lock); pthread_mutex_lock(&buffer->lock); }

bb_copy_msg(message, &buffer->messages[buffer->out % BUFFER_SIZE]); buffer->out--; pthread_mutex_unlock(&buffer->lock); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!