void t1( int xx)
{
     int ii = 0; /*局部于函数*/
    char szBuf[1];
    FOREVER
    {
        if ( xx == 1)
        {
         ii= ii+3;
             printf("  i = %d \n", ii);
      }         
       else
       {
           ii= ii+5;
             printf("ii = %d \n", ii);
      }
       

         taskDelay(200);
    }
}

/*************************************************************************
*receive and show data from the port
*/
void t2( int xx)
{
    static int ii = 0;   /* ii 相当于2个 task用同一个变量,task为线程级的,会共用  */
   
    char szBuf[1];
    FOREVER
    {
        if ( xx == 1)
        {
         ii= ii+3;
             printf("  i = %d \n", ii);
      }         
       else
       {
           ii= ii+5;
             printf("ii = %d \n", ii);
      }
       

         taskDelay(200);
    }
}

STATUS CTask()
{
 int taskid=0;

 taskid = taskSpawn(
  "a1",
  100,
  0,
  20000,
  (FUNCPTR) t1,
  1,0,0,0,0,0,0,0,0,0);
 if(taskid==NULL)
 {
  printf("Something is wrong when create task\n");
  return ERROR;
 }
 
 taskid = taskSpawn(
  "a2",
  150,
  0,
  20000,
  (FUNCPTR) t1,
  2,0,0,0,0,0,0,0,0,0);
 if(taskid==NULL)
 {
  printf("Something is wrong when create task\n");
  return ERROR;
 } 
}

STATUS CTask2()
{
 int taskid=0;

 taskid = taskSpawn(
  "a1",
  100,
  0,
  20000,
  (FUNCPTR) t2,
  1,0,0,0,0,0,0,0,0,0);
 if(taskid==NULL)
 {
  printf("Something is wrong when create task\n");
  return ERROR;
 }
 
 taskid = taskSpawn(
  "a2",
  150,
  0,
  20000,
  (FUNCPTR) t2,
  2,0,0,0,0,0,0,0,0,0);
 if(taskid==NULL)
 {
  printf("Something is wrong when create task\n");
  return ERROR;
 } 
}


嵌入试面试常见题

interview

What is an lvalue and rvalue in C

Lvalue or location value is an address that is stored into while rvalue is the "read" value, ie., value of a constant or the value stored at a location. In the assignment

a = 31;

we have the lvalue of a used on the left hand side and the integer contant value 31 assigned to this location.

In the assignment

Embedded C interview Questions for Embedded Systems Engineers

Q: Which is the best way to write Loops?
Q: Is Count Down_to_Zero Loop better than Count_Up_Loops?
A: Always, count Down to Zero loops are better.
This is because,at loop termination, comparison to Zero can be optimized by complier. (Eg using SUBS)
Else, At the end of the loop there is ADD and CMP.

Q: What is loop unrolling?
A: Small loops can be unrolled for higher performance, with the disadvantage of increased
codesize. When a loop is unrolled, a loop counter needs to be updated less often and
fewer branches are executed. If the loop iterates only a few times, it can be fully unrolled,
so that the loop overhead completely disappears.
eg:
int countbit1(uint n)
{ int bits = 0;
while (n != 0)
{
if (n & 1) bits++;
n >>= 1;
}
return bits;
}

int countbit2(uint n)
{ int bits = 0;
while (n != 0)
{
if (n & 1) bits++;
if (n & 2) bits++;
if (n & 4) bits++;
if (n & 8) bits++;
n >>= 4;
}
return bits;
}

Q: How does, taking the address of local variable result in unoptimized code?
A: The most powerful optimization for compliler is register allocation. That is it operates the variable from register, than memory. Generally local variables are allocated in registers. However if we take the address of a local variable, compiler will not allocate the variable to register.

Q: How does global variable result in unoptimized code?
A: For the same reason as above, compiler will never put the global variable into register. So its bad.

Interrupt Handling, Interview Question for Embedded Systems Engineers

Q: How is ISR handled in vxworks ?
A: VxWorks supplies interrupt routines which connect to C functions and pass arguments to the functions to be executed at interrupt level.  To return from an interrupt, the connected function simply returns.  A routine connected to an interrupt in this way is referred to as an interrupt service routine (ISR) or interrupt handler.  When an interrupt occurs, the registers are saved, a stack for the arguments to be passed is set up, then the C function is called. On return from the ISR, stack and registers are restored.

