C/C ++ written test method 01-System

Source: Internet
Author: User

1. Differences between processes and threads.

A thread is an execution unit in a process and a schedulable entity in the process.
Differences from processes:
(1) scheduling: A thread is the basic unit of scheduling and allocation, and a process is the basic unit of resource ownership.
(2) concurrency: not only can concurrent execution be performed between processes, but also between multiple threads of the same process.
(3) Owning resources: a process is an independent unit with resources. A thread does not own system resources, but can access resources belonging to a process.
(4) system overhead: when a process is created or withdrawn, because the system allocates and recycles resources for it, the overhead of the system is significantly greater than the overhead when a thread is created or withdrawn.


2. If you only want to run one instance of the program, you cannot run two instances. Like winamp, only one window can be opened. How can this problem be implemented?

Use memory ing or global atomic (mutex variable), find the window handle FindWindow, mutex, write the flag to the file or registry, and share the memory.


3. How to intercept the keyboard response and change all 'A' to 'B '?

Keyboard hook SetWindowsHookEx


4. When the program should use the thread, and when the single thread is highly efficient.

1. Time-consuming operations use threads to improve application response

2. threads are used for parallel operations, such as the concurrent threads on the server side in the C/S architecture to respond to user requests.

3. In multiple CPU Systems, threads are used to improve CPU utilization

4. Improve the program structure. A long and complex process can be considered to be divided into multiple threads and become several independent or semi-independent running parts. Such a program will facilitate understanding and modification.

In other cases, a single thread is used.


5. Does Linux have kernel-level threads?

A: A thread is usually defined as a different execution route of code in a process. There are two types of threads: user-level threads and kernel-level threads ". A user thread is a thread implemented in a user program without kernel support. It does not depend on the core of the operating system, the application process uses the thread library to provide functions for creating, synchronizing, scheduling, and managing threads to control user threads. This kind of thread can be implemented even in operating systems like DOS, but the thread scheduling needs to be completed by the user program, which is similar to Windows 3.x collaborative multitasking. In addition, the kernel needs to participate in thread scheduling. It depends on the operating system core and is created and abolished by the internal requirements of the kernel. These two models have their own advantages and disadvantages. The user thread does not require additional kernel expenses, and the Implementation Mode of the user State thread can be customized or modified to meet the requirements of special applications, however, when a thread is in the waiting state due to I/O, the whole process will be switched to the waiting state by the scheduler, and other threads will not be able to run; there are no restrictions on Kernel threads, which are conducive to the advantage of multi-processor concurrency, but occupy more system expenses. Windows NT and OS/2 support kernel threads. Linux supports kernel-level multithreading.


6. How does a thread prevent large peaks?

A: It indicates how to prevent a large number of threads from being generated at the same time by using the thread pool. The thread pool can improve the scheduling efficiency and limit resource usage at the same time, when the maximum number of threads in the thread pool is reached, other threads will wait in queue.


7. Communication between processes
Shared Storage System
Message Transmission System
Pipelines: Based on file systems


8. Cause of process deadlock
Illegal resource competition and process Promotion order


9. deadlock handling
Ostrich policy, prevention policy, avoidance policy, detection and deadlock Removal


10. What are the Process Scheduling Policies in the operating system?
FCFS (service first), priority, time slice rotation, multi-level feedback


11. What are the differences and connections between threads and processes? Does a thread have the same stack? Does the dll have an independent stack?

The process is dead, but only a collection of resources. Real program execution is completed by threads. When the program starts, the operating system creates a main thread for you.

Each thread has its own stack.
Whether there is an independent stack in the DLL. This question is hard to answer, or whether the question itself has a problem. Because the code in the DLL is executed by some threads, only the thread has a stack. If the code in the DLL is called by the thread in the EXE, does it mean that the DLL does not have its own stack? If the DLL code is executed by a thread created by the DLL itself, does the DLL have an independent stack?

The above is about the stack. For the heap, if each DLL has its own heap, it is best to delete it from the DLL if it is dynamically allocated from the DLL, if you allocate memory from the DLL and delete it in the EXE or another DLL, it is very likely that the program will crash.


12. Which of the following operations on int variable x need to be synchronized using multiple threads:ABC
A. x = y; B. x ++; C. ++ x; D. x = 1;




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.