I am looking for C program source code. Could you please help me in finding the source code of the required mentioned below.
Task that I need to complete:
Write a program to create multiple threads (one master thread and rest worker threads) and using the threads write into and read from shared memory
Restrictions:
Only one thread should be able to read at any instant of time into shared memory region.
Only one thread should be able to write at any instant of time into shared memory region.
Worker threads should inform the master thread after every read and write operation.
Master thread inform the worker thread (which is waiting to acquire shared memory region) that read or write operation is successful and it can acquire the memory for either reading and writing operations.
zerver
June 18th, 2008, 10:47 AM
Sorry, your question is too vague to answer.
Members of this forum like to help with specific programming related questions.
If you need help with "the whole thing" this is the wrong place.
There are programming consultants that can help you, but they usually don't work for free.
To get you started, I can say that (with few exceptions) all memory is shared between threads. So, you don't need to take any special action to make it shared. But you have to protect the data access using mutexes.
http://msdn.microsoft.com/en-us/library/tt45160e(VS.71).aspx
Arjay
June 19th, 2008, 01:24 PM
So, you don't need to take any special action to make it shared. But you have to protect the data access using mutexes.Slight correction. "But you have to protect the data access using synchronization (which can be a critical section, mutex, semaphore, or event)."
See the articles listed in my subject line for various techniques are safely sharing data between threads.
DreamShore
July 3rd, 2008, 02:03 AM
Something to consider:
1. Why should only one thread read? Several thread read one data shouldn't hurt usually, as long as they don't write back depending on what they've read.
2. Are you planning that one thread can read while another is writing? That hurts.
3. Better divide the shared region into exclusive parts if it can be done so. That may boost the performance.
kempofighter
July 28th, 2008, 11:20 AM
Something to consider:
1. Why should only one thread read? Several thread read one data shouldn't hurt usually, as long as they don't write back depending on what they've read.
2. Are you planning that one thread can read while another is writing? That hurts.
3. Better divide the shared region into exclusive parts if it can be done so. That may boost the performance.
Sounded like a homework assignment to me where the student was a procrastinator looking for someone to write his code for him.
skatyayan
October 8th, 2008, 02:18 PM
Hello All,
I need a help on shared memory. I have written a piece of code, let me describe about the code first. In the following attached program(please see the attachment) I am creating multiple threads(to read and write) and a shared memory. For each writer thread I have multiple reader threads. I can create upto 40 threads maximum (for each reader and writer) on AIX system. There is a output of the program named ("file" and "output") is also attached for your reference. Here is the code flow:
And a shared memory which is accessible to reader and writer threads (fully synchronised, according to me).
Problem Summary: What I am trying to do....
Now I need to divide this shared memory into multiple blocks, in such a way....
a) Writer thread 1 should write some data into first block of shared memory as follows:
BLOCK_ID (1 BYTE or less ) + THREAD_ID (1 BYTE or less) + STRING (LENGTH BYTE OF STRING).
for e.g: 01 (Block number) + 32 (thread_id) + "Shared memory content Written by writer thread 32"
once the writer thread is done it should free the lock and inform to other threads which might be waiting for that particular block of shared memory.
b) reader thread 1 should get the notification that the requested block of shared memory is available and it should lock that particular block of memory before accessing it. Once it has locked the memory, the reader thread should be able to modify the data only two fields and BLOCk_ID should reamin untouched.
for e.g: 01 (Block number unaltered) + 25 (thread_id) + "Shared memory content altered by reader thread 25"
I have created a structure but not able to write it into shared memory (writer function is failing for me)
typedef struct
{
int block_ID;
int thread_ID;
char string[1024];
}DataBlock;
DataBlock *readerthread, *writerthread;
Any input/hints will be of great help for me. Please provide hints at code level, pseudo-level may not be very helpful for me.
kempofighter
October 8th, 2008, 03:05 PM
Any input/hints will be of great help for me. Please provide hints at code level, pseudo-level may not be very helpful for me.
In other words, please write the code for him instead of trying to help him to do it himself so that he can submit his homework assignment more quickly.
TheCPUWizard
October 8th, 2008, 03:27 PM
Been watching this thread and waiting for a peice of minimal yet complete code and specific questions.... haven't seen it.
Based ona few of the past posts, it would not sup-rise me if the little bit of code that has been posted can from someone else...
Those who show an honest effort, get the best results.....
skatyayan
October 9th, 2008, 03:57 AM
I didn't asked you to write the code for me. I just asked the help at code-level doesn't mean that one should write the code for me. I do know this or any other group is there to help, but not to finish some one else work.
It's not surprising for me, and I can't change the others thoughts. But one thing I do know and would like to point that how much code do we write our own (without any help from google, books and forums). Of course almost zero(at least in my case), don't know about others.
Have studied some basics from the books (R. stevens, and some other online material), start with the problem, if you stuck up in the middle ask for help. I don't have any rights to ask other people that whether they have written their own theory (unique), implemented it.
I have not done so far, I have been reading the C book (written by Ritchie) N/W programming (stevens, comer and etc....). So it's all of others.....
I am not going to validate that I have written the code, but I have.... It'll be great if you can help rather than making comments (which is not beneficial to any other memberes in the group).
Thanks a lot for all your help and comments.
TheCPUWizard
October 9th, 2008, 07:32 AM
The best way to get help (especialoly on CodeGuru, but it applies to nearly everywhere) is to post sufficient information (code + requirements) so that someone here can replicate the problem. Then describe specifically what steps have already been taken plus exactly where you are stuck. This is all covered in the FAQ's.
Additionally (and this would also be known if the time was taken to read the FAQs), proper use of [ Code ] tags makes reading posts which contain code (or any other formatted information) much easier, and therefore more likely to be quickly understood.
You have not put in either of these efforts, which is the primary reason that you are not getting the responses you seek.
If you had taken the time to enable private messaging (also in the FAQs), then posts such as this would have been sent to you privately rather than on the forum itself.
SO, if you want to get responses, I kindly suggest that you spend some time on the FAQs (many of which are directly linked in my signature as well as the signatures of others) and re-formulate your posts. :wave: :wave:
skatyayan
October 9th, 2008, 01:45 PM
I am trying to divide the shared memory into multiple blocks in such a way that
Problem 1:
writer thread1 should write into first block of shared memory and another thread reader thread1 should read and modify the date by referring to a particular block of shared memory
Attempt which I made:
I have created a structure as follows and using that structure pointer trying to write some data into shared memory, and with another structure pointer variable I am trying to read the data. But both the reader and writer failing and the reason is not known to me. Please provide me some input on this
typedef struct{
int block_id;
int thread_id;
char string[1024];
}DataBlock;
DataBlock* writeparm;
DataBlock* readparm;
Please see the writer.c file and reader.c file in the attachment.
Problem 2:
I am trying pass one more variable to reader and writer functions in reader and writer file but this also fails, as of now I am passing only one variable to these functions as parameters.
void *reader(void *);
void *writer(void *);
What I am trying to attempt:
If I am able to pass two variables to both these function then other variable I can use as a file pointer and hopefully that may sort out the problem, but this also gives me strange kind of error. This is what I am trying to do
But when I tried this I couldn't succeed... Please give some on this also.
P.S-: I have attached the code (shmem.zip), please have a look at it.
TheCPUWizard
October 9th, 2008, 02:13 PM
OK, now we are getting somewhere...
Second problem first.
1) Dont use "void *" as an argument type if you know the type you want. Looking ar reader.c, it is obvious that the existing parameter should be an int.
2) If you want to add a second parameter to "reader" (or "writer"), remember that you must change it in both the header, and the implementation (as well as any place where it is used. Since this is "C" and not "C++", you can not do function overloading (meaning two functions with the same name but different parameters). If also appreas you have tried the correct approach of using a function with a different name ("*readerfunc", "writerfunc")
AS FOR writeparam (and readparam), you are declating them at the global scope, but appear to only use them locally to the appropriate function.
[accidently hit save....continuing]
You also have what appear to be redundant assigns.
writeparm = (DataBlock *)shmaddr;
for(i = 0; i < num_writers; i++)
{
DataBlock *writeparm = (DataBlock *)shmaddr[i];
Finally you have a part of your code marked as "All Junk", but provided no information about what was actually in the various variables and what you expected the values to be. This is a critical step, and is easily accomplished with the debugger.
:wave: :wave:
skatyayan
October 9th, 2008, 02:42 PM
thanks a lot.
On the second problem. I have changed the function in header as follows:
void *reader(void *, char *); /* can change it to int * reader (x, x)
void *writer(void *, char *); /* can change it to int * writer (x, x)
That's not causing the problem. The problem here is that I want to pass second arg. as a char * variable. But in the main since its an integer index so it's not allowing me to pass second arg as char. First of all can we do that, if yes then how
My apologies, I did not realize that it was being passed via CreateThread.
It MUST be one argument and it must be of type "Void *"...but all is not lost...
struct MyParams
{
int X;
std::string s;
// etc...
}
Now you can add as many members to MyPArams, and pass a single instance of it to your function.
skatyayan
October 9th, 2008, 03:55 PM
Yes we're lending towards the problem and getting involved into it. Now you're agree with the structures declarations and other stuffs. May I request you to look into the code once again specially reader and writer functions.
When I am passing the input as
./main -r 1 -w 1 -s 100 (from the command prompt, it never return backs to the prompt again)
And when I am giving the input
./main -r 10 -w 10 -s 100
It gives me seg fault, while debugging it using dbx it fails in main somwhere in the line number 173 (not sure, will reproduce the error message and can send only tomorrow, don't have AIX machine at home).
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.