C basic knowledge

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 of 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. Test Method

Manual testing: personal review, spot check, and review
Machine testing: black box testing and white box testing

Black box method: This method regards the tested object as a black box. The tester does not consider the internal structure and processing process of the program. the test is only performed at the software interface, according to the specification. check whether the program meets the functional requirements. therefore, black box testing is called function testing or data-driven testing/white box method: This method regards the test object as an open box. The tester must understand the internal structure and processing process of the program, 1. Check the details of the processing process, test as many logical paths as possible in the program, and check whether the internal control structure and data structure are wrong, the actual running status is consistent with the expected status.

2. Differences between heap and stack.

Heap is a stack, and stack is a stack.
Stack space is automatically allocated/released by the operating system, and heap space is manually allocated/released.
Stack space is limited, and heap is a large free storage zone.
The memory allocated by the malloc function in C is on the heap, and the c ++ corresponds to the new operator.
During the compilation period, the program performs variable and function allocation memory on the stack, and the parameter transfer is also performed on the stack when the function is called during the program running.

8. Talk about the paging mechanism under ia32

Small page (4 K) two-level paging mode, large page (4 m) level

9. For two variables, how can we find out where a ring exists in a single linked list with a ring?

One increments one and the other increments two. When they point to the same point, the ring appears.

11. 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) to find the window handle ..
Findwindow, mutually exclusive, write flag to file or registry, shared memory ..

12. How can I intercept the keyboard response and change all 'A' to 'B '?

Keyboard hook setwindowshookex

17. What is the difference between designing concurrent servers in network programming and using multiple processes and multithreading?

1. Process: The child process is a replica of the parent process. The child process obtains copies of the data space, heap, and stack of the parent process.

2. Thread: A thread is a concept closer to the execution body than a process. It can share data with other threads of the same process, but has its own stack space, it has an independent execution sequence.

Both of them can increase the concurrency of the program and improve the program running efficiency and response time.

The use of threads and processes has their own advantages and disadvantages: the thread execution overhead is small, but it is not conducive to resource management and protection. The process is the opposite. At the same time, threads are suitable for running on SMP machines, while processes can be migrated across machines.

Static
6. What is the role of the keyword static?
Few people can answer this simple question completely. In the C language, the keyword static has three obvious functions:
1) In the function body, a variable declared as static remains unchanged when the function is called.
2) in a module (but in the function body), a variable declared as static can be accessed by all functions in the module, but not by other functions outside the module. It is a local global variable.
3) in a module, a function declared as static can only be called by other functions in the module. That is, this function is restricted to use within the local scope of the module that declares it.
Most of the respondents can answer part 1 correctly. Some of them can answer part 2 correctly. Few people can understand part 3. This is a serious disadvantage of a candidate because he obviously does not understand the benefits and importance of localized data and code scope.

7. What does the const keyword mean?
As soon as I hear the subject say, "const means constant", I know that I am dealing with an amateur. Last year Dan
Saks has fully summarized all usage of const in his article, So ESP (TRANSLATOR: Embedded Systems
Programming) every reader should be very familiar with what const can do and cannot do. If you have never read that article, just say that const means "read-only. Although this is not a complete answer, I accept it as a correct answer. (If you want to know more detailed answers, read the Saks Article carefully .)
If the examinee can answer this question correctly, I will ask him an additional question: what does the following statement mean?
Const int;
Int const;
Const int *;
Int * const;
Int const * a const;
/******/
The first two functions are the same. A is a constant integer. Third, it means that a is a pointer to a constant INTEGER (that is, an integer cannot be modified, but a pointer can be ). Fourth, a is a constant pointer to an integer (that is, the integer to which the Pointer Points can be modified, but the pointer cannot be modified ). The last one means that A is a constant pointer to a constant INTEGER (that is, the pointer to an integer cannot be modified, and the pointer cannot be modified ). If the examinee can answer these questions correctly, he will leave a good impression on me. By the way, you may ask, even if you don't need a keyword, you can easily write a program with the correct function. Why do I need to pay attention to the keyword const so much? I also have the following reasons:
1) The function of the keyword const is to send very useful information to the person who reads your code. In fact, the purpose of declaring a parameter as a constant is to inform the user of the application of this parameter. If you spend a lot of time cleaning up the garbage left by others, you will soon learn to thank this excess information. (Of course, programmers who know how to use const seldom leave garbage for others to clean up .)
2) by attaching some information to the optimizer, using the keyword const may produce more compact code.
3) the rational use of the keyword const can enable the compiler to naturally protect those parameters that do not want to be changed, so as to prevent them from being accidentally modified by code. In short, this can reduce the occurrence of bugs.

