EmbLogic's Blog

Difference between physical addressing and virtual addressing concept

up vote 8 down vote accepted

Physical addressing means that your program actually knows the real layout of RAM. When you access a variable at address 0x8746b3, that’s where it’s really stored in the physical RAM chips.

With virtual addressing, all application memory accesses go to a page table, which then maps from the virtual to the physical address. So every application has its own “private” address space, and no program can read or write to another program’s memory. This is called segmentation.

Virtual addressing has many benefits. It protects programs from crashing each other through poor pointer manipulation, etc. Because each program has its own distinct virtual memory set, no program can read another’s data – this is both a safety and a security plus. Virtual memory also enables paging, where a program’s physical RAM may be stored on a disk (or, now, slower flash) when not in use, then called back when an application attempts to access the page. Also, since only one program may be resident at a particular physical page, in a physical paging system, either a) all programs must be compiled to load at different memory addresses or b) every program must use Position-Independent Code, or c) some sets of programs cannot run simultaneously.

The physical-virtual mapping may be done in software (with hardware support for memory traps) or in pure hardware. Sometimes even the page tables themselves are on a special set of hardware memory. I don’t know off the top of my head which embedded system does what, but every desktop has a hardware TLB (Translation Lookaside Buffer, basically a cache for the virtual-physical mappings) and some now have advanced Memory Mapping Units that help with virtual machines and the like.

The only downsides of virtual memory are added complexity in the hardware implementation and slower performance.

Posted in Uncategorized | Leave a comment

USE OF SOCKETS

SIMPLE USES OF SOCKET PROGRAMMING IN EMBEDDED SYSTEMS LINUX WITH ‘C’

Typically two processes communicate with each other on a single system through one of the following inter process communication techniques.

  • Pipes
  • Message queues
  • Shared memory

There are several other methods. But the above are some of the very classic ways of interprocess communication.

But have you ever given a thought over how two processes communicate across a network?

For example, when you browse a website, on your local system the process running is your web browser, while on the remote system the process running is the web server. So this is also an inter process communication but the technique through which they communicate with each other is SOCKETS, which is the focus of this article.

What is a SOCKET?

In layman’s term, a Socket is an end point of communication between two systems on a network. To be a bit precise, a socket is a combination of IP address and port on one system. So on each system a socket exists for a process interacting with the socket on other system over the network. A combination of local socket and the socket at the remote system is also known a ‘Four tuple’ or ’4-tuple’. Each connection between two processes running at different systems can be uniquely identified through their 4-tuple.

There are two types of network communication models:

  1. OSI
  2. TCP/IP

While OSI is more of a theoretical model, the TCP/IP networking model is the most popular and widely used.

As explained in our TCP/IP Fundamentals article, the communication over the network in TCP/IP model takes place in form of a client server architecture. ie, the client begins the communication and server follows up and a connection is established.

Sockets can be used in many languages like Java, C++ etc but here in this article, we will understand the socket communication in its purest form (i.e in C programming language)

Lets create a server that continuously runs and sends the date and time as soon as a client connects to it.

Socket Server Example

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h> 

int main(int argc, char *argv[])
{
    int listenfd = 0, connfd = 0;
    struct sockaddr_in serv_addr; 

    char sendBuff[1025];
    time_t ticks; 

    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    memset(&serv_addr, '0', sizeof(serv_addr));
    memset(sendBuff, '0', sizeof(sendBuff)); 

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(5000); 

    bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 

    listen(listenfd, 10); 

    while(1)
    {
        connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); 

        ticks = time(NULL);
        snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
        write(connfd, sendBuff, strlen(sendBuff)); 

        close(connfd);
        sleep(1);
     }
}

