producer-consumer problem


 
Thread Tools Search this Thread
Top Forums Programming producer-consumer problem
# 1  
Old 12-16-2008
producer-consumer problem

The intention of the program. Create N threads with 2 shared memories.
One shared memory to write, one shared memory to recieve.
Consumer creates 2 shared memory to share with producer.
I need H threads for the producer as well(somebody help on it..)
Also another question, would segment_id variable interfere with each other in threads?

Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <string.h>
#include <pthread.h>
#define N 10
#define H 20
pthread_t house[N];
struct message
{
               char text[10];
               int source;
                int destination;
};

void *hovig(void *param)
{
                        struct message m1;
                        int segment_id;
                        int segment_id1;
                         int size = 60;
                        struct message *shared_memory;
                        struct message1 *shared_memory1;
                        segment_id = shmget(IPC_PRIVATE,size,S_IRUSR | S_IWUSR);
                       shared_memory = (struct message *)  shmat(segment_id,NULL,0);
                        segment_id1 = shmget(IPC_PRIVATE,size,S_IRUSR | S_IWUSR);
                       shared_memory1 = (struct message *)  shmat(segment_id1,NULL,0);

                      return  (void*)(size);
}

int main()
{
                        int value = 4;
                         int i = 0;
                      for(i = 0; i < N; i++)    
                            pthread_create(&house[i], NULL,&hovig, &(value));
                    
                      pthread_join(house[i],NULL);
}

# 2  
Old 12-16-2008
why you use shared memory in thread programming?
pthread_cond_wait + pthread_cond_signal + pthread_mutex_lock + pthread_mutex_unlock will work fine.

Code:
producer                                                     consumer
-----------------------------------------------------------------------------------
pthread_mutex_lock ()                       |   pthread_mutex_lock ()
while (total == buffer length)              |   while (total < 1) 
   pthread_cond_wait (less condition)   |       pthread_cond_wait (more condition) 
put_to_buffer ...                              |   get_from_buffer  ....
pthread_cond_signal (more)               |   pthread_cond_signal (less)
pthread_mutex_unlock ()                   |   pthread_mutex_unlock ()

# 3  
Old 12-20-2008
what if i use semget semop and not use pthread_mutex_lock..
shall it work?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Abnormal producer consumer problem driving me nuts

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: normally, i hate asking someone to do my homework for me but am getting desperate right now. i have a project... (1 Reply)
Discussion started by: alexantosh
1 Replies

2. UNIX for Dummies Questions & Answers

Abnormal producer consumer problem driving me nuts

normally, i hate asking someone to do my homework for me but am getting desperate right now. i have a project about consumer producer problem. the deadline is tonight at 23:55. but i havent gotten it working yet. i just COULDNT get it to work right yet. the problem is as follows: the C - program... (0 Replies)
Discussion started by: alexantosh
0 Replies

3. Homework & Coursework Questions

producer consumer semaphore

Control two exclusively shared resources(semaphore). The two resources are two files. The producer will write even numbers to one file, and odd numbers to another one. The consumer respectively reads from each file until it gets 5 even numbers and 5 odd numbers. Can any one help me with the... (1 Reply)
Discussion started by: gokult
1 Replies

4. UNIX for Dummies Questions & Answers

producer consumer

Control two exclusively shared resources. The two resources are two files. The producer will write even numbers to one file, and odd numbers to another one. The consumer respectively reads from each file until it gets 5 even numbers and 5 odd numbers. Can any one help me with the code. ... (3 Replies)
Discussion started by: gokult
3 Replies

5. Programming

producer consumer semaphore

Control two exclusively shared resources(semaphore). The two resources are two files. The producer will write even numbers to one file, and odd numbers to another one. The consumer respectively reads from each file until it gets 5 even numbers and 5 odd numbers. Can any one help me with the... (0 Replies)
Discussion started by: gokult
0 Replies

6. UNIX for Advanced & Expert Users

producer consumer

Control two exclusively shared resources(semaphore). The two resources are two files. The producer will write even numbers to one file, and odd numbers to another one. The consumer respectively reads from each file until it gets 5 even numbers and 5 odd numbers. Can any one help me with the... (0 Replies)
Discussion started by: gokult
0 Replies

7. Shell Programming and Scripting

producer consumer

Control two exclusively shared resources. The two resources are two files. The producer will write even numbers to one file, and odd numbers to another one. The consumer respectively reads from each file until it gets 5 even numbers and 5 odd numbers. Can any one help me with the code. ... (1 Reply)
Discussion started by: gokult
1 Replies

8. Programming

producer consumer

Control two exclusively shared resources. The two resources are two files. The producer will write even numbers to one file, and odd numbers to another one. The consumer respectively reads from each file until it gets 5 even numbers and 5 odd numbers. Can any one help me with the code. ... (0 Replies)
Discussion started by: gokult
0 Replies

9. UNIX for Dummies Questions & Answers

Slow Producer - Fast consumer

I would like to loop through a set of directories, performing operation(s) on each one. The basic script** is dirs=`find . -name .svn -print` for f in $dirs; do echo "Processing $f directory .." done Fine and dandy, but here is the problem: the find expression must complete... (2 Replies)
Discussion started by: jakeo25
2 Replies

10. UNIX for Dummies Questions & Answers

Producer/Consumer

Hi all, I have an Producer/Consumer program in C(using shared memory and semaphores).It is working fine with sleep calls after every read/write operation.Each one is a independent program.Now if i remove the sleep calls in consumer,it goes on waiting in the loop till the producer puts some valid... (3 Replies)
Discussion started by: poorni_uma
3 Replies
Login or Register to Ask a Question