We mainly study about heap and stack data structures which act as storage allocation area in memory. Is it that queue is also a storage allocation area in memory? As far as I know, process control blocks are made in OS region and actually they also act as storage allocation area only so can I consider queue to be a storage allocation area in memory?
2 Answers
You've been misled by homonyms.
There is a data structure called stack: a container where items can be added at any time but only the most recently added can be removed. And there is an area in memory called stack, which is where function arguments, local variables, return values and return addresses are stored¹. There's one stack memory area per thread. The two concepts have the same name because the stack memory area is organized as a stack data structure: an item is added when a function call is executed, and it is removed upon return from a function; since function calls are nested, the item that's removed is the last one that was added and not yet removed.
There is a data structure called heap: a tree of ordered items where each node holds a smaller value than all of its children². And there is an area in memory called heap, which is where storage is allocated dynamically. There is one heap per process. The two concepts are completely unrelated, they have the same name because there's a finite supply of English words with roughly the right connotations and it happens that the same word was picked for both.
There is no reason why a word that is a name for a data structure should also be a name for an area in memory. There can be queue data structures in an operating system, of course, but there's no area in memory that's called “the queue”.
Process control blocks are not organized as a queue, and they are not a storage allocation area, they're just a common name for the place in memory where the operating system stores information about each process (and not every operating system has all the information in one place, so not every OS's description mentions a “process control block”).
¹ This is just the 10000-foot view. Return addresses and arguments could be placed in different areas, some arguments typically end up in registers, etc.
² That's a min-heap; a max-heap is a tree where each node holds a larger value than all of its children.
-
$\begingroup$ Sir , can you please explain why do we have one stack per thread and one heap per process , In a function call in one thread I may dynamically allocate something so then why can't I have one heap per thread , couldn't get the meaning of this $\endgroup$– radhikaDec 22, 2015 at 7:36
-
$\begingroup$ @radhika Threads of the same process share the same heap. You can allocate memory in one thread and use it from another thread. That's basically the definition of a thread as opposed to a process. (That's the general case, some languages have multiple heaps, but it's very rare.) $\endgroup$ Dec 22, 2015 at 10:41
it seems that you haven't studied computer architecture yet. I'll try to explain. You can't in OS use queue to be a storage location in memory because the heap and stack is implented in the hardware. To use queue as a storage area you'll need do your own hardwares. To know how the heap work see: http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html.
-
$\begingroup$ A heap can't be implemented (easily) in hardware, a stack can. A queue would wander along the memory, so is also hard to do cleanly in hardware. $\endgroup$– vonbrandDec 20, 2015 at 1:57