Solved

Synchonization Implementation.

Posted on 2003-10-27
14
363 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
14 Comments
 
LVL 2

Expert Comment

by:asbharadwaj
ID: 9632543
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
ID: 9655710
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
ID: 9655756
/sbin/ldconfig is working but rest is the same.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:asbharadwaj
ID: 9655909
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
ID: 9655930
>>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
ID: 9655994
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
ID: 9656036
>>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
 
LVL 2

Assisted Solution

by:asbharadwaj
asbharadwaj earned 125 total points
ID: 9656587
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
ID: 9656715
>>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
ID: 9675924
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
ID: 9676381
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
ID: 9691790
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
ID: 9692541
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
ID: 9699434
Thank you  asbharadwaj for all the help!!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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