Volatile
8. What does the keyword volatile mean? Three different examples are provided.
A variable defined as volatile means that this variable may be unexpectedly changed, so that the compiler will not assume the value of this variable. Precisely, the optimizer must carefully re-read the value of this variable every time when using this variable, rather than using the backup stored in the register. The following are examples of volatile variables:
1) Hardware registers of parallel devices (for example, Status Registers)
2) Non-automatic variables that will be accessed in an interrupt service subroutine)
3) variables shared by several tasks in multi-threaded applications
People who cannot answer this question will not be hired. I think this is the most basic problem to distinguish between C programmers and embedded system programmers. Embedded people often deal with hardware, interruptions, RTOS, etc. All of these require volatile variables. If you do not know volatile content, it will lead to disasters. If the subject correctly answers this question (well, I suspect it will be the case), I will go a little deeper to see if this guy understands the full importance of volatile.
1) can a parameter be const or volatile? Explain why.
2) can a pointer be volatile? Explain why.
3); what are the following function errors:
Int square (volatile int * PTR)
{
Return * PTR ** PTR;
}
The answer is as follows:
1) Yes. One example is read-only status registers. It is volatile because it may be unexpectedly changed. It is const because the program should not try to modify it.
2); yes. Although this is not very common. One example is when a service subroutine repairs a pointer to a buffer.
3) This code is a little abnormal. The purpose of this Code is to return the pointer * PTR points to the square of the value. However, since * PTR points to a volatile parameter, the compiler will generate code similar to the following:
Int square (volatile int * PTR)
{
Int A, B;
A = * PTR;
B = * PTR;
Return a * B;
}
* The value of * PTR may be unexpectedly changed, so a and B may be different. As a result, this Code may not return the expected square value! The correct code is as follows:
Long square (volatile int * PTR)
{
Int;
A = * PTR;
Return a *;
}

# Define bit3 (0x1 <3)
Static int;
Void set_bit3 (void)
{
A | = bit3;
}
Void clear_bit3 (void)
{
A & = ~ Bit3;
}

Accessing fixed memory locations)
10. embedded systems often require programmers to access a specific memory location.
In a project, the value of an integer variable whose absolute address is 0x67a9 must be set to 0xaa66. The compiler is a pure ANSI compiler. Write code to complete this task. Test whether you know that it is legal to forcibly convert an integer number (typecast) into a pointer to access an absolute address. The implementation of this problem varies with the individual style. The typical code is as follows:
Int * PTR;
PTR = (int *) 0x67a9;
* PTR = 0xaa55;

Dynamic Memory Allocation (dynamic memory allocation)
14. Although not as common as embedded computers, embedded systems still have the process of dynamically allocating memory from heap. In the embedded system, what are the possible problems with dynamic memory allocation? Here, I expect the examinee to mention memory fragmentation, fragment collection issues, variable holding time, and so on. This topic has been widely discussed in the ESP magazine (mainly P. j. plauger, his explanation is far more than anything I can mention here). Let's look back at these magazines! After the examinee enters a false sense of security, I come up with a small program: What is the output of the following code snippets? Why?
Char * PTR;
If (PTR = (char *) malloc (0) = NULL)
Puts ("got a null pointer ");
Else
Puts ("got a valid Pointer ");
This is an interesting question. Recently, one of my colleagues inadvertently passed the 0 value to the malloc function. After obtaining a valid pointer, I came up with this question. This is the above Code. The output of this code is "got a valid Pointer ". I used this to discuss such a problem and see if the subject thought of the library routine. It is important to get the correct answer, but the solution to the problem and the basic principles of your decision are more important.

15 typedef
The C language is frequently used to declare an existing data type. You can also use a pre-processor to do similar things. For example, consider the following example:
# Define DPS struct s *
Typedef struct s * TPS;
The intention of the above two cases is to define the DPS and TPS as a pointing structure s pointer. Which method is better? (If so) Why?
This is a very subtle question. Anyone should be congratulated on answering this question (the legitimate reason. The answer is: typedef is better. Consider the following example:
DPS P1, P2;
TPS P3, P4;
The first extension is
Struct s * P1, P2;
The code above defines P1 as a point to the structure, and P2 as an actual structure, which may not be what you want. The second example correctly defines the P3 and P4 pointers.

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.