1

I have been reading OS these days and I am getting more and more confused..

Q1: Suppose we have multiple threads for a process. So in the physical memory, there would be memory allocated for the heap, code and data segment. Apart from this, there would be a separate stack memory for each thread. How are the stacks arranged? How do we ascertain the space a thread would need? What if a thread overuses the stack space it was allocated?

Q2: In case of a context switch, in which another thread of the same process is activated, we save the PC, registers, stack pointer and the return address of the interrupted thread. What exactly is this return address? The PC is sufficient to tell us which instruction would be executed next, so what is the use of this return address? Please help me :(

1 Answer 1

3

'How are the stacks arranged?' - dynamically allocated by the 'CreateThread()', (or whatever it's called), API, called from the OS loader for the main thread, and subsequently by the main thread and others to create more threads.

'How do we ascertain the space a thread would need?' - on desktop systesm/OS with large virtual memory spaces, compilers/linkers usually specify a large maximum size, (eg. 1MB), for the stack of the main thread. This value is stored in the executable file header and used by the OS to reserve the 1MB, or whatever, of virtual address space when the executable is loaded. The OS commits to physical RAM/paging a much smaller minimum stack when the main, or other, thread is started.

'What if a thread overuses the stack space it was allocated?' - if a thread overuses its committed stack, a page-fault interrupt is generated and, if the reserved stack limit is not reached, more RAM is committed to extend the stack. If the limit is reached, a small 'overdraft' of a page or two is committed and an exception raised in the offending thread.

'return address of the interrupted thread. What exactly is this return address? The PC is sufficient to tell us which instruction would be executed next, so what is the use of this return address?' Typically, the PC is not explicitly saved. The return address is saved on the stack by the hardware/software interrupt mechanism that generated the entry to the OS code from the user app code. So, saving the stack-pointer efectively saves the PC, (and often a pile of registers etc. as well, before a switch is made to an OS stack to implement the interrupt/system call).

Sign up to request clarification or add additional context in comments.

2 Comments

Alright thank you very much for your answer. just one clarification, you mean to say whenever a thread is created, the OS creates a virtual memory of 1MB(for example), while actually allocating a very few bytes in the RAM? If the thread needs more space, more RAM is allocated(upto 1 MB) after which an exception is raised. Did i get it right? Please if you clarify.
OS reserves a virtual memory of 1MB(for example), while committing some pages of virtual memory, (often 4 pages of 4K each). These committed pages are available for loading/swapping into RAM. If the thread runs out of committed stack space, needs more virtual memory for stack and has not reached its 1MB limit, more virtual memory is committed, some is paged into RAM and the instruction that caused the page fault is restarted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.