user3635707
user3635707

Reputation: 5

what is the error in the below c program?

a.c:
#include "a.h"
double GetTimeStamp()
{
            struct timespec start;

            if((clock_gettime( CLOCK_REALTIME, &start)) == -1 )
            {
              perror("clock gettime\n");

            }

/* I am calculating the Clock granularity here the granularity is basically how long that timer interrupt
 * will last while it's processing the background task.*/
            //micro seconds output
        return (1e3 * start.tv_sec + start.tv_nsec * 1e-3);

}

int a()
{
intr_rx_time = GetTimeStamp();
IPLatency = intr_rx_time;

}

a.h:
a();
double intr_rx_time, IPLatency;

b.c:
#include "a.h"
extern double IPLatency;
uint32 ipLatency;

int main()
{
  ipLatency = (uint32)IPLatency;
  printf("Current IP Latency microseconds: %ld\n", ipLatency);
}

In the above : I am calculating a timestamp from a.c program. Later I am reading the calcualted timestamp value in b.c program as shown above. But I am getting the output as 0 in b.c program. what is the error in the above program ?? could someone please help .

Upvotes: 0

Views: 52

Answers (2)

pmg
pmg

Reputation: 108938

The intr_rx_time and IPLatency in a.c and b.c are different objects.

Add extern to the header file (change from "definition" to "declaration") and define the variables (without extern) in a single one of the .c files.

// a.h
int a(void);
extern double intr_rx_time, IPLatency;

// a.c
#include <stdio.h>
#include <time.h>
#include "a.h"
double GetTimeStamp(void)
{
    struct timespec start;
    if ((clock_gettime(CLOCK_REALTIME, &start)) == -1) {
        perror("clock gettime");
    }
    /* I am calculating the Clock granularity here the granularity is basically how long that timer interrupt
     * will last while it's processing the background task. */
    //micro seconds output
    return (1e3 * start.tv_sec + start.tv_nsec * 1e-3);
}

int a(void)
{
    intr_rx_time = GetTimeStamp();
    IPLatency = intr_rx_time;
}

// b.c
#include <inttypes.h>
#include <stdio.h>
#include "a.h"
double IPLatency;
uint32 ipLatency;

int main(void)
{
    ipLatency = IPLatency;
    printf("Current IP Latency microseconds: %" PRIu32 "\n", ipLatency);
}

Upvotes: 1

user207064
user207064

Reputation: 665

1) move double intr_rx_time, IPLatency; from a.h to a.c

2) prototype in a.h

a();

should be

int a(void);

3) add below lines to a.h

#include<time.h> 
#include<stdio.h>
typedef unsigned int uint32;

4) change %ld to %d in b.c

Upvotes: 0

Related Questions