Link to home
Start Free TrialLog in
Avatar of linux_newb
linux_newb

asked on

Synchonization Implementation.

Hi all!!

I am new to linux programming and i am not sure if ia m putting my question properly.

I have to port an application program from Windows  to linux. Can somebody tell me how to implement critical sections and event objects in linux. So far, i have understood that Critical Sections (used by threads of single process) can be implemented by using mutex and Event objects by using semaphores. Please let me know if i am correct/wrong.

My program uses Event objects both within the same process and across the processes. How should i go about doing this?  Please help me linuxgurus outhere.

Thanks
-Anu
Avatar of asbharadwaj
asbharadwaj

The most common method of communication between 2 processes in linux is a signal
A signal can be sent by one process to any other process and any process has a pre-determined
reaction to a signal. However, this reaction can be redefined for most of the signals thereby allowing
a process to act in its own way thus acting as an event mechanism.

Do a man signal to find out more info
or visit http://caml.inria.fr/oreilly-book/html/book-ora168.html

As far as threads within a single process go, you are correct. Binary semaphores and mutexes are
what you need for communication.
Avatar of linux_newb

ASKER

Thanks for the help!

I have few more doubts.
1. How can i retrieve the information from the system about the exception and error occured?
2. I am trying to work with shared libraries. And i used the tutorial at the url given below:

http://www-106.ibm.com/developerworks/library/l-shobj/

After installing the shared library in /usr/local/lib ,the sample program (using the shared library) doesn't seem to recognise the function defined in shared library.

I tried installing the shared library in the current directory. Even after setting the LD_LIBRARY_PATH, ldconfig command is not recognised. And gcc doesnt seem to have any -lprint option. Am i supposed to give here the name of library? I tried giving real, soname and linker name but nothing seems to work. Is there anything else that needs to be done apart from the things mentioned in tutorial?

Don't know how much am i making sense:(

Thanks again
-Anu

/sbin/ldconfig is working but rest is the same.
What errors do you get?
Are you able to compile the sample program without linking?
Post the commandline you r using and the exact error message you get
SOLUTION
Avatar of asbharadwaj
asbharadwaj

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
FInally, I  am able to compile the sample program now by installing the shared library in my working directory. Problem was in

ln -sf libprint.so libprint.so.1  &
$ gcc -o client client.c -L . -lprint

Man page says  ln [OPTION]... TARGET [LINK_NAME]
So, now $ gcc -o client client.c -L . libprint.so  works well.

But i am still not able to compile my program if i install the library in /usr/local/lib. Errors are

$ gcc -c client.c
$ ld  -o client -lc client.o
ld: warning: cannot find entry symbol _start; defaulting to 08048184
client.o: In function `main':
client.o(.text+0x29): undefined reference to `printstring'




>>If you are talking of errors in a system call, there is a global variable called errno that is set >>to an appropriate value by any system call that encounters an error and there is a function >>perror that converts the value to a sensible error message and prints it on the stderr of >>your program.
>>do a man perror and you will get enough info

Will this also work in case of multi-threaded application?  Any way to get information regarding exceptions too?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>Will this also work in case of multi-threaded application?

Refer
http://www.calpoly.edu/cgi-bin/man-cgi?perror+3

As per this, perror and strerror are threadsafe and will work even for multithreaded apps.

>>Any way to get information regarding exceptions too?
What exceptions? Dont think there's any exception handling mechanism in linux.
Not very sure.
Thanks! you were right that library has to be included in the search path of ld. I was doing everything except this. So, its working.

But now i have another problem. Till now i was using c files. I tried replacing printf's with cout's and working with cpp files. Shared library is compiling well and when i am compile+linking  my sample program (client.cpp using shared library), executable file is being created. But i am getting following message when i try to run the executable:

$ g++ -o client client.cpp -L. -lprint
$ ./client
./client: relocation error: ./libprint.so.1: undefined symbol: __dso_handle

And when i compile and link separately, i am getting following messages during linking and no executable is created:

$ g++ -c client.cpp
$ ld -lc -o client client.o -L. -lprint
ld: warning: cannot find entry symbol _start; defaulting to 080483a0
client.o: In function `main':
client.o(.text+0x19): undefined reference to `std::cout'
client.o(.text+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
client.o: In function `__static_initialization_and_destruction_0(int, int)':
client.o(.text+0x5a): undefined reference to `std::ios_base::Init::Init()'
client.o(.text+0x65): undefined reference to `__dso_handle'
client.o: In function `__tcf_0':
client.o(.text+0x89): undefined reference to `std::ios_base::Init::~Init()'
client.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
./libprint.so: undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
./libprint.so: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'

Is there anything else that has to be taken care of when working with c++ files?


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well thanks  asbharadwaj!! Using
gcc -shared -Wl,-soname -Wl,libprint.so.1 -o libprint.so.1.0 libprint.o

client is working fine.

Coming back to Event objects. I am a little confused regarding signals. I just want my process to wait until some flag is signalled implying that it can go ahead and continue with its execution. I can't follow how can i use signals here.
Instead of using signals for event mechanism, can i use condition variables or some other interprocess communication object.

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you  asbharadwaj for all the help!!