In the above program, we have created a server. In the code :

  • The call to the function ‘socket()’ creates an UN-named socket inside the kernel and returns an integer known as socket descriptor.
  • This function takes domain/family as its first argument. For Internet family of IPv4 addresses we use AF_INET.
  • The second argument ‘SOCK_STREAM’ specifies that the transport layer protocol that we want should be reliable ie it should have acknowledgement techniques. For example : TCP
  • The third argument is generally left zero to let the kernel decide the default protocol to use for this connection. For connection oriented reliable connections, the default protocol used is TCP.
  • The call to the function ‘bind()’ assigns the details specified in the structure ‘serv_addr’ to the socket created in the step above. The details include, the family/domain, the interface to listen on(in case the system has multiple interfaces to network) and the port on which the server will wait for the client requests to come.
  • The call to the function ‘listen()’ with second argument as ’10? specifies maximum number of client connections that server will queue for this listening socket.
  • After the call to listen(), this socket becomes a fully functional listening socket.
  • In the call to accept(), the server is put to sleep and when for an incoming client request, the three way TCP handshake* is complete, the function accept () wakes up and returns the socket descriptor representing the client socket.
  • The call to accept() is run in an infinite loop so that the server is always running and the delay or sleep of 1 sec ensures that this server does not eat up all of your CPU processing.
  • As soon as server gets a request from client, it prepares the date and time and writes onthe client socket through the descriptor returned by accept().Three way handshake is the procedure that is followed to establish a TCP connection between two remote hosts. We might soon be posting an article on the theoretical aspect of the TCP protocol.Finally, we compile the code and run the server.

    Socket Client Example

    #include <sys/socket.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <arpa/inet.h> 
    
    int main(int argc, char *argv[])
    {
        int sockfd = 0, n = 0;
        char recvBuff[1024];
        struct sockaddr_in serv_addr; 
    
        if(argc != 2)
        {
            printf("\n Usage: %s <ip of server> \n",argv[0]);
            return 1;
        } 
    
        memset(recvBuff, '0',sizeof(recvBuff));
        if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        {
            printf("\n Error : Could not create socket \n");
            return 1;
        } 
    
        memset(&serv_addr, '0', sizeof(serv_addr)); 
    
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_port = htons(5000); 
    
        if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
        {
            printf("\n inet_pton error occured\n");
            return 1;
        } 
    
        if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
        {
           printf("\n Error : Connect Failed \n");
           return 1;
        } 
    
        while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
        {
            recvBuff[n] = 0;
            if(fputs(recvBuff, stdout) == EOF)
            {
                printf("\n Error : Fputs error\n");
            }
        } 
    
        if(n < 0)
        {
            printf("\n Read error \n");
        } 
    
        return 0;
    }

    In the above program, we create a client which will connect to the server and receive date and time from it. In the above piece of code :

    • We see that here also, a socket is created through call to socket() function.
    • Information like IP address of the remote host and its port is bundled up in a structure and a call to function connect() is made which tries to connect this socket with the socket (IP address and port) of the remote host.
    • Note that here we have not bind our client socket on a particular port as client generally use port assigned by kernel as client can have its socket associated with any port but In case of server it has to be a well known socket, so known servers bind to a specific port like HTTP server runs on port 80 etc while there is no such restrictions on clients.
    • Once the sockets are connected, the server sends the data (date+time) on clients socket through clients socket descriptor and client can read it through normal read call on the its socket descriptor.

    Now execute the client as shown below.

    $ ./newsc 127.0.0.1
    Sun Dec  18 22:22:14 2011

    We can see that we successfully got the date and time from server. We need to send the IP address of the server as an argument for this example to run. If you are running both server and client example on the same machine for testing purpose, use the loop back ip address as shown above.

    To conclude, In this article we studied the basics of socket programming through a live example that demonstrated communication between a client and server processes capable of running on two different machines.

Posted in Uncategorized | Leave a comment

Threads implementation with clients

RCS file: ipc_thrdsrvr.c,v
Working file: ipc_thrdsrvr.c
head: 1.2
branch:
locks: strict
access list:
symbolic names:
keyword substitution: kv
total revisions: 2;    selected revisions: 2
description:
—————————-
revision 1.2
date: 2014/03/31 11:58:38;  author: root;  state: Exp;  lines: +10 -10
implemented threads on two clients
—————————-
revision 1.1
date: 2014/03/31 11:53:46;  author: root;  state: Exp;
Initial revision
=============================================================================

