Friday, 4 December 2015

Embedded C Programs for Interview

Embedded C Programs for Interview

1. Implement below using C program.



Given int *data= [x1,x2,x3,x4….xn];
write a function such that
data= [x1,x1*x2,x1*x2*x3,....]


#include<stdio.h>
int main()
{
int data[]={1,2,3,4,5,6,7,8,9};
int i,j=2,mul[9];
mul[0]=1;
mul[1]=2;
int size=9;
printf("\n Given array is\n");
for (i=0;i<size;i++)
printf("%d\n",data[i]);
for (i=1;i<size;i++)
{
mul[i]=data[i]*mul[i-1];
j++;
}
printf("\n Mult array\n");
for (j=0;j<size;j++)
printf("%d\n",mul[j]);
}
1,2,3,4,5,6,7,8,9
1.2,6,24,120,740


2. Using C program Implement realloc using malloc - Dynamic memory allocation.



char *my_realloc(Char *ptr, int size)
{
           if(ptr)
           free(ptr);
           char *new_ptr = malloc(ptr,size);
return new_ptr;


}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define malloc(_x) mymalloc((_x))
#define free(_x) myfree((_x))
void myfree(void * p) {
   size_t * in = p;
   if (in) { --in; free(in); }
}
void * mymalloc(size_t n) {
   size_t * result = malloc(n + sizeof(size_t));
   if (result) { *result = n; ++result; memset(result,0,n); }
   return result;
}
size_t getsize(void * p) {
   size_t * in = p;
   if (in) { --in; return *in; }
   return -1;
}


void *reallocation(void *ptr,size_t size) {
   void *newptr;
   int msize;
   msize=getsize(ptr);
   printf("msize=%d\n",msize);
   if (size <= msize) return ptr;
   newptr=malloc(size);
   memcpy(newptr,ptr,msize);
   free(ptr);
   return newptr;
}
int main() {
   char * aa = malloc(50);
   char * bb ;
   printf("aa size is %d\n",getsize(aa));
   strcpy(aa,"my cookie");
   bb = reallocation(aa,100);
   printf("bb size is %d\n",getsize(bb));
   printf("<%s>\n",bb);
   free(bb);
}


3.Write a program to initialize array using malloc, to check nth count of array.

void main()
{
int *a;
printf("enter the number of variables\n");
scanf("%d",&o);
printf("Variable is %d\n",o);
a=(int*)malloc(o*sizeof(int));
printf("Enter Elements of First List \n");
for (i = 0; i < o; i++) {
    scanf("%d", a + i);
}
int s=sizeof(a)/sizeof(int);
printf("\n%d\t%u\n",s,&s);
for(m=0;m<=3;m++)
printf("%d\n",a[m]);
}

4. Write a Program to print sizeof data types- sizeof(); operator.



#include <stdio.h>
int main()
{
printf("Size of data type for Char-%d!\n",sizeof(char));
printf("Size of data type for Short-%d!\n",sizeof(short));
printf("Size of data type for Int-%d!\n",sizeof(int));
printf("Size of data type for Long-%d!\n",sizeof(long));
printf("Size of data type for Double-%d!\n",sizeof(double));
return 0;
}


5.Write a program to reverse the given number.



#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,cnt=0,o,count=0,l=0,cnt1=0,j=0,k=0,m;
int *a;
char b[5];
printf("enter the number of variables\n");
scanf("%d",&o);
printf("Variable is %d\n",o);
a=(int*)malloc(o*sizeof(int));
printf("Enter Elements of First List \n");
for (i = 0; i < o; i++) {
    scanf("%d", a + i);
}
printf("Reversed String is\n");
while(o!=0)
{
printf("%d\n",a[o-1]);
o--;
}
}


6.Write syntax to initialize the pointer to given memory location.



#include <stdio.h>
int main()
{
   int a=9;
   int *p;
   *p=0x012;
    printf("%d\n%u\n%u\n",*p,p,&p);
   *p=a;
   printf("\n%d,%u\n",*p,&p);
   return 0;
}


7.Write a program to Reverse the string.


#include <stdio.h>


int main()
{
   int k=0,j,i=0,count=0;
   char a[]="abhishek",b[10];
   while(a[i]!='\0')
   {
       count++;
       i++;
    }
   for(j=count;j>=0;j--)
   {
       b[k++]=a[j];
   }
   for(i=0;i<=count;i++){
   printf("%c",b[i]);
  }
   printf("\n");
   return 0;
          }

Wednesday, 21 October 2015

Embedded C Programs for Interview - String

String Programs

Malloc to initialize array, to check nth count of array!!

int *a;
printf("enter the number of variables\n");
scanf("%d",&o);
printf("Variable is %d\n",o);
a=(int*)malloc(o*sizeof(int));
printf("Enter Elements of First List \n");
    for (i = 0; i < o; i++)    {
        scanf("%d", a + i);
    }
int s=sizeof(a)/sizeof(int);
printf("\n%d\t%u\n",s,&s);
for(m=0;m<=3;m++)
printf("%d\n",a[m]);



Program for sizeof data types


#include <stdio.h>
int main()
{
    printf("Size of data type for Char-%d!\n",sizeof(char));
    printf("Size of data type for Short-%d!\n",sizeof(short));
    printf("Size of data type for Int-%d!\n",sizeof(int));
    printf("Size of data type for Long-%d!\n",sizeof(long));
    printf("Size of data type for Double-%d!\n",sizeof(double));
    return 0;
}

Integer reverse program




#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,cnt=0,o,count=0,l=0,cnt1=0,j=0,k=0,m;
int *a;
char b[5];
printf("enter the number of variables\n");
scanf("%d",&o);
printf("Variable is %d\n",o);
a=(int*)malloc(o*sizeof(int));
printf("Enter Elements of First List \n");
    for (i = 0; i < o; i++)    {
        scanf("%d", a + i);
    }
printf("Reversed String is\n");
while(o!=0)
{
printf("%d\n",a[o-1]);
o--;
}
}

Pointer Initialization


#include <stdio.h>

int main()
{
    int a=9;
    int *p;
    *p=0x012;
     printf("%d\n%u\n%u\n",*p,p,&p);
    *p=a;
    printf("\n%d,%u\n",*p,&p);

    return 0;
}

String Reverse Program

Friday, 24 April 2015

bind: Address already in use , server_thread(): bind(8080) failed

bind: Address already in use

Beaglebone black , was getting this error because of my port 8080 was used for other service, I solved this the answer was just to get ride of the running service on that port else to switch to other port.


root@beaglebone:~# cd mjpg-streamer
root@beaglebone:~/mjpg-streamer# ./mjpg_streamer -i "./input_uvc.so" -o "./output_http.so -w ./www"
MJPG Streamer Version: svn rev: exported
 i: Using V4L2 device.: /dev/video0
 i: Desired Resolution: 640 x 480
 i: Frames Per Second.: -1
 i: Format............: JPEG
 i: TV-Norm...........: DEFAULT
