Solved

Synchonization Implementation.

Posted on 2003-10-27
14
344 Views
Last Modified: 2010-04-22
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
0
Comment
Question by:linux_newb
  • 7
  • 7
14 Comments
 
LVL 2

Expert Comment

by:asbharadwaj
Comment Utility
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
 

Author Comment

by:linux_newb
Comment Utility
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
 

Author Comment

by:linux_newb
Comment Utility
/sbin/ldconfig is working but rest is the same.
0
 
LVL 2

Expert Comment

by:asbharadwaj
Comment Utility
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
 
LVL 2

Assisted Solution

by:asbharadwaj
asbharadwaj earned 125 total points
Comment Utility
>>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
 

Author Comment

by:linux_newb
Comment Utility
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
 

Author Comment

by:linux_newb
Comment Utility
>>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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Assisted Solution

by:asbharadwaj
asbharadwaj earned 125 total points
Comment Utility
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
 
LVL 2

Expert Comment

by:asbharadwaj
Comment Utility
>>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
 

Author Comment

by:linux_newb
Comment Utility
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
 
LVL 2

Assisted Solution

by:asbharadwaj
asbharadwaj earned 125 total points
Comment Utility
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
 

Author Comment

by:linux_newb
Comment Utility
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
 
LVL 2

Accepted Solution

by:
asbharadwaj earned 125 total points
Comment Utility
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
 

Author Comment

by:linux_newb
Comment Utility
Thank you  asbharadwaj for all the help!!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now