Git Product home page Git Product logo

timebox's Introduction

timebox

Politely timebox unix programs

Timeboxing is a time management technique. Some humans find it beneficial to start with a fixed time period and size their work to fit. Can machines practice timeboxing too? Sure! That's kind of what an RTOS does. If you want to timebox a regular old unix program though, you'll need something else...

Silly Example

See how many files can be counted under your home directory in 5-6 seconds:

time timebox 5.0 1.0 find ~ | wc -l

The two numbers 5.0 and 1.0 are the run period and grace period, respectively. If the program doesn't exit before 5 seconds is up, timebox sends it SIGTERM. If it still hasn't exited after 6 seconds, timebox sends it SIGKILL. This is not unlike what init does with running processes at shutdown.

Other Applications

  • Problems that have "best effort" solutions, like search. Exhaustive searches can take a long time. Perhaps you always want to respond with something within a second. In your search program, print the best result you've found so far when you receive SIGTERM. You can then run it under timebox.

  • Suppose you're a cost-constrained cloud user being charged for cpu time. (I mean, who isn't these days?) You want to crack a hash solve a problem, but can't spend more than $X, even if that means you might not get a solution at all. Timebox it!

  • College programming assignments where you a submit source code solution. A grading program compiles, runs, and checks the ouput of your program. But what if your program is naughty and never exits? Or what if you submit a small C++ program which generates GBs of compiler errors that take forever to format and print? Timebox it!

  • You want to expose a server, but only for a limited, short period of time. Forgetting and leaving it open indefinitely would be risky. Timebox it!

Okay But Why

This was inspired by "Unix System Call Timeouts". Specifically, the "waitpid equivalent with timeout?" question on Stack Overflow. There are all kinds of ways to do this...

SIGALRM

Set an alarm and wait for SIGALRM. This is hard to get right, and you need to watch out for race conditions.

signalfd

Turn SIGCHLD into a selectable event via signalfd. Only works on Linux.

self-pipe

Use djb's self-pipe trick to turn SIGCHLD into a selectable event. This is sort of the poor man's signalfd, and is probably the most reliable and broadly portable way to do it. Although I wonder if the "can't safely mix select with signals" reason still applies on modern unixes.

sigtimedwait

Use sigtimedwait with a signal set of SIGCHLD. This is a nice, simple approach, but wouldn't mix well with an event loop doing other things. Not that we're doing that here, but it might be relevant in your program.

EOF on child-to-parent pipe

timebox uses yet another way: wait for EOF on a child-to-parent pipe, which happens when the pipe's write side is closed at child exit.

timebox's People

Contributors

acg avatar

Stargazers

Mourad Kejji avatar MonkeyMike avatar Jacob Bijani avatar Scott Ivey avatar Allan Laal avatar Paweł Rychlik avatar Adrian Lanzafame avatar Miles Richardson avatar Mark Steve Samson avatar C.J. avatar  avatar Ivan Vashchaev avatar Stefaney Roberts avatar Steven K. Baum avatar Ezequiel Maraschio avatar  avatar Zvi avatar Joey Pabalinas (jp) avatar  avatar Kevin Dean avatar Miki Oracle avatar Steven Gonzalez avatar Varun Ramesh avatar Mahmoud Rusty Abdelkader avatar Saadh avatar Rido avatar Steffen Dienst avatar Asgeir Storesund Nilsen avatar  avatar  avatar torchhound avatar Wesley Ross Justice avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.