Posted in Project 04: FTP based Client Server using Threads and Sockets, Uncategorized | Leave a comment

Threads are used in performing IPC .And ipc technique is message ques

RCS file: server_thread.c,v
3 Working file: server_thread.c
4 head: 1.1
5 branch:
6 locks: strict
7         root: 1.1
8 access list:
9 symbolic names:
10 keyword substitution: kv
11 total revisions: 1;     selected revisions: 1
12 description:
13 this is server in which ipc is done
14 and threads are implemented in them
15 —————————-
16 revision 1.1    locked by: root;
17 date: 2014/03/31 10:18:07;  author: root;  state: Exp;
18 Initial revision
19 =============================================================================

Posted in Uncategorized | Leave a comment

pointer to function

whats the diffrenece between these two??
int *function (int)
int (*function) (int)

Posted in Uncategorized | Leave a comment

stack push and pop program using functions .

//header.h//

#include
2 #include
3 #define MAX 5
4 int push(void **,int *);
5 int pop(void **,int *);
6 void display(void **,int *);
//fun.c//

#include”abc.h”
2 int push(void **astack,int *atop)
3 {
4 static char ch=1;
5 if(*atop >= MAX-1)
6 {
7 printf(“stack overflow”);
8 return -1;
9 }
10 if(*atop==-1)
11 {
12 *astack;
13 astack=(char*)malloc(1);
14 (*atop)++;
15 *(char*)(*astack+*atop)=ch;
16 ch++;
17 return 0;
18 }
19 (*atop)++;
20 *astack=(char*)realloc(*astack,(*atop) + 1);
21 *(char*)(*astack+*atop)=ch;
22 ch++;
23 return 0;
24
25 }
26 int pop(void **astack,int *atop)
27 {
28 if(*atop= 0; i–)
42 {
43 printf(“the data at %d pos is =%d\n”,i,*(char*)(*astack+i));
44 }
45 }
//main.c//
#include”abc.h”
2 int main()
3 {
4 int ch;
5 void *stack;
6 int top= -1;
7 do
8 {
9 printf(“1.push\n”);
10 printf(“2.pop\n”);
11 printf(“3.display\n”);
12 printf(“4.quit\n”);
13
14 scanf(“%d”,&ch);
15
16 switch(ch)
17 {
18 case 1: push(&stack,&top);
19 break;
20 case 2: pop(&stack,&top);
21 break;
22 case 3: display(&stack,&top);
23 break;
24 default : printf(“wrong choice\n”);
25 }
26 }
27 while(ch != 4);
28 return 0;
29 }
~

Posted in Uncategorized | Leave a comment

Multiple Data Compression and Encryption using Iterative technique

RCS file: mdc.c,v
Working file: mdc.c
head: 1.6
branch:
locks: strict
emblogic: 1.6
access list:
symbolic names:
keyword substitution: kv
total revisions: 6;    selected revisions: 6
description:
successfully created master array
finded no of distinct charcters  also using functions
currently 2 functions open master_array used
—————————-
revision 1.6    locked by: emblogic;
date: 2014/02/24 15:03:46;  author: emblogic;  state: Exp;  lines: +34 -30
marray which waaas done global was made loacal ,,
but ndc has to been made global
—————————-
revision 1.5
date: 2014/02/21 12:29:19;  author: emblogic;  state: Exp;  lines: +52 -13
eureka!!!!!successfully done 4bit compression
compressed file creted
—————————-
revision 1.4
date: 2014/02/17 07:01:43;  author: emblogic;  state: Exp;  lines: +3 -11
Eureka hurrrrrrrrreeeeeeeeeeeee successfully finded index of the charcaters.
—————————-
revision 1.3
date: 2014/02/17 06:56:40;  author: emblogic;  state: Exp;  lines: +30 -4
error in checking the index of ndc….
—————————-
revision 1.2
date: 2014/02/12 10:07:42;  author: emblogic;  state: Exp;  lines: +16 -2
successsfully findeed codelength
hureeeeeeeeeeeeeee;
—————————-
revision 1.1
date: 2014/02/12 09:53:45;  author: emblogic;  state: Exp;
Initial revision
=============================================================================

