💾 Archived View for thrig.me › tech › fpg.c captured on 2024-05-10 at 12:34:12.

View Raw

More Information

⬅️ Previous capture (2023-04-19)

-=-=-=-=-=-=-

// fpg - forground process group hypothesis testing

#include <sys/types.h>
#include <sys/wait.h>

#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>

void logsig(int sig);

int
main(void)
{
	pid_t parent = getpid();
	if (tcsetpgrp(STDIN_FILENO, parent) != 0) err(1, "tcsetpgrp");
	pid_t child  = fork();
	if (child < 0) err(1, "fork failed");
	if (child) {
		signal(SIGINT, logsig);
		fprintf(stderr, "parent %d\n", parent);
		int status;
		waitpid(child, &status, 0);
		_exit(0);
	} else {
		signal(SIGINT, logsig);
		pid_t newpid = getpid();
		fprintf(stderr, " child %d\n", newpid);
		sleep(999);
		_exit(0);
	}
}

void
logsig(int sig)
{
	fprintf(stderr, "SIG %d -> %d\n", sig, getpid());
	_exit(0);
}