Skip to content

Instantly share code, notes, and snippets.

@osandov
Created August 22, 2022 19:13
Show Gist options
  • Save osandov/4d360514ea0f59ede080c5176a21374b to your computer and use it in GitHub Desktop.
Save osandov/4d360514ea0f59ede080c5176a21374b to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
static const int NUM_KEEP = 5;
int main(void)
{
for (int i = 0; i < NUM_KEEP; i++) {
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return EXIT_FAILURE;
}
if (pid == 0) {
if (unshare(CLONE_NEWIPC) < 0) {
perror("unshare");
_exit(EXIT_FAILURE);
}
for (;;)
pause();
}
}
for (;;) {
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return EXIT_FAILURE;
}
if (pid == 0) {
if (unshare(CLONE_NEWIPC) < 0) {
perror("unshare");
_exit(EXIT_FAILURE);
}
_exit(EXIT_SUCCESS);
}
int wstatus;
if (waitpid(pid, &wstatus, 0) < 0) {
perror("waitpid");
return EXIT_FAILURE;
}
if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != EXIT_SUCCESS)
sleep(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment