1

I created a logging library liblog.so. I want to share a variable called File *rt_file it's defined in lib_rt.h and also this header inherited by log_rt.c,log_linux.c ==> liblog.so,logd.c ==> logd and also log_rt_client.c ==> log_rt_client. Here i created a daemon logd which puts(actually should) file pointer rt_file.

That's what i want to do. Here liblog.so is a dynamic libarary, logd starts on boot up and it should set file pointer to rt_file and log_rt_client should get the file pointer and print some logs.

Problem comes to share the common variable rt_file. I tried other suggestions suggested in

stackoverflow [1]: first solution was to use extern and complete the task. For my situation i though for [2] this. Rather than making a global variable put the variable to scoped file and use get and set functions to access the values.

but both options didn't worked for me let me Explain.

  1. I (Have to)compiled all the files liblog.so logd and log_rt_client separately so, it was showing error when log_rt_client had declaration of rt_file but not defined because it's defined in logd. which was not(can't) compiled with log_rt_client. Though all the files linked to liblog.so
  2. When i tried second options, results were unexpected, logd calls to liblog.so set's and get's the file pointer but when i ran log_rt_client rt_file doesn't had the expected pointer(File pointer). Means allocation for rt_file for both are differen.

Can any one suggest me a solution. Hope this explains everything.

Community
  • 1
  • 1
Arun Gupta
  • 810
  • 2
  • 9
  • 26

1 Answers1

0

You have to use shared memory

Dynamic lib is mapped into process address space, So each process will see their copy of rt_file mapped into their data segment (i mean any global variable. in your case it is rt_file).

Sasi V
  • 1,074
  • 9
  • 15