I understood the mechanism of user threads mapping on kernel threads at thread level: now I'd like to understand the mechanism at process level.
An user thread can access the resources of his "father" process: when the user thread is mapped on a kernel thread, what is of the user process resources? And more:
We're talking of "kernel threads": threads of the same process share the resources of that process. Kernel threads have to work on different resources (the specific resources of the user process corrensponding to the user thread they're mapping). So each kernel thread belongs to a different "kernel process", that inherit the resources of the user process?
Sorry for my bad english, I hope you can understand.
From what I understand,
A thread is created at Kernel level, then for user-mode, it does a mode switch and the thread runs in user-mode. Now it can access it's resources in user-mode.
When a thread is running in kernel-mode, it can still access it's resources in user-mode.
You should check out these video's that explain how a thread is created and what the difference between user-mode and kernel-mode threads.
http://academicearth.org/courses/operating-systems-and-system-programming
Then there's also 'threads' that just run in kernel mode and cannot be accessed by a user-mode process.
I hope this helps.
Related
Is there an operating system-specific way in Linux/Darwin/Windows, to restrict access to certain virtual memory pages to only one thread, so that when another thread tries to access it, the OS would intercept and report an error?
I'm trying to emulate the behavior of fork with multiple processes, where each process has its own memory except for some shared memory, mainly to avoid all programming errors where one worker would access memory belonging to another worker.
As a general proposition, this is not possible. The whole idea of threads is to have multiple streams of execution that share the same address. If you're a kernel mode kommando, you might be able to some up with some modification of the page tables that a thread uses to make pages inaccessible from usermode then unlocks them.
Does a thread that is blocked cause the process to become blocked? Why and How? Thanks to all experts for answering.
A process cannot be blocked because the concept of "blocked" only applies to a thread of execution. The only meaningful sense in which you could say that a process was blocked is if the process only had one thread and that thread was blocked.
A thread is a flow of execution through the process code, with its own program counter, system registers and stack. A thread is also called a light weight process. Threads provide a way to improve application performance through parallelism. Threads represent a software approach to improving performance of operating system by reducing the overhead thread is equivalent to a classical process.
Each thread belongs to exactly one process and no thread can exist outside a process. Each thread represents a separate flow of control.Threads have been successfully used in implementing network servers and web server. They also provide a suitable foundation for parallel execution of applications on shared memory multiprocessors.
So, as you may have guessed, No ! A thread cannot block a process.
Like in process management and memory management.
Are the scheduler and memory manager implemented as kernel threads that are run on the cpu the moment they are needed? If not, how does the kernel treat them?
Are they like processes, tasks, or some line of code that gets executed when needed?
Some are, some aren't. The terms "process management" and "memory management" are kind of broad and cover a fair bit of kernel code.
For memory management, a call to mmap() will just require changing some data structures and can be done by the current thread, but if pages are swapped out it will be done by kswapd, which is a kernel thread.
You might consider the scheduler a special case: since the scheduler is responsible for scheduling all threads, it itself is not a thread and does not execute on any thread (otherwise it would need to schedule itself... but how would it schedule itself, if it had to schedule itself first in order to do that?). You might think of the scheduler as running directly on each processor core when necessary.
I stumbled into this question when I was reading “JVMs typically implement blocking by suspending the blocked thread and rescheduling it later” from http://www.ibm.com/developerworks/java/library/j-jtp04186/?S_TACT=105AGX52&S_CMP=cn-a-j
When we say a process or thread gets blocked when doing IO operations (read, write) or getting access to some exclusive resource (lock, synchronized), when will it get to re-execute? are they constantly waiting until getting a notification from somewhere or does it simply quit its turn and run again after a while?
Has it anything to do with the specified platform? os or jvm?
That would devolve to the underlying OS that must provide threading support to the VM - has to be that way so that the Java app can co-exist harmoniously with all the other proceses and threads that are typically loaded on an OS - browsers, sidebars, anitvirus, video/audio players, Torrent clients, OS internal threads etc. etc.
The code of a blocked thread gets no CPU cycles at all. A thread in that state is just an unused-for-now stack allocation and an extra struct/class pointer in a container in the kernel, waiting for something else to change its state. If it remains blocked or an extended time, the stack may even get swapped out on a busy system.
So yes, they constantly waiting until getting a notification from somewhere.
How to protect a shared resource from both user processes and kernel processes simultaneously?. It's rare and very corner case. I have been asked this query in an interview.
TIA
Well, it can be done by several way.
One such way is
System Call
Create two system call, one to acquire a lock and one to release a lock. If user process wants to access the shared resource, it would call the acquire-lock system call. If the system call successfully returns, user process can access the shared resource. When user process is done it would release the lock, by calling the release system call. The system calls itself acquire a release a spinlock_t or mutex_t(or any other locking mechanism). The kernel processes that wants to access the shared resource has to acquire the same lock using spin_lock/spin_unlock or mutex_lock/mutex_unlock.
As pointed out by #Damon this is very generic questions, and you should be asking specific question to the interview inorder to give a concrete answer.