Q: How do you connect the C Function to any interrupt?
A: Using intConnect(INUM_TO_IVEC(intNum), intHandler, argToHandler).
The first argument to this routine is the byte offset of the interrupt vector to connect to. The second argument is the interrupt handler and the last is any argument to this handler.

Q: Is the C Code directly vectored to Hardware interrupts using IntConnect? Explain?
A:No. Interrupts cannot actually vector directly to C functions. Instead, intConnect( ) builds a wrappper,a small amount of code, that saves the necessary registers, sets up a stack entry (either on a special interrupt stack, or on the current task's stack) with the argument to be passed, and calls the connected function. On return from the function it restores the registers and stack, and exits the interrupt;

Q: Does the ISR use the stack of interrupted Task? Explain?
A: It depends on system architecture.

Semaphore/Mutex Interview Question for Embedded Systems Engineers

Q: What are the different types of semaphores in vxworks? Which is the fastest?
A: VxWorks supports three types of semaphores. Binary, mutual exclusion, and counting semaphores. Binary is the fastest semaphore.

Q: When will you use binary semaphore ?
A: Binary semaphores are used for basic task synchronization and communication.

Q: When will you use mutual exclusion semaphore?
A: Mutual exclusion semaphores are sophisticated binary semaphores that are used to address the issues relating to task priority inversion and semaphore deletion in a multitasking environment.

Q: When will you use Counting semaphore?
A: Counting semaphores maintain a count of the number of times a resource is given. This is useful when an action is required for each event occurrence. For example if you have ten buffers, and multiple tasks can grab and release the buffers, then you want to limit the access to this buffer pool using a counting semaphore.

Q: What is the difference between Mutex and binary semaphore?
A: The differences are:
1) Mutex can be used only for mutual exclusion, while binary can be used of mutual exclusion as well as synchronisation.
2) Mutex can be given only by the task that took it.
3) Mutex cannot be given from an ISR.
4) Mutual-exclusion semaphores can be taken recursively. This means that the semaphore can be taken more than once by the task that holds it before finally being released.
5) Mutex provides a options for making the task that took it as DELETE_SAFE. This means, that the task cannot be deleted when it holds mutex.

VxWorks Task, Interview Questions

Q: What is a task in VxWorks?
A: A task is an independent program with its own thread of execution and execution context. VxWorks uses a single common address space for all tasks thus avoiding virtual-to-physical memory mapping. Every task contains a structure called the task control block that is responsible for managing the task's context.

Q: How do task's manage the  context of execution ?
A: Every task contains a structure called the task control block that is responsible for managing the task's context. A task's context includes

· program counter or thread of execution
· CPU registers
· Stack of dynamic variables and function calls
· Signal handlers
· IO assignments
· Kernel control structures etc.,

Q: What are the Different states of tasks?
A task has 4 states. Read, Pend, Delay, Suspended

Q: Explain the task State transition ?
A: A task can be created with taskInit() and then activated with taskActivate() routine or both these actions can be performed in a single step using taskSpawn(). Once a task is created it is set to the suspend state and suspended until it is activated, after which it is added to the ready queue to be picked up by the scheduler and run. A task may be suspended by either the debugging your task, or the occurrence an exception. The difference between the pend and suspend states is that a task pends when it is waiting for a resource. A task that is put to sleep is added to delay queue.

VxWorks Interview Question for Embedded Systems Engineers

Q: What is a RealTime System ?
A: A Real-time system is defined as a system where the response time for an event is predictable and deterministic with minimal latency.

Q: How is RTOS different from a general operating system?
A:
TaskScheduling:
RTOS generally have priority-based preemptive scheduling, which allows high-priority threads to meet their deadlines consistently. Whereas In a GPOS, the scheduler typically uses a "fairness" policy to dispatch threads and processes onto the CPU. Such a policy enables the high overall throughput required by desktop and server applications, but offers no assurances that high-priority, time-critical threads will execute in preference to lower-priority threads.

Preemetive Kernel:
In RTOS, kernel operations are preemptible. And so the RTOS kernel must be simple and elegant as possible.

Mechanisms to Avoid Priority Inversion:
This occurs when a lower-priority thread can inadvertently prevent a higher-priority thread from accessing the CPU. VxWorks specifically has protection for this.

Q: What is a task in VxWorks?

what is the difference between linked list and array?

Storage:
1) Arrays are statically allocated whereas linked list are dynamically allocated and linked.
Generally, if the number of allocations are known before hand, we use arrays. Otherwise linked list

