Tuesday, March 15, 2011

#C_CODE: palindrome code in c using pointers.



/* Details: Palindrome code using pointer
* Author : Vinod Sasidharan
* Date : Wednesday, March 16, 2011, 10.14 am
* Remarks: Multiple words possible in input string due
* to use of fgets.
*/


/* Include files */
#include <stdio.h>
#include <string.h>

/* Function prototype declaration */
unsigned char palindrome(char *s);

/* Function name: Palindrome
* Input : Pointer to a string
* Output : 8bit unsigned - 0/1
*/
unsigned char palindrome(char *s)
{
char *s2 = s + strlen(s) - 1;

/* Sanity check */
if(!*s) return 0;

while(*(s2--) == *(s++) && *s);
return((unsigned char)(!*s && *(++s2) == *(--s)));
}

/* Main function */
int main()
{
char buff[35];

fputs("Input string: ",stdout);
fgets(buff,sizeof(buff),stdin);
fflush(stdin);

/* To avoid the \n in the input string */
*(buff + strlen(buff) - 1) = '\0';

printf("Is the string %s palindrome: %s\n",buff,
palindrome(buff)?"YES":"NO");
getchar();
return 0;
}

Tested on: Visual C++ 2008 Express Edition, Windows XP

Monday, March 14, 2011

How is size of int determined?

int data type's size is usually chosen as natural size of the ALU.
Width of ALU = Size of operand.
Popular ALU's are 16 bits or 32 bits.
int is a non-portable data type.
To develop portable programs, two suffixes are used viz. short and long.

NOTE: Size of every type of pointer is the size of int

Wednesday, March 9, 2011

C Code:Estimate Mean,Variance & Standard Deviation




#include <stdio.h>
#include <math.h >

/* Compute a running average and variance of a set of input values. */
void AvgVar(double x, double *mean, double *var)
{
    static int n = 0;
    static double sum = 0,sumSq = 0;
    sum += x;
    sumSq += (x*x);
    n++;
    *mean = sum/n;
    *var = (sumSq/n) - (*mean * *mean);
}
int main()
{
    double arr[10] = {4,2,5,3,2,4,2,6,3,4,};
    double mean,var;
    int i;
    for(i=0; i<10; i++)
        AvgVar(arr[i],&mean,&var);
    printf("Mean = %f, Variance = %f\n",mean,var);
    printf("Standard Deviation = %f\n",sqrt(var));
    getchar();
    return 0;
}

Ref: eetimes.com - Jack Crenshaw

Tuesday, March 8, 2011

Priority inversion

In priority based scheduling, a high priority task may be waiting for a resource held by a low priority task, which in turn is not running.
The priority of this task effectively gets lowered to that of the task holding the resource.
Until the low priority task gets the CPU, the high priority task cannot run.

Disadvantages of having many tasks in an RTOS

1. More likely to have data sharing between tasks: increased likelihood of shared-data
problems.
2. More need fo semaphores to protect shared data: increased overhead and possible semaphore bugs.
3. More need for messages, queues, pipes: increased overhead and likelihood of bugs.
4. More memory needed for stacks,message buffers.
5. More CPU time spent switching tasks
6. More calls to RTOS,increasing system overhead.

Recommendation:
“other things being equal, use as few tasks as you can get away with; add more tasks to your design only for clear reasons.”

What makes an OS an RTOS?

1. Multi threaded and preemptive (i.e. the kernel itself is preemptive)
2. Thread priorities (No deadline driven systems exists)
3. Predictable thread synchronization mechanisms
4. Priority inheritance
5. Known OS behavior

Code Optimization Techniques

1. Inline functions - Eliminates the runtime overhead associated with the function call. (For functins with few lines of code)
2. Table lookups - A table of pointers to functions
3. Hand-coded assembly - Compiler not aware of special instructions
4. Register variables - Place variable in general-purpose register rather than on stack
5. Fixed-point arithmetic - Avoid using a floating point library which contains software subroutines which will emulate floating-point instructions.
6. Variable size -Use processor's native register width for variables whenever possible.
7. Loop unrolling - Unrolling duplicates the body of the loop multiple times.