Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hadrianw
Last active January 24, 2016 01:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadrianw/8e6f9c1007953b37f768 to your computer and use it in GitHub Desktop.
Save hadrianw/8e6f9c1007953b37f768 to your computer and use it in GitHub Desktop.
Process Race - wait for a child process to finish, kill the rest.
/*
Compile with:
$ gcc prace.c -o prace -std=c99 -pedantic -Wall -Wextra
Example usage in shell script:
$ ((sleep 4; echo foo)& (sleep 2; echo bar)& exec ./prace)
bar
$
*/
#define _XOPEN_SOURCE
#include <stddef.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
int
main(int argc, char *argv[])
{
(void)argc; (void)argv;
wait(NULL);
signal(SIGTERM, SIG_IGN);
kill(0, SIGTERM);
return 0;
}
/*
Copyright (C) 2016 by Hadrian Wegrzynowski
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment