Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CCUnix] add function to call a process with a timeout #143

Open
grayswandyr opened this issue Jul 18, 2017 · 5 comments
Open

[CCUnix] add function to call a process with a timeout #143

grayswandyr opened this issue Jul 18, 2017 · 5 comments

Comments

@grayswandyr
Copy link
Contributor

The subject says it all.

@c-cube
Copy link
Owner

c-cube commented Jul 20, 2017

That would be useful indeed, but I haven't found a way of doing it without multi-threads. Technical suggestions welcome.

@hcarty
Copy link
Contributor

hcarty commented Jan 22, 2018

Could this go into a CCProcess or similar module under containers.threads? It could provide similar functionality to Lwt's https://ocsigen.org/lwt/3.1.0/api/Lwt_process

@c-cube
Copy link
Owner

c-cube commented Jan 22, 2018

@hcarty I think it should be in the unix sub-library anyway, but the problem is I'm not sure how to do it even with threads (Thread.kill doesn't work, and I think it's a bit messy to kill the subprocess while the thread is running).

Maybe we can do sth where we fork manually and setup a ulimit/rlimit, or setup some alarm (Unix.alarm?) before exec'ing the command. Do you know if alarm is preserved by exec?

@c-cube
Copy link
Owner

c-cube commented Jan 22, 2018

I tried this little experiment:

let f () =
  let st = Unix.gettimeofday() in
  let pid = Unix.fork() in
  if pid=0 then (
    Unix.alarm 4;
    Unix.execv "sleep" [| "sleep"; "10" |]
  ) else (
    Unix.waitpid [] pid;
    Printf.printf "done (%.2f)" (Unix.gettimeofday() -. st)
  );;

f();;

and it does print "done" after 4s. So it might work. What do you think?

@hcarty
Copy link
Contributor

hcarty commented Jan 22, 2018

That's pretty promising - I didn't realize the signal would persist like that.

It would be nice to have something which doesn't rely on Unix.fork for better Windows compatibility. If I can get some time for it I'll try to work something out with threads and killing the subprocess after its time is up. And capture the subprocess's output if I'm feeling adventurous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants