Solved

Thread programming - Basic question

Posted on 2003-12-09
12
422 Views
Last Modified: 2010-04-15
I was reading thread programming chapter, and saw following line of code:

perror = ("Thread creation failed.");

I think perror is a thread function defined in the library <pthread.h>.  My question is what does perror do?  Does it print the error message?  to where?  

Second question would be how do I see all the functions defined in a particular library?  I am looking for command on linux to see <pthread.h> library.  

Your help is greatly appreciated!

M D
0
Comment
Question by:mfaa
  • 6
  • 5
12 Comments
 
LVL 24

Accepted Solution

by:
shivsa earned 100 total points
ID: 9905892
perror is print error. it print the error if the just above command failed and exited with some error, it catches that error and print it. in above case if thread_create fails it will print this error message. it is a good debugging tool provided in C.

u can check all the function in man pages.
like if some functions are defined in pthread.h file
pick any function and do
man pthread_create
0
 
LVL 45

Expert Comment

by:Kdo
ID: 9906550

Hi mfaa,

perror() is a function, not a variable and is usually prototyped in <stdio.h>.  It's a wonderful tool for reporting errors and usually results in better error messages than most programmers are willing to provide.

ThreadId = CreateThread ();
if (ThreadID == NULL)
  perror ("Could not create thread");

In the example above, perror() will print out your error message and then offer a "canned" description of the error.  This is sometimes overkill, but it is tremendously helpful when you're doing the same kinds of things over and over.

DatabaseThreadId = CreateThread ();
if (DataThreadID == NULL)
  perror ("Could not create database thread");

CommunicationThreadId = CreateThread ();
if (CommuncationThreadID == NULL)
  perror ("Could not create Communcation thread");

ConsoleThreadId = CreateThread ();
if (ColnsoleThreadID == NULL)
  perror ("Could not create Console thread");

You'll get your message that says exactly what you want to say (like what you are trying to do) and a description of the error.


Use ar(1) to see all of the modules and/or entry points in a library

ar -t stdlib.a

To see all of the entry points and external symbols, enter:

nm stdlib.a


Kent
0
 

Author Comment

by:mfaa
ID: 9912182
Hi Kent,

Thanks for responding with the answer.  Yes perror is a function, not a variable; that equal sign is a typo.  It should be

perror("Thread Creation Failed");

I tried ar(1),  ar -t stdlib.a and nm stdlib.a commands, none of these work...it says no such file or directory...it might be b/c of different version of linux?? I am using Red Hat 7.2.  

But anyway thanks for your help...it's very much clearer now.  



Hi Shivsa,

man pthread_create works....it works with other functions too.  I wanted to search for perror function..which library it belongs to...I tried with stdio.h and pthread.h....and it functions correctly with both of these...tried separately both of them.  

I tried man perror too.  It doesn't give me what library it belongs to.  If you know the answer, please let me know.  But your response was very much a help.  Thanks a lot!

MD
0
 
LVL 45

Expert Comment

by:Kdo
ID: 9912196

Hi MD,

stdlib.a should be in the default library directory.  Try these:

ar -t /usr/lib/stdlib.a
nm -t /usr/lib/stdlib.a

Kent
0
 

Author Comment

by:mfaa
ID: 9912251
Hi Kent,

I tried first one...it says no such file or directory.

for second one it says...invalid radix.  

I was just wondering...how do I earn points to ask more questions?  I think I am out of points and I need to ask tonns of questions?  Can I also answer questions and earn points?  If yes where do you go to answer questions?

Thanks.

MD
0
 
LVL 45

Expert Comment

by:Kdo
ID: 9912306

Apologies.  The file is /usr/lib/libc.a.  And the -t switch isn't appropriate for nm(1).

ar -t /usr/lib/libc.a
nm /usr/lib/libc.a


The "Ask a Question" page has options for signing for "premium service" and "buying points".  Check out both of those links:

http://www.experts-exchange.com/Programming/Programming_Languages/C/premiumServices.jsp
https://secure.experts-exchange.com/buyPoints.jsp
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:mfaa
ID: 9912331
Hi Guys,

This is not any point earning question..just looking for some good advice.  All the advices are greatly welcomed!  I just started a job as a C programmer.  It's my first job.  I graduated with BS in Computer Science.  In my college, I did lots of programming..but never did any detail..or real world thing.  Just did from the book.  After that I was learning .net programming on my own.  I liked it.  But when I got this opportunity, in this job market, I didn't want to miss it.  So I took the job. For two months I was just getting familiar with the tools, the machines...the functions of the sytem..nothing to do with programming.  It's  an aviation company...so had to learn much about the Air traffic control system first.  I really enjoyed learning those.  

Now comes the programming part.  At this point, I dont have to do lots of programming, but they want me to do debugging.  Someone else has already done the programming and is also doing debugging with me.  He has 10 yrs of experience..and is so quick with all the bug catching, where I dont have any clue, where the bug is.  

The point is I am really frustrated at this point, and feel like it's going to be impossible for me to do these.  I didn't have any experience with Linux system before.  I am learning linux programming on my own...I know I am learning a bit more everyday, but I am really depressed with my situation.

Any good advice on this.  How can I become a good C programmer?  Any good books recommendations?  Anything at all is very appreciated!  

Thanks!

MD
0
 
LVL 45

Expert Comment

by:Kdo
ID: 9912378
Hi mfaa,

You're doing C programming/debugging for an ATC school?  Very, very cool.  I did sys admin work for the Air Force ATC school in Biloxi, Ms several years ago.  The education that I got just hanging around those people has been immeasurable!  And the stories!!!  I spent much of my career around military bases, but the ATC guys were certainly the most entertaining.

Hand in there.  It's always tough moving into a new arena and having to endure these growing pains.  They will get better!

What compilers and/or tools do they have for debugging?

Kent
0
 

Author Comment

by:mfaa
ID: 9912395
Thanks Kent!  It works now!  
:)

MD
0
 

Author Comment

by:mfaa
ID: 9912443
For debugging we are using linux based compiler called insight...other programmer..he uses electric fence tool alot b/c of memory prob...but I am just using insight so far...we are the only two ppl working on this project and he is not on the same site...very far..comes once in a month here and teaches me stuff.  I am working for not ATC school, but ATC is just a part of it...working for USDOT...yes very interesting plaace to work for.  But I just want to be comfortable with waht I do.  I really want to learn these stuff.  

Any advice on how you became good at programming?

MD
0
 
LVL 45

Expert Comment

by:Kdo
ID: 9912487

The biggest thing that you can do to get better is to hang onto the job.  The easiest way to do that is to not change anything that has even the slightest chance of getting into "production" unless you are told to.  Most places just don't like "surprises".  :(

Where are you and the co-worker located?

In /usr/lib there should be a lot of libraries (file names ending with .a).  You can run nm against any/all of them to see what modules are where.


Kent
0
 

Author Comment

by:mfaa
ID: 9912584
Yes I am being careful about that.  And I am going to hang onto the job.  I am in NJ and my coworker is in California.

MD
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

760 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

20 Experts available now in Live!

Get 1:1 Help Now