Embedded C,RTOS interview questions with answers

Recommended Embedded RTOS books to buy:
1) What is RTOS and why do we go for it?
       RTOS sheludes execution in a timely manner,manages system resources and provides a consistent foundation for developing application code.
     Application code designed for RTOS could be ranging from simple digital stopwatch to complex aircraft navigation systems.
RTOS differs from the GPOS by
  Task scheduling : RTOS like vxWorks,neclueos, uC/OSII uses a strict scheduling algorithms (like pre-emptive scheduling) that makes the tasks meet their deadline to get the job done.
  The sheduler decides which task run when by knowing its priorities.Whereas In a GPOS, the scheduler typically uses a "fairness" policy that offers no assurances that high-priority, time-critical tasks will execute in preference to lower-priority tasks.This policy is maintained to obtain the overall output by the desktop pc or server pc.

2) What are the different types of semaphores in vxworks RTOS? Which is the fastest?
       VxWorks supports three types of semaphores.
 1) Binary 2) mutual exclusion, and 3)counting semaphores.
 Fastest : Binary semaphore.

         3. What is priority inversion ? why priority inversion occurs?
     Tasks in RTOS's communucates between them via IPC source semphores for synchornization.
     The situation priority inversion occurs when a higher priority taks is ready to run but it is waiting for a shared resource that is to be released by a lower priority task currently running .Now higher-priority task is pending on the resource and it has to wait until the lower priority task to finish.
4. What are the solutions for priority inversion ?

    1) Priority inheritance
    2) Priority ceiling

5. What is super loop ?

      Super loop is the infinite loop that runs all the time because , most of the embedded systems has no OS in it to return to application.
//Super loop example
 //all other code




//This is is tricky assembly version
goto label:

6) What is the difference between Structure and union? Where do we use union?

        When a structure is defined the compiler allocates memory for all of its member variables with necessary alignment .Whereas for unions the compiler allocates memory for the highest the union is equal to the biggest size of member from the union member variable list.union can only store information in one field at any one time.
#include <stdio.h>
struct mStruct
    char name[10];
    int age;
    float height;

union mUnion
    char name[15];
    int age;
    float height;

int main(void)
    union  mUnion  uTest;
    struct mStruct sTest;
    sTest.age    = 20;
    sTest.height = 6.1;
    strcpy(uTest.name ,"uTest");
    uTest.height = 6.0;
    uTest.age    = 20;
    printf("sizeof(uTest) = %d ,sizeof(sTest) = %d \n",sizeof(uTest),sizeof(sTest));
    printf("uTest.age = %d , uTest.height = %f , uTest.name = %s\n",uTest.age, uTest.height, uTest.name);
    printf("sTest.age = %d , sTest.height = %f , sTest.name = %s\n",sTest.age, sTest.height, sTest.name);
7) Convert number 2 --> 37 without using any other operator but bitwise ? 
    int x = 2;
    printf("X before :%d\n",x);
    x = (x<<x<<x) | (x<<x<<x) |  x<<!!x | !!x ;
    printf("X After  :%d\n",x); 


8) Set , Get , Clear ,Toggle , Display Bits ?
// Display Every bits in a int number 
void displayBits(int data)
    int dataSize = 1<<sizeof(data);
    int count = 0;
    for (count = dataSize;count >= 0; count--)
// test a bit if it is zero or one 
int8_t testBit(int8_t value,int whichBit)
    int mask = 1 << whichBit;
    if (value&mask)
        return TRUE;
    else return FALSE;

// Set a bit to one 
int8_t setBit(int8_t result, int whichBit)
    return (result |= (1<<whichBit));

// toggle a bit  
int8_t toggleBit(int8_t result, int whichBit)
    return (result ^= (1<<whichBit));

/* Clear a bit to zero */
int8_t clearBit(int8_t result, int whichBit)
    return (result &=~ (1<<whichBit));

Litte Endian , Big Endian Formats in C

    Endianness is byte ordering used to store the data in memory ie which byte has to be stored at the lower order address or the higher address.Here is a simple code given to determine the platform is which endian type.
#include <stdio.h>
#include <stdlib.h>
#define STR_LIM (5)
#define STR_MAX_SIZE (256)

enum endian
char *int2char(int value);
int findEndian(int data);

int main()
    char string[STR_LIM] = "TEST";
    char testStr[5];
    int  number = 2048;
    printf(" Address : Value Representation\n");
    printf("%d:%c , %d:%c, %d:%c, %d:%c \n",&string[0],string[0],&string[1],string[1],&string[2],string[2],&string[3],string[3]);
    printf("%d:%c , %d:%c, %d:%c, %d:%c \n",&testStr[0],testStr[0],&testStr[1],testStr[1],&testStr[2],testStr[2],&testStr[3],testStr[3]);
    printf("LITTLE Endian Platform found\n");
    else printf("BIG Endian Platform found\n");

/* Integer to char conversion */
char *int2char(int value)
    static char string[STR_MAX_SIZE];
    return string;

/* Check Which Endian the Platform is */
int findEndian(int data)
    char *charData;
    int ret = eLITTLE_ENDIAN;
    charData = (char *) &data;
    if(charData[0] == eBIG_ENDIAN)
    ret = eBIG_ENDIAN;
    return ret;