Click on the above “Baibai Technology” and select “Pin to Public Account”
Embedded essentials delivered instantly
Question 1: Briefly describe the differences between memcpy and strcpy?
Question 2: What are the differences between semaphores and mutexes?
Question 3: Briefly describe the process of program compilation?
Answer to Question 1:
(1) The content copied is different. strcpy can only copy strings, while memcpy can copy any content, such as character arrays, integers, structures, classes, etc.
(2) The methods of copying are different. strcpy does not require specifying the length; it ends when it encounters the end character of the copied string “\0”, making it prone to overflow. memcpy, on the other hand, determines the length of copying based on its third parameter.
(3) The purposes are different. strcpy is typically used for copying strings, while memcpy is generally used for copying other types of data.
Answer to Question 2:
(1) Mutexes are used for thread mutual exclusion, while semaphores are used for thread synchronization.
Mutual exclusion means that a resource can only be accessed by one visitor at a time, having uniqueness and exclusivity. However, mutual exclusion does not limit the order of access to the resource, meaning access is unordered.
Synchronization means that, based on mutual exclusion (in most cases), it achieves ordered access to resources by visitors through other mechanisms. In most cases, synchronization has already achieved mutual exclusion, especially for all write operations to the resource, which must be mutually exclusive. A few cases allow multiple visitors to access the resource simultaneously.
(2) The value of a mutex can only be 0 or 1, while the value of a semaphore can be a non-negative integer.
This means that a mutex can only be used for mutual access to one resource; it cannot solve the multi-threaded mutual exclusion problem for multiple resources. A semaphore can achieve multi-threaded mutual exclusion and synchronization for multiple similar resources. When the semaphore is a single-value semaphore, it can also achieve mutual access to one resource.
(3) The locking and unlocking of a mutex must be used correspondingly by the same thread, while a semaphore can be released by one thread and acquired by another.
Answer to Question 3:
Preprocessing: Preprocessing commands start with the symbol “#”. Preprocessing is equivalent to assembling a new C program based on preprocessing commands, but it is often with the extension .i. The preprocessing of the C language mainly has three aspects:
1. Macro definitions.
2. File inclusion.
3. Conditional compilation.
Compilation: Converts the preprocessed .i file into an .s assembly file.
At this stage, the compiler mainly performs lexical analysis, syntax analysis, semantic analysis, etc. After checking for errors, it translates the code into assembly language.
Assembly: Translates the assembly file into machine language and packages it into an relocatable object file (.o). This file is a binary file, and the byte encoding is machine instructions.
Linking: Merges the referenced other .o files into the .o file of our program, resulting in the final executable file.