• C

Thread programming - Basic question

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
mfaaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

shivsaCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenDBACommented:

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
mfaaAuthor Commented:
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
Challenges in Government Cyber Security

Has cyber security been a challenge in your government organization? Are you looking to improve your government's network security? Learn more about how to improve your government organization's security by viewing our on-demand webinar!

Kent OlsenDBACommented:

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
mfaaAuthor Commented:
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
Kent OlsenDBACommented:

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
mfaaAuthor Commented:
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
Kent OlsenDBACommented:
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
mfaaAuthor Commented:
Thanks Kent!  It works now!  
:)

MD
0
mfaaAuthor Commented:
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
Kent OlsenDBACommented:

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
mfaaAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.