Microsoft Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
Process
Each process provides the resources needed to execute a program. A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution. Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.
Thread
A thread is the entity within a process that can be scheduled for execution. All threads of a process share its virtual address space and system resources. In addition, each thread maintains exception handlers, a scheduling priority, thread local storage, a unique thread identifier, and a set of structures the system will use to save the thread context until it is scheduled. The thread context includes the thread's set of machine registers, the kernel stack, a thread environment block, and a user stack in the address space of the thread's process. Threads can also have their own security context, which can be used for impersonating clients.
another difference is that processes are "fork"ed and threads are "spawn"ed.
A fork (creating a process) is usually more time-consuming. This is a system call.
A spawn (creating a new thread) can be faster than forking a new process.
The first time you run a program, you create a new process (never a new thread). A process can have multiple threads, but not the other way around. (edited and corrected)
Pardon? Are you sure we can create a thread without invoking a system call into Kernel at some point?
fork is a linux/unix system library function to create a new process identical to the calling one
Are you sure spawn means create new thread, instead of fork+exec ?
"pthread_create" creates a new thread on unix/linux
both pthread_create and fork call "clone" on linux (they both require the linux kernel's intervention)
It definitely is possible to create User Level Threads without a system call, but you are right, this does not have to be so, as there are also Kernel Level Threads.
1. Threads are easier to create than processes since they
don't require a separate address space.
2. Multithreading requires careful programming since threads
share data strucures that should only be modified by one thread
at a time. Unlike threads, processes don't share the same
address space.
3. Threads are considered lightweight because they use far
less resources than processes.
4. Processes are independent of each other. Threads, since they
share the same address space are interdependent, so caution
must be taken so that different threads don't step on each other.
This is really another way of stating #2 above.
5. A process can consist of multiple threads.
Suppose I have a program that does the following steps
1. Read a file
2. Read data from a database
3. Compute some values from the info in the file received and the data
4. display something on the screen
When this is realized into code, its a program. When the program is executed, an image of it is created (with some resources such as file handle, etc.) and loaded into main memory and scheduled to run on the processor. Now its a process. Single program be used to instantiate multiple processes.
If we look closely, we can perform steps 1 and 2 independently, then 3rd and then 4th. Do we really need to wait for 1 to finish before beginning 2? To make things work parallel, we can run 1 and 2 parallel in smaller partitions of the process. This partition of the process, which has some resources inherited and its own context within the process is a thread. Threads are lighter weight and help improve throughput since we don't need to wait for executing independent tasks even within a process.
A process can contain multiple threads.
That is to say that a thread is the smallest unit of execution of a process.
A process can basically be divided to have two units-
1.Resource ownership
resources like virtual address space , main memory , I/O devices and files are owned by the process
2.execution unit->this is the one that is generally often called thread
this unit is totally independent from the resource ownership unit.
threads have their shared address space and individual private address space.
They are a means to divide a process into various modules that can execute independently.
I found a very nice explanation somewhere, so am pasting it here :
- teli.vaibhav October 27, 2013A process is an executing instance of an application, for example when we double click on MSWord, a process is launched. A thread on the other hand is only a path of execution within a process.
A process can contain multiple threads. When you start Word, the operating system creates a process and begins executing the primary thread of that process.
A thread can do anything a process can do. But since a process can consist of multiple threads, a thread could be considered a ‘lightweight’ process. Thus, the essential difference between a thread and a process is the work that each one is used to accomplish. Threads are used for small tasks, whereas processes are used for more ‘heavyweight’ tasks – basically the execution of applications.
Another difference between a thread and a process is that threads within the same process share the same address space, whereas different processes do not. This allows threads to read from and write to the same data structures and variables, and also facilitates communication between threads.