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
linux_newbAsked:
Who is Participating?
 
asbharadwajConnect With a Mentor Commented:
Signals are an asynchronous communication mechanism and cannot be used in the
situation you describe.
The kind of signalling you want can be achieved by using
shared memory
files
sockets
pipes require that the two processes be related
fifos work even for unrelated processes

Refer this book for all the information
http://www.advancedlinuxprogramming.com/downloads.html
0
 
asbharadwajCommented:
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.
0
 
linux_newbAuthor Commented:
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

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
linux_newbAuthor Commented:
/sbin/ldconfig is working but rest is the same.
0
 
asbharadwajCommented:
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
0
 
asbharadwajConnect With a Mentor Commented:
>>How can i retrieve the information from the system about the exception and error occured?

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
0
 
linux_newbAuthor Commented:
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'




0
 
linux_newbAuthor Commented:
>>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?
0
 
asbharadwajConnect With a Mentor Commented:
To compile with the shared library in any other directory,
copy the libprint.so.1.0 to the directory (/usr/local/lib in your case) and do an ldconfig -v -n there.
That will create the symbolic link libprint.so.1 to which you'll have to create another link
for the linker name which is libprint.so for this example in the same directory.
Then do gcc -o client -lprint client.c and that will work.
note: The directory in which you r keeping the library has to be in the default search path of
ld. Otherwise you'll also have to use -LPATHtothelibrary while compiling.
0
 
asbharadwajCommented:
>>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.
0
 
linux_newbAuthor Commented:
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?


0
 
asbharadwajConnect With a Mentor Commented:
This is what I found on this at
http://lists.debian.org/debian-gcc/2002/debian-gcc-200203/msg00141.html

You mustn't invoke "ld" directly to build a shared library.  Instead,
you should use gcc like this:

gcc -fPIC -c libprint.cpp

gcc -shared -Wl,-soname -Wl,libprint.so.1 -o libprint.so.1.0 libprint.o

This worked for me and the client runs fine
0
 
linux_newbAuthor Commented:
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.

0
 
linux_newbAuthor Commented:
Thank you  asbharadwaj for all the help!!
0
All Courses

From novice to tech pro — start learning today.