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
explain_shmat(3)					     Library Functions Manual						  explain_shmat(3)

NAME
explain_shmat - explain shmat(2) errors SYNOPSIS
#include <libexplain/shmat.h> const char *explain_shmat(int shmid, const void *shmaddr, int shmflg); const char *explain_errno_shmat(int errnum, int shmid, const void *shmaddr, int shmflg); void explain_message_shmat(char *message, int message_size, int shmid, const void *shmaddr, int shmflg); void explain_message_errno_shmat(char *message, int message_size, int errnum, int shmid, const void *shmaddr, int shmflg); DESCRIPTION
These functions may be used to obtain explanations for errors returned by the shmat(2) system call. explain_shmat const char *explain_shmat(int shmid, const void *shmaddr, int shmflg); The explain_shmat function is used to obtain an explanation of an error returned by the shmat(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. shmid The original shmid, exactly as passed to the shmat(2) system call. shmaddr The original shmaddr, exactly as passed to the shmat(2) system call. shmflg The original shmflg, exactly as passed to the shmat(2) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. Example: This function is intended to be used in a fashion similar to the following example: void *result = shmat(shmid, shmaddr, shmflg); if (!result) { fprintf(stderr, "%s ", explain_shmat(shmid, shmaddr, shmflg)); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_shmat_or_die(3) function. explain_errno_shmat const char *explain_errno_shmat(int errnum, int shmid, const void *shmaddr, int shmflg); The explain_errno_shmat function is used to obtain an explanation of an error returned by the shmat(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. shmid The original shmid, exactly as passed to the shmat(2) system call. shmaddr The original shmaddr, exactly as passed to the shmat(2) system call. shmflg The original shmflg, exactly as passed to the shmat(2) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. Example: This function is intended to be used in a fashion similar to the following example: void *result = shmat(shmid, shmaddr, shmflg); if (!result) { int err = errno; fprintf(stderr, "%s ", explain_errno_shmat(err, shmid, shmaddr, shmflg)); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_shmat_or_die(3) function. explain_message_shmat void explain_message_shmat(char *message, int message_size, int shmid, const void *shmaddr, int shmflg); The explain_message_shmat function is used to obtain an explanation of an error returned by the shmat(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. shmid The original shmid, exactly as passed to the shmat(2) system call. shmaddr The original shmaddr, exactly as passed to the shmat(2) system call. shmflg The original shmflg, exactly as passed to the shmat(2) system call. Example: This function is intended to be used in a fashion similar to the following example: void *result = shmat(shmid, shmaddr, shmflg); if (!result) { char message[3000]; explain_message_shmat(message, sizeof(message), shmid, shmaddr, shmflg); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_shmat_or_die(3) function. explain_message_errno_shmat void explain_message_errno_shmat(char *message, int message_size, int errnum, int shmid, const void *shmaddr, int shmflg); The explain_message_errno_shmat function is used to obtain an explanation of an error returned by the shmat(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. shmid The original shmid, exactly as passed to the shmat(2) system call. shmaddr The original shmaddr, exactly as passed to the shmat(2) system call. shmflg The original shmflg, exactly as passed to the shmat(2) system call. Example: This function is intended to be used in a fashion similar to the following example: void *result = shmat(shmid, shmaddr, shmflg); if (!result) { int err = errno; char message[3000]; explain_message_errno_shmat(message, sizeof(message), err, shmid, shmaddr, shmflg); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_shmat_or_die(3) function. SEE ALSO
shmat(2) shared memory attach explain_shmat_or_die(3) shared memory attach and report errors COPYRIGHT
libexplain version 0.52 Copyright (C) 2011 Peter Miller explain_shmat(3)
All times are GMT -4. The time now is 10:03 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy