Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 19th, 2008, 9:22 AM   #1
secrecy230
Newbie
 
Join Date: May 2005
Posts: 11
Rep Power: 0 secrecy230 is on a distinguished road
Implement a queue using share memory?

I know how to build a queue in C.
However,what does it mean to "Implement a queue using shared memory?"
How to do it? and can someone provide me a start please?

thanks in advance!
secrecy230 is offline   Reply With Quote
Old May 19th, 2008, 11:27 AM   #2
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Re: Implement a queue using share memory?

I'm not entirely sure, but it might mean that instead of allocating memory on the heap for every node in the queue, that you instead allocate a "pool" of memory for the whole structure and assign memory from there.

For example, if you were writing a linked-list (can you implement a queue with a linked-list? I don't know too much about computer science-y stuff ) you might be able to do something like this:

c Syntax (Toggle Plain Text)
  1. typedef struct lnode {
  2. void *data;
  3. struct lnode *next;
  4.  
  5. int used_;
  6. } lnode;
  7.  
  8. #define LIST_MAX 5000
  9.  
  10. static lnode sharedmem[LIST_MAX];
  11.  
  12. static lnode *allocator( void ) {
  13. int i;
  14.  
  15. for ( i = 0; i < LIST_MAX; i++ ) {
  16. if ( ! sharedmem[i].used_ ) {
  17. sharedmem[i].used_ = 1;
  18. return &(sharedmem[i]);
  19. }
  20. }
  21.  
  22. return NULL;
  23. }
  24.  
  25. static lnode *make_lnode( void *data, lnode *next ) {
  26. lnode *n = allocator();
  27.  
  28. n->data = data;
  29. n->next = next;
  30.  
  31. return n;
  32. }

Alternatively, you could malloc() a block of memory within the structure of the ADT and keep a memory index.

EDIT: on second thought, look near the bottom of this page.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old May 19th, 2008, 1:50 PM   #3
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Implement a queue using share memory?

you need to google for "producer consumer problem" or "bounded buffer". you should find what you are looking for.
mbd is offline   Reply With Quote
Old May 21st, 2008, 2:58 AM   #4
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 265
Rep Power: 2 Fall Back Son is on a distinguished road
Re: Implement a queue using share memory?

I think you were joking but you can implement a queue a variety of ways lol.
Fall Back Son is offline   Reply With Quote
Old May 21st, 2008, 9:34 AM   #5
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Implement a queue using share memory?

if you think i am joking, then you are mistaken. obviously you can implement a queue in many ways. however if you are using memory shared between threads, you must provide protection for that shared resource. this is a classic comp sci problem which is probably what he is currently studying.
mbd is offline   Reply With Quote
Old May 21st, 2008, 11:50 AM   #6
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 265
Rep Power: 2 Fall Back Son is on a distinguished road
Re: Implement a queue using share memory?

No, I was talking to Jessehk or whatever his name is. I realize what the problem entails. I also don't understand why anyone would want you to do this in C, and I'm interested to know what kind of practical applications (In C) might use this.
Fall Back Son is offline   Reply With Quote
Old May 22nd, 2008, 11:24 AM   #7
mbd
Programmer
 
Join Date: Nov 2007
Posts: 86
Rep Power: 1 mbd is on a distinguished road
Re: Implement a queue using share memory?

fall back son, what language would you want to do this in?
mbd is offline   Reply With Quote
Old May 22nd, 2008, 3:27 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,888
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Implement a queue using share memory?

Of course you'd want to use C!

C is the language for implementing queues and/or linked lists, since it's basically the only commonly used language that doesn't already support lists or queues.

Let's look at TIOBE's list of the most popular programming languages as of May 2008.

Source: http://www.tiobe.com/index.php/conte...pci/index.html
1   Java            (Built-in support for queues)
2   C
3   Visual Basic    (Would not use this language)
4   PHP             (Would not use this language)
5   C++             (Built-in support for queues)
6   Perl            (Built-in support for queues)
7   Python          (Built-in support for queues)

Sure you could make your own queue in Java. But it's always more effective to demonstrate something in a language that does not have support for it.

That leaves C.

So as mbd asked, what language would you use?
Sane is offline   Reply With Quote
Old May 22nd, 2008, 8:22 PM   #9
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
Re: Implement a queue using share memory?

Quote:
Originally Posted by Fall Back Son View Post
No, I was talking to Jessehk or whatever his name is.
In the time you wrote that sentence, you could have scrolled up three posts and checked for yourself. Congratulations.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old May 25th, 2008, 1:03 AM   #10
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Re: Implement a queue using share memory?

I think everyone missed the mark here. "shared memory"most commonly means memory shared between processes, not threads. In this case a queue with shared memory could be used to create an interprocess message passing system. You still need to use some kind of semaphore or mutex mapped into shared memory in order to synchronize access to the queue between multiple processes.

You should first learn how to implement a queue, then learn the API for shared memory on your particular OS. Jessehk's code should be able to help you with some of it.
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
an efficient way to store/lookup memory addresses? rgba Software Design and Algorithms 8 Feb 13th, 2008 1:54 AM
Hexadecimal Memory Address Question 357mag C++ 1 Jul 8th, 2007 9:19 PM
queue - shared memory programmingnoob C++ 4 Mar 26th, 2007 8:19 PM
Heap vs. Stack memory Eric the Red C++ 11 Oct 24th, 2006 6:18 PM
Queue again zman2245 C++ 3 Sep 9th, 2005 1:07 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:14 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC