Interview summary-process, threads and multithreading

Source: Internet
Author: User

threads, processes and multithreading is a very easy interview process problems, Baidu last year when the three sides encountered this problem, then Baidu HR asked: What is the difference between threads and processes? Do you write too many processes or multi-threaded programs? In your opinion, multi-threaded and multi-process programs that kind of program is more difficult to write?

The first question is routine, but it's not so easy to get it right, and now, the second and third question is actually a problem, because the third question is the best way to verify that the second question is telling the truth. In addition, in the Microsoft's written test this April 6, the first question was also taken, so what are the problems? If you want to review from which aspects to start?

First of all, from the perspective of interview questions, after online collection, I think will be asked the following questions.

1. What is the difference between a process and a thread?

This is one of the most common, but the most difficult to answer the question, csdn above a blogger given the answer and another Cnblog Bo Master's answer a little clearer, summed up, is a few differences:

A. A process is the basic unit of resource allocation, which is CPU scheduling, or the smallest amount of program execution. In a microkernel-based operating system such as Mac, Windows NT, the functionality of the process has changed: it is only the unit of the resource allocation, not the unit of the dispatch run. In a microkernel system, the basic unit of a real dispatch operation is a thread. Therefore, the unit that implements the concurrency feature is the thread.

B. The process has a separate address space, such as starting a new process under Linux, the system must be assigned to its own address space, the creation of a large number of data tables to maintain its code snippets, stack segments and data segments, which is a very expensive multi-tasking way of working. While running a thread in a process, they share most of the data, use the same address space, so start a thread, switch a line Chengyuan is faster than the process operation, the cost is much smaller. Of course, threads have their own local variables and stacks (note not heaps), such as creating a new process with _beginthreadex in Windows and requesting a block of data that belongs to the thread (_tiddata) while calling CreateThread.

C. Communication between threads is more convenient. The thread sharing data under the unified process (such as global variables, static variables), through which the communication is not only fast and convenient, of course, how to handle the synchronization and mutual exclusion of these accesses is the difficulty of writing multi-threaded program. Communication between processes can only be done through process communication.

D. from B, it can be easily concluded that multiple processes are more robust than multithreaded programs. A thread dies the entire process, but in protected mode, a process that dies does not have a direct impact on another process.

E. There is a difference between the execution of threads and the process. Each independent thread has its own program entry, sequential execution sequence and exit of the program, but the thread cannot execute independently, must be attached to the program, and the application provides concurrency control for multiple threads.

2. What is thread safety? (May 2012 Baidu intern Interview)

If a multi-threaded program runs as expected, and the result is the same as a single-threaded program, then the description is "thread safe."

3. The answer to the question in the second kill multi-threading

A. The concept of threads, the relationship between the basic state of threads and the state

In addition, the knowledge of the operating system in this website is quite detailed, there is another explanation threading concept

B. What are the different ways to implement multithreading?

This seemingly in the Java interview will appear, I am focused on C + +, ignoring, but have to say, the second kill multi-threaded face Test series is really a good summary

C. Multi-threaded synchronization and mutual exclusion there are several ways to implement? What's all this? (c + +)

Critical sections (cs:critical section), Events (event), mutexes (mutexes), semaphores (semaphores), it is important to note that the critical section is the most efficient, because there is essentially no additional overhead, and the two-core object involves switching between the user state and the kernel state. Overhead, in addition, the key segment, mutex has the concept of thread ownership, so it can only be used for mutual exclusion between threads, and can not be used in synchronization. Only the mutex can perfectly solve the "abandonment problem" caused by the unexpected termination of the process.

D. What are the similarities and differences between multi-threaded synchronization and mutex, and under what circumstances are they used separately? Examples Show

The so-called synchronization, indicating that there is a first, the more formal explanation is that "thread synchronization is a constraint between threads, one thread's execution depends on another thread's message, when it does not get another thread of the message should wait until the message arrives until the wake." "The so-called mutex, the more formal explanation is that" thread mutex refers to the exclusive nature of the shared process system resources when each individual thread accesses it. When there are several threads that want to use a shared resource, at most one thread is allowed to use at most, and other threads that want to use that resource must wait until the resource is freed by the resource. Thread mutexes can be seen as a special kind of thread synchronization. "means that you cannot access it at the same time, and it is a sequential problem, so mutual exclusion is a special synchronous operation.

For example, with a global variable, globally, in order to ensure thread safety, we stipulate that the next child thread can access global only if the main thread modifies the global, which requires the synchronization of the main thread and the child thread, which can be implemented with critical segments. When a sub-thread accesses global and another thread cannot access global, then mutual exclusion is required.

E. The following multi-threaded operation of the int variable x, which needs to be synchronized:
A. x=y;    B. x + +;    C. ++x; D. X=1;

The answer is ABC, obviously, Y's write with x read Y to synchronize, X + + and ++x all need to know the value prior to X., so also synchronize.

F. Multi-threaded stack and heap are public or private

A: Stack public, heap private

B: Stack public, heap public

C: Stack private, heap public

D: Stack private, heap private

The answer is C, the stack typically stores local variables, and programmers generally themselves apply for and release data from the heap (see the difference between heap and stack).

G. In Windows programming the mutex is similar to the critical section, please analyze the main difference between the two.

Summary of difference between critical area, mutex, Semaphore, event

For this topic, the main answers are as follows:

1) The mutex is a kernel object, so it is more resource-intensive than the critical section, but it can be named so that it can be accessed by other processes

2) from the point of view, the critical section is accessed through the serialization of multithreading to access public resources or a piece of code, fast and suitable for controlling data access.
Mutexes are designed to coordinate separate access to a shared resource together.

H. A global variable tally, two threads executing concurrently (code snippet is ThreadProc), asking two threads to end, tally range of values.

inttally = 0;//globalvoidThreadProc() {        for(inti = 1; i <= 50; i++)             tally += 1;}

When two threads are running, the result is a maximum of 100, and when a thread runs at the end, and another line Cheng gang out 0, the result is a minimum of 50 when it is not computed.

   

Interview summary-process, threads and multithreading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.