C :: Producer Consumer Using Pthreads And FIFO

Nov 23, 2013

I am working on the producer-consumer problem and am mostly happy with what I have except for one remaining issue. I am using pthreads for consumers and producers on a FIFO, and synchronizing using a combination of semaphores and mutexes. Things seem to work fine, except that my consumers seem unable to tell when there may be more data coming into the FIFO.

I need to be able to determine not just when the FIFO is empty (it gets emptied every time a consumer thread runs) but when ALL producers have detached from it - that's when my consumers should pthread_exit, and not before that. I have read conflicting information on how FIFOs work in terms of EOF and have been unable to get it working.

(NOTE: that's what I *think* the problem is... It's my first time working with pthreads, so there may be other issues I am not even aware of).

In any case, my code is below. I am running it on a Linux VM on top of Windows, if that makes any difference. It takes 3 command line parameters - FIFO name (will be created if doesn't exist), number of producers, and number of consumers.

Code:

#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>

[Code].....

View 5 Replies


ADVERTISEMENT

C :: Consumer And Producer Threads Aren't Synchronizing Correctly?

Dec 3, 2013

I have a school assignment to create a program that uses a producer thread to create a series of random numbers, and a consumer thread to read those numbers (via a shared bounded buffer), and record the min, max, average, and number of items. Now, the threads aren't synching well. Here's my code:

Code:
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>

pthread_cond_t empty;
pthread_cond_t full;
int done = 0;
pthread_mutex_t lock;

[code] ....

And output:

Code:
Producer in for
Consumer Entering While
Consumer reading item 0
Producer making item 0
Consumer reading item 1
Consumer reading item 2
Consumer reading item 3
Producer making item 1
Producer making item 2
Producer making item 3
Producer making item 4
Producer making item 5
Producer making item 6
Consumer reading item 4
Minimum: 0
Maximum: 4978
Average: 995
Items Produced: 5

View 4 Replies View Related

C++ :: FIFO Reads Endlessly After Closing It On Producer Side

May 10, 2012

Right after I close FIFO on the producer side, the app on the consumer side gets into an endless loop. select() returns that FIFO is ready to read. Below is the excerpt from the consumer code. The FIFO is opened "blocking" on both ends.

Code:

FifoClass fifo_r = FifoClass("/tmp/FIFO_READ");
int fdr = fifo_r.OpenR();// this->fd = open(name.c_str(), O_RDONLY , 0);
for(int i=0; ;i++) {
FD_ZERO(&rset);
FD_SET(fdr, &rset);

[Code] .....

View 2 Replies View Related

C :: Access A FIFO (Circular Queue) Between Two Cores

Oct 31, 2014

I want to write a program where one core (core 0) will fill the FIFO and other core (core 1 ) will delete the data from FIFO.

Core 0:

I create a fifo

Code:
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;

And my en-queue function is in core 0 and writing to specific memory location..(which works perfectly)

Code:
void enq(int data)
{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;

[Code] ....

Now the problem is in the core 1. Here I am unable to read the values from the specific memory location. I am getting garbage value. Where I am doing some stupid error.. I did not understand

Code:
(front->ptr) = (unsigned int *) memory_location;

When I print the (front->ptr) it shows correct memory address but inside the De-queue function in core 1, I am getting wrong value..

Code:
int deq(int buf[n]) {
front1 = front;
printf("Val %d ", front->info); // showing wrong value
if (front1 == NULL) {
printf("

Error: Trying to display elements from empty queue");
return 0;

[Code] ...

View 8 Replies View Related

C :: Translate Pthreads Into OpenMP

May 20, 2013

I'm quite new to openMP, mostly used pthreads and mpi before. Now I like to tinker a bit with openMP, but haven't found any good docs, reference list or similar.

What's the equivalent to pthread's mutex lock in openMP?

Code:
#pragma omp parallel for
for(i=0; i<n ; i++){
// Do something intelligent...
// If needed handle a shared variable.
}

How do I protect the shared variable?

View 1 Replies View Related

C :: How To Emulate Callback Mechanism - Pthreads Join

Jun 19, 2013

I am trying to emulate callback mechanism in C. The code I have is follows:

Code:
#include <stdio.h>
#include <pthread.h>

struct fopen_struct {
char *filename;
char *mode;
void *(*callback) (FILE *);

[Code] .....

If I do a pthread_join in fopen_t() it works perfectly fine. But if I do that in main() I get a segmentation fault error. I want to call it in main() because I want it to be non-blocking, is there any solution?

View 1 Replies View Related

C :: Doing Histogram Using Pthreads - Segmentation Fault (core Dumped)

Apr 24, 2014

I am doing Histogram using pthreads and after long struggle on it.. finally it says 'Segmentation Fault (Core Dumped)'. I knew that error means something somewhere I am accessing to a wrong memory location.

Here is my code..

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void Usage(char prog_name[]);
void Gen_data(void *p);
void Gen_bins(void *p);
int Which_bin(void *p);
void Print_histo(void *p);

[Code] ...

How to fix 'Segmentation Fault (Core Dumped)'?

View 2 Replies View Related

Visual C++ :: Pthreads - Solve System Of Equations By Gauss-Jordan Method

Dec 15, 2012

I have a problem: solve the system of equations by the Gauss-Jordan methods with pthreads. I have a big matrix A (for example 2000x2000), so i must solve Ax = b. Before I devide matrix to number of threads and each thread must work only with his peace of matrix.

Code:
#include <iostream>
#include "synchronize.h"
#include <pthread.h>
using namespace std;
typedef struct _ARGS {
int thread_count;
int thread_number;

[Code] .....

I write it on Ubuntu, and when I compile [g++ main.cpp -o main -lpthread -lm] the it works good(for example, if in 1 thread I get time_of_working = 10 sec, on 2 threads time_of_working = 5.4, i.e. about 2 times faster ), if I compile like this [g++ main.cpp -o main -lpthread -lm -O3] it is only 1.2-1.3 times faster.

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved