{"id":16116245,"url":"https://github.com/frol/timelimit","last_synced_at":"2025-04-06T08:44:25.045Z","repository":{"id":30600035,"uuid":"34155223","full_name":"frol/timelimit","owner":"frol","description":"The fork of http://devel.ringlet.net/sysutils/timelimit/ with added CPU time limit functions","archived":false,"fork":false,"pushed_at":"2019-05-11T20:04:47.000Z","size":48,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-12T14:19:06.448Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/frol.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-18T06:28:15.000Z","updated_at":"2021-10-12T03:33:38.000Z","dependencies_parsed_at":"2022-09-08T13:31:05.356Z","dependency_job_id":null,"html_url":"https://github.com/frol/timelimit","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frol%2Ftimelimit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frol%2Ftimelimit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frol%2Ftimelimit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frol%2Ftimelimit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frol","download_url":"https://codeload.github.com/frol/timelimit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247457740,"owners_count":20941906,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-09T20:21:43.312Z","updated_at":"2025-04-06T08:44:25.027Z","avatar_url":"https://github.com/frol.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"timelimit utility\n=================\n\nThis is the fork of [Peter Pentchev's timelimit util](http://devel.ringlet.net/sysutils/timelimit/).\n\nThe timelimit utility executes a given command with the supplied arguments and terminates the\nspawned process after a given time with a given signal. If the process exits before the time\nlimit has elapsed, timelimit will silently exit, too.\n\nThe Fork Notes\n--------------\n\nThis fork added a feature to limit child process CPU time via `setitimer(ITIMER_VIRTUAL)`, use `-c`\nas you would use `-t`, but unfortunately, you cannot change the signal sent to a child process by\ndesign, it will be always `SIGVTALRM` (see details about `ITIMER_VIRTUAL` in man pages to\n`setitimer`).\n\nIt is important to understand that:\n\n* `setitimer(ITIMER_VIRTUAL)` is applied directly to the child process and does not count CPU\ntime of grand children;\n* `setitimer(ITIMER_VIRTUAL)` sends SIGVTALRM to the child process and if it ignores it (see an\nexample below), the limitation does not work at all, so I suggest to use `-c` in a combination\nwith `-t`.\n\n\nOther tries to implement this:\n\n* `timer_create(CLOCK_PROCESS_CPUTIME_ID)` is not inherited by `fork`/`execve`, so it cannot be\napplied here.\n\nOther added features:\n\n* `-Q` - silence child program stdout/stderr/stdin;\n* `-u \u003cuid\u003e` - drop privileges to the specified user id;\n* `-g \u003cgid\u003e` - drop privileges to the specified group id.\n\n\nExample of bypassing `setitimer(ITIMER_VIRTUAL)`\n------------------------------------------------\n\n```c\n#include \u003cstdlib.h\u003e\n#include \u003csignal.h\u003e\n\nint main()\n{\n    sigset_t mask;\n\n    /* Block timer signal */\n    sigemptyset(\u0026mask);\n    sigaddset(\u0026mask, SIGVTALRM);\n    sigprocmask(SIG_SETMASK, \u0026mask, NULL);\n   \n    int i, a = 0;\n    for (i = 0; i \u003c 1000000000; ++i)\n        a += i;\n}\n```\n\nIf you compile this and try to limit CPU time, say, with 0.1 second (`timelimit -c 0.1 ./example`),\nthis won't have any effect as the `./example` program ignores `SIGVTALARM`.\n\nI couldn't find any solution to this issue, so if anybody will help me to solve it I will be very\npleased.\n\nFor now, I use real time timer in addition to the CPU time timer and see if CPU time was exceeded\nafter I kill the process:\n\n```bash\n$ time timelimit -s KILL -t 1.0 -c 0.1 ./blackbox_binary\n```\n\nExtra notes\n-----------\n\nI use `timelimit -t 1.0 -c 0.1` in a combination with\n`cgmemtime --kill-children-on-next --memory-limit \u003cbytes\u003e` (there is also my fork with added options)\nto test black-box solutions for online programming contests.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrol%2Ftimelimit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrol%2Ftimelimit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrol%2Ftimelimit/lists"}