UVCIOC_CTRL_ADD - Error at Pan (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Tilt (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Pan Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Pan/tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_ADD - Error at Focus (absolute): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Tilt (relative): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Pan/tilt Reset: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Focus (absolute): Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at LED1 Mode: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at LED1 Frequency: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Disable video processing: Inappropriate ioctl for device (25)
UVCIOC_CTRL_MAP - Error at Raw bits per pixel: Inappropriate ioctl for device (25)
 o: www-folder-path...: ./www/
 o: HTTP TCP port.....: 8080
 o: username:password.: disabled
 o: commands..........: enabled
bind: Address already in use
bind: Address already in use
 o: server_thread(): bind(8080) failed



Step 1 :


 netstat -p
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 192.168.40.25:59952     ssl.discourse.org:https ESTABLISHED 2922/chromium-brows
tcp        0      0 192.168.40.25:47608     nrt13s18-in-f24.1:https ESTABLISHED 2922/chromium-brows


Step 2 :
fuser 8080/tcp 

Step 3 :
fuser -k 8080/tcp 





Thursday, 9 October 2014

Memory management unit(MMU)

Memory Management

This is quite possibly the most important part of any Kernel. And rightfully so--all programs and data require it. As you know, in the Kernel, because we are still in Supervisor Mode (Ring 0), We have direct access to every byte in memory. This is very powerful, but also produces problems, espically in a multitasking evirement, where multiple programs and data require memory.
One of the primary problems we have to solve is: What do we do when we run out of memory?
Another problem is fragmentationIt is not always possible to load a file or program into a sequencal area of memory. For an example, lets say we have 2 programs loaded. One at 0x0, the other at 0x900. Both of these programs requested to load files, so we load the data files:





Notice what is happening here. There is alot of unused memory between all of these programs and files. Okay...What happens if we add a bigger file that is unable to fit in the above? This is when big problems arise with the current scheme. We cannot directly manipulate memory in any specific way, as it will currupt the currently executing programs and loaded files.
Then there is the problems of where each program is loaded at. Each program will be required to be Position Indipendent or provide relocation Tables. Without this, we will not know what base address the program is supposed to be loaded at.
Lets look at these deeper. Remember the ORG directive? This directive sets the location where your program is expected to load from. By loading the program at a different location, the program will refrence incorrect addresses, and will crash. We can easily test this theory. Right now, Stage2 expects to be loaded at 0x500. However, if we load it at 0x400 within Stage1 (While keeping the ORG 0x500 within Stage2), a triple fault will accure.
This adds on two new problems. How do we know where to load a program at? Coinsidering all we have is a binary image, we cannot know. However, if we make it standard that all programs begin at the same address--lets say, 0x0, then we can know. This would work--but is impossible to impliment if we plan to support multitasking. However, if we give each program there own memory space, that virtually begins at 0x0, this will work. After all, from each programs' perspective, they are all loaded at the same base address--even if they are different in the real (physical) memory.
What we need is some way to abstract the physical memory. Lets look closer.




Virtual Address Space (VAS)

Virtual Address Space is a Program's Address Space. One needs to take note that this does not have to do with System Memory. The idea is so that each program has their own independent address space. This insures one program cannot access another program, because they are using a different address space.
Because VAS is Virtual and not directly used with the physical memory, it allows the use of other sources, such as disk drives, as if it was memory. That is, It allows us to use more memory then what is physically installed in the system.
This fixes the "Not enough memory" problem.
Also, as each program uses its own VAS, we can have each program always begin at base 0x0000:0000. This solves the relocation problems discussed ealier, as well as memory fragmentation--as we no longer need to worry about allocating continous physical blocks of memory for each program.
Virtual Addresses are mapped by the Kernel trough the MMU. More on this a little later.

Virtual Memory: Abstract

Virtual Memory is a special Memory Addressing Scheme implimented by both the hardware and software. It allows non contigous memory to act as if it was contigius memory.
Virtual Memory is based off the Virtual Address Space concepts. It provides every program its own Virtual Address Space, allowing memory protection, and decreasing memory fragmentation.
Virtual Memory also provides a way to indirectly use more memory then we actually have within the system. One common way of approching this is by using Page files, stored on a hard drive.
Virtual Memory needs to be mapped through a hardware device controller in order to work, as it is handled at the hardware level. This is normally done through the MMU, which we will look at later.
For an example of seeing virtual memory in use, lets look at it in action:
Notice what is going on here. Each memory block within the Virtual Addresses are linear. Each Memory Block is mapped to either it's location within the real physical RAM, or another device, such as a hard disk. The blocks are swapped between these devices as an as needed bases. This might seem slow, but it is very fast thanks to the MMU.
Remember: Each program will have its own Virtual Address Space--shown above. Because each address space is linear, and begins from 0x0000:00000, this immiedately fixes alot of the problems relating to memory fragmentation and program relocation issues.
Also, because Virtual Memory uses different devices in using memory blocks, it can easily manage more then the amount of memory within the system. i.e., If there is no more system memory, we can allocate blocks on the hard drive instead. If we run out of memory, we can either increase this page file on an as needed bases, or display a warning/error message,
Each memory "Block" is known as a Page, which is useually 4096 bytes in size.
Once again, we will cover everything in much detail later.

Memory Management Unit (MMU): Abstract


The MMU, Also known as Paged Memory Management Unit (PMMU) is a component inside the microprocessor responsible for the management of the memory requested by the CPU. It has a number of responsibilities, includingTranslating Virtual Addresses to Physical Addresses, Memory Protection, Cache Control, and more.

Segmentation: Abstract

Segmentation is a method of Memory Protection. In Segmentation, we only allocate a certain address space from the currently running program. This is done through the hardware registers.
Segmentation is one of the most widly used memory protection scheme. On the x86, it is useually handled by the segment registers: CS, SS, DS, and ES.
We have seen the use of this through Real Mode.

Paging: Abstract

THIS will be important to us. Paging is the process of managing program access to the virtual memory pages that are not in RAM. We will cover this alot more later.