Posted in Uncategorized | Leave a comment

C Reference function rand() generate a random

number

This function of stdlib will generate a random number.

Usage of rand():

int rand (void);

Parameters:

The function rand() returns a pseudo-random integral number.
This number will be in the range 0 to RAND_MAX. The algorithm of rand() uses a seed to generate the series of numbers, this is why srand must be used to initialize the seed to some distinctive value.

The constant RAND_MAX is defined in standard library (stdlib).

The random numbers are delivered from a predetermined range.

Return value:

Will return an integer value between 0 and RAND_MAX.

as the srand() function used to seed the pseudo generator. the prototype of the srand() is:

void srand(unsigned int seed);

Posted in Uncategorized | Leave a comment

RCS

IT is revision control system.
With the help of this we can create an image file and we can change our data, and if we want back our previous data, we can do it with the help of rcs.
To do this, first you have to do rcs -i “name of file”,
after this you have to check in
ci “name of file”.
then check out with lock
co -l “name of file”.

Posted in Uncategorized | Leave a comment

DIFFERENT-DIFFERENT WORKING PROCESSES OF THREADS

What Is the Difference Between Hyper Threading & Multi-Core Technology?

The technology behind Hyper-Threaded, or HT, and multi-core processors enables processors to far exceed the performance of single-core, non-HT processors. The differences between the technologies are great, however, so it’s important to understand these differences before choosing what to use in your business computers. With either technology, however, you’ll achieve greater performance than you would with a regular processor

Hyper-Threading Technology

Hyper-Threading technology creates two virtual processing cores for each physical core present in a CPU. The physical core powers the virtual cores, which then share the responsibility of task processing. Each virtual core is identical to the other, and though neither is as powerful as the physical core, together they far exceed the physical core’s power when HT isn’t enabled. The use of these virtual cores enables the CPU to delegate tasks between cores in real time.

Advantages of Hyper-Threading

The workload created by a CPU-intensive operation, such as running two demanding programs at the same time — an operation that would slow down a single, physical core regardless of its raw power — is split between the virtual cores in a processor that utilizes HT technology. With two virtual cores tackling tasks at the same time, processing times are shorter, programs open faster and your computer will stay more responsive during multi-tasking. In a nutshell, Hyper-Threading increases processing efficiency.

Multi-Core Technology

Multi-core technology, which is most commonly available in dual-core, quad-core and hexa-core CPUs, is a technology that adds extra physical processing cores. In a single core CPU, tasks are processed one at a time on a first-come, first-serve basis. This can be problematic for multi-tasking as tasks will start backing up. In a processor with two or more cores, multi-tasking is far more efficient as multiple cores are available to process tasks. The more cores you have, the more data you can process without a dip in performance.

Advantages of Multi-Core

Multi-core technology has all the advantages of Hyper-Threading technology and more. Unlike HT technology, which uses two virtual cores for every physical core to process tasks more efficiently, multi-core technology adds physical cores. As a single physical core is more powerful than a single virtual core, a dual-core processor is more powerful than a single-core processor with Hyper-Threading. Many newer model CPUs are Hyper-Threaded and multi-core, which enables even greater performance. For example, if you have a quad core processor — that’s four cores — with HT, you would have eight virtual cores.

Posted in Uncategorized | Leave a comment

about proc

Proc is sometimes referred to as a process information pseudo-file system. It doesn’t contain ‘real’ files but runtime system information (e.g. system memory, devices mounted, hardware configuration, etc). For this reason it can be regarded as a control and information centre for the kernel. In fact, quite a lot of system utilities are simply calls to files in this directory. For example, ‘lsmod’ is the same as ‘cat /proc/modules’ while ‘lspci’ is a synonym for ‘cat /proc/pci’. By altering files located in this directory you can even read/change kernel parameters (sysctl) while the system is running. some of its common commands and their function are given as below:-

proc/PID/cmdline
Command line arguments.

/proc/PID/cpu
Current and last cpu in which it was executed.

/proc/PID/cwd
Link to the current working directory.

/proc/PID/environ
Values of environment variables.

/proc/PID/exe
Link to the executable of this process.

/proc/PID/fd
Directory, which contains all file descriptors.

/proc/PID/maps
Memory maps to executables and library files.

/proc/PID/mem
Memory held by this process.

/proc/PID/root
Link to the root directory of this process.

/proc/PID/stat
Process status.

/proc/PID/statm
Process memory status information.

/proc/PID/status
Process status in human readable form.

Posted in Uncategorized | Leave a comment

ABOUT “NICE” THIS IS USEFUL FOR :IPC

Most people don’t even know that running processes at a different priority is an option.  Many have never even heard of the nice command.  In this article, I will discuss what thenice command can do for your processes.

Every process needs some time on the CPU regardless of its priority.  Some get more time or run more often frequently because of their priority. Some are elevated in priority until their turn comes around.

A newly initiated process acquires the priority of it’s parent.  For example,  If you use the lscommand after you log in, the ls process will inherit the priority of your shell. By using the nicecommand, you can lower the priority of a process.  On some systems, the kernel will itself automatically boost the priority of a process when it has been waiting for a long time
without being run.

The range of values that the nice command use depends on the particular Unix system.  Typically, values will run between  -20 and +19, but only root can increase the priority of a process above its default.

There are two options to reduce/increase value of a process.  You can either do it using thenice command or the renice command.

To reduce a value of your shell you can do:

$ nice -10 mysqld

The -10 command lowers the priority of a process ten notches.  You also can run this:

$ renice +19 PID

The +19 bumps the priority of a running process to 19.

To learn more about nice, simply type ‘man nice’ and ‘man renice’ on your Unix system and get a better idea of what all the options it has.  While nicing our processes maybe an old style sysadmin work, it can become very useful when systems get busy.  You can have some processes take priority over others and have them complete their task faster

Posted in Uncategorized | Leave a comment

IPC using FIFO

head 1.1;
access;
symbols;
locks
arjun:1.1; strict;
comment @ * @;

1.1
date 2014.03.29.16.36.13; author arjun; state Exp;
branches;
next ;

desc
@ipc using fifo
here we are using one fifo for requesting client to server
we are able to collect the right data at server
@

1.1
log
@Initial revision
@

Posted in Uncategorized | Leave a comment

PUZZLE…Can you print anything without using semicolon(;)..?

here is code…

int main()

{if(printf(“hello”))

}

Posted in Uncategorized | Leave a comment

program to reverse link list using three pointers

#include
#include
#include
struct node
{
int a;
struct node *next;
};
struct node * create_ll();
void display(struct node *);
void rev(struct node *);
int main()
{
struct node *start ,*temp,*temp1;
int i,n;
start=create_ll();
temp=start;
printf(“enter node\n”);
scanf(“%d”,&n);
for(i = 0; i next=temp1;
temp=temp->next;
}
display(start);
rev(start);
return 0;
}
struct node *create_ll()
{
struct node *start;
start=malloc(sizeof(struct node));
printf(“enter data”);
scanf(“%d”,&start->a);
start->next=NULL;
return start;
}
void display(struct node *start)
{
struct node *temp;
temp=start;
while(temp!=NULL)
{
printf(“data is %d\n”,temp->a);
temp=temp->next;
}
}
void rev(struct node *start)
{
struct node *temp,*temp1;
temp=start;
temp1=start;
while(temp1->next != NULL)
{
temp=temp1->next;
temp1->next=temp1->next->next;
temp->next=start;;
start=temp;
}
printf(“the reversed list is ::::::::\n”);
while(start!=NULL)
{
printf(“data is %d\n”,start->a);
start=start->next;
}
}

Posted in Uncategorized | Leave a comment