473,385 Members | 1,461 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

proper way to return errors to parent process

The main() function of my C program is doing some system calls.
If any of my system call fails, I want to terminate the program
and propagate the exact cause of exit (the error number) to the parent
process.
What is the correct way of doing this ?

Should I do,
exit(errno);
Or
return(errno);

Nov 14 '05 #1
2 1696
In article <11**********************@o13g2000cwo.googlegroups .com>,
ju**********@yahoo.co.in says...
The main() function of my C program is doing some system calls.
If any of my system call fails, I want to terminate the program
and propagate the exact cause of exit (the error number) to the parent
process.
What is the correct way of doing this ?

Should I do,
exit(errno);
Or
return(errno);


Neither. At least not if you want it to be portable. The values
known to work for sure are 0, EXIT_SUCCESS and EXIT_FAILURE.

exit(errno) may do what you want, and it may not. On platforms where
it does work, then return will in all likelihood also work and the
shell or some other calling entity won't be able to tell the
difference.

Note that return is not a system or library call, so no parens are
required.
--
Randy Howard (2reply remove FOOBAR)
"Making it hard to do stupid things often makes it hard
to do smart ones too." -- Andrew Koenig
Nov 14 '05 #2
ju**********@yahoo.co.in writes:
The main() function of my C program is doing some system calls.
If any of my system call fails, I want to terminate the program
and propagate the exact cause of exit (the error number) to the parent
process.
What is the correct way of doing this ?

Should I do,
exit(errno);
Or
return(errno);


You can do either. (BTW, the parentheses on "return(errno);" are
superfluous but harmless; "return errno;" is slightly preferred.) The
main difference is that exit() works from anywhere; return terminates
the program only if it's executed within main().

However, there's no guarantee that the information will be correctly
passed back to the caller. The only strictly portable values you can
return from the main program are 0, EXIT_SUCCESS, and EXIT_FAILURE.

I'm guessing that you're using a Unix-like system. You should
probably ask about this in comp.unix.programmer. (Some things to
consider: how much information is actually passed to the environment,
and whether the system has particular conventions for a program's exit
status.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bob Swerdlow | last post by:
My application starts up a number of processes for various purposes using: self.popen = popen2.Popen3("/usr/local/bin/python -O "myscript.py") and then shuts them down when appropriate with...
5
by: Lee | last post by:
I am using a modal window and an iFrame to try and pull a return value back. I am doing this across domains. I have the value returned from the modal window to the iFrame window but I can not...
94
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it...
4
by: atv | last post by:
Whatis the proper way to handle errors from function calls? For example, i normally have a main function, with calls to mine or c functions. Should i check for errors in the functions called...
0
by: Scott McChesney | last post by:
All - I am in the process of developing a custom control that mimics the Office 2003 "Inbox" as closely as possible. After a great deal of frustrating work trying to do a heavily custom-drawn...
48
by: Michel Rouzic | last post by:
I know it must sound like a newbie question, but I never really had to bother with that before, and I didn't even find an answer in the c.l.c FAQ I'd like to know what's the really proper way...
3
by: darrel | last post by:
I have a .aspx.vb page that calls a function in a .vb file that has a try catch. Usually, I use a catch to display an error. In this case, I want the parent page to know if the function errors...
4
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. ...
1
by: =?Utf-8?B?Q3VzdG9t?= | last post by:
I tried to update Vista OS to Vista SP1, when system restarts during installation it checks disk volume for errors, in the process of scaning disk if finds some errors as follows: Checking file...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.