Default exit function implementation [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to implement default behavior of exit call. I don't know what should I do and what is the most suitable way to do this. I have read that it should close file descriptors and something else.
Should I close default streams (stdout,err and in) ?
How to exit from nested functions calls ? Using goto is bad practice, what is the best way to break out ?
Thanks.

Do all of the things listed in exit(3), then invoke the _exit(2) system call. Alternatively, use longjmp(3) to jump back up to the main() function, then return from it. This invokes the same behavior as calling exit(3), and is just as dependent on the C runtime, so if exit(3) is unavailable for some reason, returning from main() will probably not work correctly either.
Unfortunately, AFAIK there is no portable way to enumerate all of the functions which may have been registered with atexit(3) and on_exit(3), so you'll have to keep track of those manually (i.e. every time you call atexit(3) or on_exit(3), append the function pointer to a list). Flushing stdio(3) is 3 straightforward fflush(3) calls.
You do not need to close any streams or file descriptors; the OS should do that automatically (the OS must not leak streams and fd's, so it is responsible for cleaning them up).
NB: longjmp() is almost always wrong under C++; throw an exception instead. It generally should only be used under straight C.

Related

What happens if I use close() on a char array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I had an interesting question. I have:
char buf[100]
And I decided to try using close(buf)
Code compiled, the program works. But is there any point in using close() like this?
Thank you.
Assuming "close" is the function from posix, most likely nothing will happen but it's also possible stuff could break badly.
Arrays in c and c++ decay to pointers, close takes an int. Implicitly converting a pointer to an int is not allowed by the c++ spec but some compilers allow it anyway (doing some testing it looks like modern g++ only allows it if -fpermissive is specified).
Most likely the integer that results from said conversion will be large, file descripters are usually small, so most likely close will just return a bad file descriptor error and do nothing but if it does happen to match a file descriptor then things could get interesing.....
It should not compile. The compiler should emit warnings.
The behaviour is undefined
No, there is no point in using close on a char array
There is also no meaning in doing that. What would you want to achieve?

How can I know whether a file is being used by other process by using c++ under Linux? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I want to copy a file to another file. But it always failed. I checked my code carefully, but I still can not know what's wrong with it. So I guess may be one of the two files is being used by other process. So firstly, I want to check whether they are being used by other process.
If so, is there any solution to stop the procedure by using c++??
If your code always fails, I would assume that it's because there is a problem with the code, because that's where the problem is in approximately 99.999%1 of the cases.
That said, there isn't much point in checking if you can open a file before you try, since some other process might open (or lock, or remove, ...) the file between your checking and your opening it.
The reliable method is to just go ahead and try to open the file, and then handle failure gracefully (which you need to do anyway).
Footnotes:
1 Scientific fact that I just made up.

Is it possible to send a pointer to function via socket? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two processes of the same program, possibly running on two different machines.
I'd like the process P2 to receive a function pointer from process P1 via socket.
Is is possible? Is it a good practice?
EDIT: more interesting would be to send the code of the function too, but I'm skeptic about this. Would it be possible?
You can send a function pointer from one process to another, the same way you can send a pointer to some other object.
The problem is that the pointer may not actually point to the function as it exists in the target process. Especially if the OS is protecting itself with things like ASLR.
You could also send the code across, provided you had some way of figuring out where it ended, and that it was position independent code, and that your environment allowed you to write arbitrary data to memory and then call it.
But, to be honest, there are better ways to achieve what you seem to want, such as the use of RPC (remote procedure calls), in a more portable manner.

Preferred method of error handling method [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a file based hash table thing that can run into various filesystem or user errors. Initially I created all the data functions as bools like
bool add(int key, int value)
bool get(int key, int &value)
and so on where the input/output would go through the parameters and the success/fail would come as the function result.
Then I had some mmf wrapper classes I needed to fool-proof for the same project and I realized that they can fail at the constructor in which case returning a bool is not really an option so I added a bunch of
if (!somethingthatindicatesfail) throw std::exception("description here");
to them.
So now i have something that throws exceptions inside something that returns a bool and then there is system error codes that I also need to include in the error log.
Its a mess.. I'm going to rewrite all of the fail scenario logic but before I do, what is your preferred error handling/conveying method?
The end result I'm imagining is a module that doesn't crash but logs the errors, prevents further damage to the data and advises the user to shut it down.
As you already pointed out, you can't use return codes from constructors. So, if you want a single method that works for all the code, your only real choice is exception handling.
Note, however, that in some cases, it's preferable to just abort instead. In particular, exceptions will attempt to unwind the stack, but if the situation is dire enough, it's possible that could cause further damage, and aborting (existing without unwinding the stack) is a better option.
For that case, it can make sense to have (for example) a separate watch-dog that logs the problem and re-starts the program when/if it crashes. Being a separate process, it can continue and execute reasonably even if the program itself is bollixed up to the point that its only reasonable choice is to abort.

Matlab functions and parallel_invoke [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
In my project I have a lot of MATLAB functions. For each function I call Initialize function, when the application starts. I tried to call this functions using parallel_invoke. I tried it several times and allways it takes more time, that code without this. Can somebody explain this ?
Is there is something specific in MATLAB or Initialize functions ?
The Matlab Runtime only has a single interpreter thread, so calling Matlab functions in parallel does not gain you anything: when the first function A is called, the MCR acquires a lock and only releases it when that function exits. Calling another function B during that period results in trying to acquire the lock, which then obviously just blocks until A finishes. The reason you see it taking up more time is probably due to the overhead of the locking/parallel_invoke.
I'm not sure what you mean with for each function I call Initialize function: unless you are using multiple Matlab dlls (which will afaik be less performant than having a single dll) you only need to call it's Initalize/Terminate once.