嵌入式软件开发面试题interview Questions for firmware engineer(1)

这篇文章详细探讨了进程与线程在资源分配、内存共享、切换开销及优缺点等方面,比较了多进程与多线程的选择,介绍了进程间与线程间通信方法,涵盖了上下文切换、内存溢出预防、变量作用域和生命周期、编译优化以及MMU基础知识。同时讨论了硬实时系统与软实时系统的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. Processes and Threads (Different System Resource Management Methods)
  2. Difference
    • Process: Basic unit of resource allocation, composed of one or more threads.
    • Thread: Basic unit scheduled by the dispatcher, representing a task.
    • Each process has its independent memory space, and a process can have multiple threads. Process switching has high overhead.
    • Multiple threads share memory, and thread switching has low overhead.
    • A process crash doesn’t affect other processes.
    • A thread crash affects the entire process it belongs to.
  3. Pros and Cons of Multiprocessing and Multithreading
    • Memory usage, data sharing, synchronization, CPU utilization, creation/destruction, and switching speed, reliability, programming and debugging comparisons.
  4. Choosing Between Processes and Threads
    • Choose threads for frequent creation/destruction.
    • Choose threads for tasks requiring significant computation and frequent switching.
    • Choose processes for safety and stability.
    • Choose threads for speed.
  5. Interprocess and Interthread Communication (Synchronization) Methods
    • Interprocess: Named pipes, unnamed pipes (parent-child processes), message queues, semaphores, shared memory, sockets.
  6. Process State Transitions
    • Ready, Blocked, Running.
  7. Parent Process and Child Process
    • After calling fork() in the parent process, a child process is created with identical code, data, and user stack.
    • Typically, the parent process waits for the child process to finish.
  8. Context Switching
    • Process context switching involves saving the values of all CPU registers, stack content, and execution position for later restoration.
    • Interrupt context involves saving the current process’s execution state when an interrupt occurs.
  9. C/C++
  10. new and malloc
    • malloc and free are C/C++ library functions, requiring the stdlib.h header.
    • new and delete are C++ keywords, no header needed, compiler support required.
    • new allocates memory without specifying size; malloc requires size.
    • Results: new returns a typed pointer; malloc returns a void pointer.
  11. Memory Allocation for 1GB (malloc 1.2GB)
    • Possible; malloc allocates virtual address space, not physical memory.
  12. extern “C” Purpose
    • Allows C++ code to call C code, ensuring the compiler compiles according to C language conventions.
  13. Functions Leading to Memory Overflow and Improvement
    • strcat and strcpy can lead to overflow.
    • Improvement: Use strncat, strncpy, or memcpy with specified limits.
  14. Static Usage
    • static for local variables keeps them in the static area; they persist beyond function calls.
    • static for global variables limits their scope to the current file.
    • static for functions restricts their visibility to the current file.
  15. const Usage
    • const makes data read-only.
  16. Volatile Purpose and Usage
    • Prevents compiler optimization, forces reading from memory instead of cached values.
    • Usage: Hardware registers, shared variables in multithreading, variables accessed by interrupt handlers.
  17. const Constants vs. #define Macros
    • #define max 1 replaces during preprocessing, no memory allocation, no type safety.
    • const int max = 1; determines value during compilation, allocates memory, type-safe.
  18. Variable Scope and Lifetime
    • Global variables: File scope, available externally, lasts until program termination.
    • Local variables: Function scope, created on entry, destroyed on exit.
  19. sizeof and strlen
    • sizeof() is an operator, returns the size in bytes during compile time.
    • strlen() is a function, returns the length of a string during runtime, requires <string.h> header.
int main()  
{
      
    char *p = "hello";  
    // p is a pointer to a string literal "hello"
    
    char arr1[] = "hello";  
    // arr1 is an array of characters initialized with the string "hello"
    
    char arr2[] = {
    'h', 'e', 'l', 'l', 'o' }; 
    // arr2 is an array of characters initialized individually
    
    printf("%d\n", sizeof(p));
    // Result is 4 on 32-bit systems and 8 on 64-bit systems
    // The size of a pointer variable depends on the system's architecture
    
    printf("%d\n", sizeof(arr1));
    // Result is 6 because it includes the null terminator '\0'
    
    printf("%d\n", sizeof(arr2));
    // Result is 5 because it's a character array, not a string, and doesn't include '\0'
    
    printf("%d\n", strlen(p));
    // Result is 5 because strlen calculates the length of the string, excluding the null terminator
    
    printf("%d\n", strlen(arr1));
    // Result is 5 because strlen calculates the length of the string, excluding the null terminator
    
    p
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值