Sponsored Content
Full Discussion: producer-consumer problem
Top Forums Programming producer-consumer problem Post 302268640 by joey on Tuesday 16th of December 2008 04:21:19 AM
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);
}

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
shmget(2)							System Calls Manual							 shmget(2)

Name
       shmget - get shared memory segment

Syntax
       #include <sys/types.h>
       #include <sys/ipc.h>
       #include <sys/shm.h>

       int shmget (key, size, shmflg)
       key_t key;
       int size, shmflg;

Description
       The system call returns the shared memory identifier associated with key.

       A  shared  memory  identifier and associated data structure and shared memory segment of size size bytes are created for key, if one of the
       following is true:

	    The key is equal to IPC_PRIVATE.  For further information, see

	    The key does not already have a shared memory identifier associated with it, and (shmflg & IPC_CREAT ) is true.

       Upon creation, the data structure associated with the new shared memory identifier is initialized as follows:

	    The and are set equal to the effective user ID and effective group ID of the calling process.

	    The low-order nine bits of are set equal to the low-order nine bits of shmflg.  The is set equal to the value of size.

	    The and are set equal to zero (0).	The shm_ctime is set equal to the current time.

Return Values
       Upon successful completion, a non-negative integer, namely, a shared memory identifier is returned.  Otherwise, a value of -1  is  returned
       and errno is set to indicated the error.

Diagnostics
       The system call fails if any of the following is true:

       [EINVAL]       The size is less than the system-imposed minimum or greater than the system-imposed maximum.

       [EACCES]       A  shared  memory  identifier  exists for key, but operations permission, as specified by the low-order nine bits of shmflg,
		      would not be granted.  For further information, see

       [EINVAL]       A shared memory identifier exists for key, but the size of the segment associated with it is less than size and size is  not
		      equal to zero.

       [ENOENT]       A shared memory identifier does not exist for key, and (shmflg & IPC_CREAT ) is false.

       [ENOSPC]       A  shared  memory  identifier  is to be created, but the system-imposed limit on the maximum number of allowed shared memory
		      identifiers would be exceeded.

       [ENOMEM]       A shared memory identifier and the associated shared memory segment are to be created, but the amount of available  physical
		      memory is not sufficient to fill the request.

       [EEXIST]       A shared memory identifier exists for key, but ((shmflg & IPC_CREAT ) and (shmflg & IPC_EXCL )) is true.

See Also
       shmctl(2), shmop(2), ftok(3)

																	 shmget(2)
All times are GMT -4. The time now is 09:10 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy