mercredi 5 août 2015

How much DATA section memory is loaded along with Code section in C


I have created a shared library where I have static const data section and code section of 4 functions.

Here are the details of my static const data section,

static const u32 T0[256] = { 256 NON_ZERO value };
static const u32 T1[256] = {256  NON_ZERO value };
static const u32 T2[256] = {256  NON_ZERO value };
static const u32 T3[256] = {256  NON_ZERO value };
static const u32 T4[256] = {256  NON_ZERO value };
static const u32 T5[256] = {256  NON_ZERO value };
static const u32 T6[256] = {256  NON_ZERO value };
static const u32 T7[256] = {256  NON_ZERO value };
static const u32 T8[256] = {256  NON_ZERO value };
static const u32 T9[10] = {10 NON_ZERO value };

Different functions defined just below the static const section

int A();
int B();

// Access different index of T0 - T4 table 
void C();

void D();

As per my understanding the text/code section will contain the executable instruction whereas data section will contain initialized static data ( for simplicity in my case all are static const)

Inside C() function , different index of T0,T1,T2 and T3 are accessed in random order.

Intentionally, inside C(), I have not accessed T0[0].

However, every time I call C() function, it is loading T0[0] irrespective of whether T0[0] is accessed or not inside C() function.

My question is how much adjacent memory of data section is loaded along with code section?

I was thinking may be whole 4KB page loaded, so everytime C() function called, whole 4KB page loaded , therefore T0[0] is also loaded along with it.

but, the experimental result shows THIS CONCEPT IS NOT TRUE/CORRECT.

Let me explain it elaborately as below.

I have calculated the distance between function C() and different static const data as below

Base address of C - Base address of T0[0] = 3221 Bytes
Base address of C - Base address of T1[0] = 4345 Bytes
Base address of C - Base address of T2[0] = 5369 Bytes
Base address of C - Base address of T3[0] = 6393 Bytes

So, whenever C() is invoked , ONLY 64Bytes (i.e. T0[0] ) is loaded . T0[1], T0[2],... the part of T0 array which also belong to same 4KB page with C() NOT LOADED.

In other experiment, when I add another static const of 64Byte ( like static const int DATA=10; ) before static const u32 T0[256] = {.....}

these distances become

Base address of C - Base address of T0[0] =3385 Bytes [ =64 + Base address of C - Base address of T0[0]]
Base address of C - Base address of T1[0] = 4345+64 Bytes =4409 Bytes [=64 + Base address of C - Base address of T0[0]+1024]
Base address of C - Base address of T2[0] = 5369+64 Bytes = 5433 Bytes[=64 + Base address of C - Base address of T0[0]+2*1024]
Base address of C - Base address of T3[0] = 6393 +64 Bytes = 6457 Bytes[=64 + Base address of C - Base address of T0[0]+3*1024]

Now, although T0[0] still present in same 4KB page with C(), it is not loaded whenever C() is invoked.

Note : To calculate whether T0[0] is loaded or not , before invoking C(), I just flushed T0[0] using clflush() instruction and then after C() is invoked I have calculated access time using rdtsc().

Can you help me to explain/understand WHY T0[0] is always accessed/loaded whenever C() is invoked although it is not accessed/used inside C() ?

or any link to understand the memory layout of program and size of memory loaded during execution of program.

I am using Debian OS and gcc compiler.



via Chebli Mohamed

javascript function callback in C code using Emscripten


The task is to call a javascript function as a callback in order to show a progress of while-loop operation. E.g. JS:

var my_js_fn = function(curstate, maxstate){//int variables
console.log(curstate.toString() + " of " + maxstate.toString());
}

C pseudocode:

int smth_that_calls_my_fn(int i, int max) {
/*
the_magic to call my_js_fn()
*/
}
    int main(){
    //....
        while (i < max){
        smth_that_calls_my_fn(i,max);
        }
    //....
    return 0;
    }

How can I link smth_that_calls_my_fn and my_js_fn ?



via Chebli Mohamed

Difference between POSIX Standards


Posix COntains different standards 1003.1a,b...., 1003.2.... Sometimes it is shown as POSIX.1, POSIX.2, POSIX.4. From my understanding POSIX.1(1003.1)then why they have given differnt naming convention.

POSIX Contains different profiles pse51,52.... Right now the code, what I am compiling is like given -lpthread. How can I know which POSIX profile, I am using.

I want to use specifically use POSIX PSE51 Profile. How to compile a code specifically to PSE51



via Chebli Mohamed

Which files to read if I want to create a c progam to give an output same as given by ps ax


In which files can we find the values displayed by ps ax command: TTY,STAT,TIME,COMMAND in /proc/processId directory



via Chebli Mohamed

Filling a polygon


I created this function that draws a simple polygon with n number of vertexes:

void polygon (int n)
{
    double pI = 3.141592653589;
    double area = min(width / 2, height / 2);
    int X = 0, Y = area - 1;
    double offset = Y;
    int lastx, lasty;

    double radius = sqrt(X * X + Y * Y);
    double quadrant = atan2(Y, X);

    int i;

    for (i = 1; i <= n; i++)
    {
        lastx = X; lasty = Y;
        quadrant = quadrant + pI * 2.0 / n;

        X = round((double)radius * cos(quadrant));
        Y = round((double)radius * sin(quadrant));

        setpen((i * 255) / n, 0, 0, 0.0, 1); // r(interval) g b, a, size

        moveto(offset + lastx, offset + lasty); // Moves line offset
        lineto(offset + X, offset + Y); // Draws a line from offset
    }
}


How can I fill it with a solid color? I have no idea how can I modify my code in order to draw it filled.



via Chebli Mohamed

Residual characters printed on stdout with socket communication


In a socket TCP communication, I have a server and a client. Both can read from and write to the socket.

The code is written in C and uses Linux system calls recv and write.

recv saves the received string in:

char message_array[2000];

Another array with the same dimensions is used as source for the write process.

After both the reading and writing process the following operation is performed, to clear all the array elements:

memset(&message_array, 0, sizeof(message_array));

Moreover, a fflush is performed on stdin and stdout at every write and every read process.

The server prints on its stdout what it writes and what it receives.

If I send small messages from both the terminals ("hello", "hi") even for several times (18-20), all appears to work correctly. But if I try to send longer messages (longer than 5 characters, but shorter than 2000!), the server side has some strange behaviour: it prints the message received from the client, but then it inserts a random number of the trailing characters of the previous messages. For example I have:

CLIENT MESSAGE: hello1
SERVER MESSAGE: hello2
CLIENT MESSAGE: hello3
SERVER MESSAGE: hello4 
CLIENT MESSAGE: some other characters5
SERVER MESSAGE: hello6
CLIENT MESSAGE: a long phrase 7
ters5

After several messages, the 5 characters ters5 from a previous message appear as trailing characters after the actual message, which was just a long phrase 7. Continuing the "conversation", other unexpected newline and "old" characters appears on the stdout of the server.

The code of the read and write routine is the following:

void *connection_handler_read(void *socket_read_desc)
{
    int read_sock = *(int*)socket_read_desc;
    int read_size;
    char remote_message[MAX_STRING_LEN];

    while( (read_size = recv(read_sock, remote_message, MAX_STRING_LEN, 0)) > 0 )
    {
        printf(remote_message);
        fflush(stdout);
    }

    free(socket_read_desc);
    connection_active = 0;
    return 0;
}

void *connection_handler_write(void *socket_write_desc)
{
    int write_sock = *(int*)socket_write_desc;
    char local_message[MAX_STRING_LEN];

    while( connection_active != 0 )
    {
        scanf ("%[^\n]%*c", local_message);
        write(write_sock, local_message, strlen(local_message));
        memset(&local_message, 0, sizeof(local_message));
        fflush(stdin);
    }

    free(socket_write_desc);
    return 0;
}

These two functions are invoked in the main as threads with pthread_create after a new socket connection is succesfully created (so, after a successful accept). They obviously operate on the same socket.

My questions are:

1) Is this a software problem or a socket problem?

2) Which trick should I have followed, in addition to memset and fflush?



via Chebli Mohamed

How to convert a jpg tp yuv in C?


Even though a question of this nature sounds very similar, I am having problems in converting a jpg image to yuv in C (without using opencv).

This is what I have understood as of now, how to solve this problem :

  1. Identify the structure of file formats for jpg and yuv. i.e what each byte in the file actually contains. This is what I think jpg format looks like.
  2. With the above structure I tried to read a jpg file and tried to decipher its 18th and 19th bytes. I did type cast them to both char and int but I don`t get any meaningful values for width and height of the image.
  3. Once I have read these values, I should be able to convert them from jpg to yuv. I was looking at this resource.

  4. Appropriately, construct yuv image and write it to a (.yuv) file.

Kindly help me by pointing me to appropriate resources. I will keep updating my progress on this post. Thanks in advance.



via Chebli Mohamed