Does execv replace the current process like this? - process

If in my code, I were to call execv, and then I had several lines of code after the call to execv, would those lines get executed, or would they not get executed, since whatever was started by execv replaces the current process?

They wouldn't be executed, unless the execv() call failed. execv() completely replaces the program running in the process that calls it.

They would not get executed, unless you forked the thread and called execv on just one of them.

Related

In AHK, what is the difference between Try/Catch and OnError()? When to use one or the other?

I am currently writing an AHK script that reads and writes files.
I would like to handle the possible I/O errors,
but the doc isn't clear to me regarding wether I should use Try/Catch or OnError().
What is the difference between the two? And when to use one or the other?
So, after some more research, here is my understanding:
Try/Catch: Use it to:
Specifically identify certain lines of code over which it will be applied.
Then, if you would like, proceed with the execution.
A Try/Catch allows to proceed with the execution of the command after the Try block that failed.
(But, a Try/Catch does not allow to proceed with the execution of the command after the one that failed within the Try block. For example if 5 commands are wrapped, then if the 2nd one threw, it will not be possible to proceed with the execution from the 3rd after doing something in the Catch-block.)
OnError(): Use it to
Deal with any unhandled error.
Block (or not) the default error dialog.
In any cases the thread execution is stopped after you handled the error.
There can be multiple OnError() handlers active at a time,
and you can decide, in which order they will be executed (or to stop the execution after any one of them) when an error occurs.
If all handlers return 0 all handlers are called one after the other, then the default error message is displayed, then the thread exits.
If any handler returns a non-zero integer, the thread exits without calling the following handlers and without displaying the default error dialog.

Resque not performing any methods

I've got Resque set up in my Rails 3.2 app and have an after hook which successfully calls
Resque.enqueue(SomeJob, self.class.name, id)
I can see the job getting fired off, but no methods in my SomeJob class are getting executed. I've got a logger set up confirming the SomeJob gets executed but the log statement inside my self.perform block never gets called.
def self.perform
log.debug("working")
end
So far I've tried methods named self.work, work, self.perform, perform and nothing seems to get called. The Resque documentation seems to be geared towards a pending 2.0.0 release but I can't quite get this to work even with 1.24.1 or 1.22.
What is the magic method that gets called in Resque? Is there any way to explicitly call it in Resque.enqueue?
At first glance it looks like you're passing in two arguments (self.class.name and id), but the self.perform method isn't able to accept them, so it could be silently failing with an invalid argument error.
My suggestion would be to change the self.perform method to the following:
def self.perform(class_name, id)
log.debug("working: class_name=#{class_name} id=#{id}")
end

Allow only one running instance of a program

How can i restrict my program to run only instance? Currently i'm running my program as daemon(starts and stops automatically), and when user clicks and tries to launch again(which is not a valid usecase), process gets launched in user context and i would like to avoid this for many reasons.
How can i achieve this?
As of now i'm getting list of processes and doing some checks and exiting at the begining itself but this method is not clean, though it solves my problem.
can someone give me a better solution?
And i'm using ps to get process list, is there any reliable API to get this done?
Use a named semaphore with count of 1. On startup, check if this semaphore is taken. If it is, quit. Otherwise, take it.
Proof of concept code: (put somewhere near application entry point)
#include <semaphore.h>
...
if (sem_open(<UUID string for my app>, O_CREAT, 600, 1) == SEM_FAILED) {
exit(0);
}
From the sem_open documentation,
The returned semaphore descriptor is available to the calling process until it is closed with sem_close(), or until the caller exits or execs.

Stop execution in repetion structure. VB.NET

I need to stop the execution of a program in VB.net. Within a repeating structure the program checks whether a variable is true or false. When the variable is false I need to stop the program execution. I do not want to close the program but simply stop it. One way I found was raising an exception, but I would not want to display the error message to the user. What is the best way to do it ?
Do
If myvariable=false then
throw new exception... 'Silent exception
end if
Loop until ….
Davis
Edit Based on below comment.
In that case I would use something like.
Do
If myvariable=false then
Return
end if
Loop until ….
I am really not sure what you are wanting to do.
You can close the application by using Application.Exit()
Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.
If you want to be really abrupt you can call the End Statement.
Terminates execution immediately.
and
The End statement stops code execution abruptly, and does not invoke the Dispose or Finalize method, or any other Visual Basic code. Object references held by other programs are invalidated. If an End statement is encountered within a Try or Catch block, control does not pass to the corresponding Finally block.
You can use the Stop Statement to suspend execution but it will invoke the debugger.
If the Stop statement is encountered in code that is running outside of the integrated development environment (IDE), the debugger is invoked. This is true regardless of whether the code was compiled in debug or retail mode.
Or if you are just wanting to exit out of your Do Statement you can try this
Do
If myvariable=false then
Exit Do
end if
Loop until ….
You can exit a loop with an exit statement. Alternatively, you can use your method of throwing an exception and surround the loop in a Try/Catch block which catches that specific exception and ignores it.
If you really want to stop then you can use the Stop keyword. However, I'm not confident from your question that this is what you actually want as it has different outcomes depending on whether it's running as a exe or in the IDE.
Some more context to your question would be useful in finding the best answer for your problem.

node.js - testing to ensure no 'dangling callbacks'

I would like my node.js tests to ensure that, once the test is over and test.finish() or similar is called, that there is no more code waiting to be run. No more I/O waiting to finish, no more timers waiting to fire, etc. etc.
Is this possible in node.js?
When using nodeunit each test function keeps running until test.done() has been called. Every test function needs to call this. This way you can make sure your callbacks have been executed. I also like to use async module to clean up my code(callbacks) a bit.
Are you using test.expect() at the beginning of each test and test.done() at the end of each one? Think of them like begin and end braces.
I wrote my own, which essentially spins up a node instance for each test. If your test leaves a callback dangling, node just won't exit, and the whole test suite hangs instead of exiting early with success (which would be bad!)