Performance:
2) Array is a high performance way of storing a group of data because each element is laid out next to it's neighbor in memory. This allows for very fast access because (a) the code can do a little math and jump quickly to any location in the array, and (b) the elements are all grouped together so they tend to be in memory at the same time (fewer page faults and cache misses).

3) If you want to index by something like a string, you could use a hash table. Lets say you have a list of players, and you want to pull an object up from each player's name. You take the string, run a hash function to convert that into a number that falls within the range of an array, and store it in that element.

Or, you can have a linked list faster by breaking it into subelements. For example, if you were storing words in a dictionary, you could have an array of linked lists with 26 entries for a-z. That way when you manipulate a list, you are manipulating a much smaller subset.

Side note:

1) Java provides arraylist (java.util.ArrayList) option to programmers. So whether ArrayList better than array ?
If the data has a known number of elements or small fixed size upper bound, or where efficiency in using primitive types is important, arrays are often the best choice. However, many data storage problems are not that simple, and ArrayList (or one of the other Collections classes) might be the right choice.

2) How Arraylist are created ?

Embedded System Interview Questions. Part II

x86 interview questions

  1. What does microprocessor speed depend on?
  2. Is the address bus unidirectional?
  3. Is the data bus is Bi-directional?
  4. What is the difference between microprocessor and microcontroller?
  5. Why does microprocessor contain ROM chips?
  6. Difference between static and dynamic RAM?
  7. What is interrupt?
  8. What is cache memory?
  9. What is stack?
  10. Can ROM be used as stack?
  11. What is NV-RAM?

C Interview Questions

  1. What is the difference between malloc() and calloc() ?
  2. write C code for deleting an element from a linked listy traversing a linked list efficient way of elimiating duplicates from an array
  3. Can structures be passed to the functions by value?
  4. Why cannot arrays be passed by values to functions?
  5. Advantages and disadvantages of using macro and inline functions?
  6. What happens when recursion functions are declared inline?
  7. What is the Scope of static variables?
  8. What is the output of printf("\nab\bcd\ref");

Embedded Systems Interview Questions

In general the question would be of two type

1) Questions concentrating more on general skills as a software engineer
On C lang, algorithms, design skills, Tools used etc

2) Questions concentrating more on specific skills in real-time system design
On RTOS,multi-tasking issues (synchronization,exclusion, scheduling),device driver skills (interrupt handling, device addressing)

Typical questions (assuming VxWorks Experience):

1) What is an Interrupt Service Routine? What are the typical requirements for an ISR?
2) What are the advantages and disadvantages of cache memory in a real-time system that uses DMA?
3) What are the different types of timing requirements that real-time systems have?
4) What is priority inversion and how is it typically addressed?
5) How many levels of task priority are there in VxWorks? Explain the task priority settings in your system?
6) You finished writing a real-time system but find that the system performs much worse than expected. What should you do? (Answers shouldinclude: hardware verification [cache, SDRAM and device timing], compiler
settings, task priority settings, profiling, bad coding, etc.)

eg:
Soving issues in signaling to devices (ie being able to look at a logic analyser and find the problem with interdevice communication), Solving issues with data throught put(ie minimizing bus activity and processor useage to move data through a system), Service times for various threads/processes and activities ...

7) Discuss issues with dynamic memory allocation in real-time systems.
(If they don't mention the words "memory leak" or "thread-safe", they should
probably be excused.)

8) "What is the difference between the three types of semaphores in vxWorks'mutual-exclusion','binary', and 'counting'?"

Linked List

What is linked list? Refresh the basics

Stanford.edu (pdf)
wikipedia

Linked list problems:

18 problems, stanford.edu (pdf)

Common Questions on linked lists ?

1) What's the difference between a linked list and an array?
2) Implement a linked list. Why did you pick the method you did?
3) Implement an algorithm to sort a linked list. Why did you pick the method you did? Now do it in O(n) time.
4) Implement an algorithm to reverse a linked list. Now do it without recursion.
5) Find the middle of a linked list. Now do it while only going through the list once
6) Implement an algorithm to insert a node into a circular linked list without traversing it.
7) How would you print out the data in a binary tree, level by level, starting at the top?
8) Write code for reversing a linked list.
9 ) Reverse the words in a sentence, i.e. "My name is Chris" becomes "Chris is name My." Optimize for speed. Optimize for space.
10)Implement Insert and Delete for
    --singly-linked linked list
    --sorted linked list
    --circular linked list