Skip to content

Instantly share code, notes, and snippets.

@qnighy
Created August 29, 2015 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qnighy/b0e04ee807c71af0f67d to your computer and use it in GitHub Desktop.
Save qnighy/b0e04ee807c71af0f67d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
// pid_t parent = getpid();
while(true) {
pid_t child = fork();
if(child < 0) {
fprintf(stderr, "fork() failed\n");
return 1;
}
if(child == 0) {
execlp("date", "date", NULL);
} else {
int status;
waitpid(child, &status, 0);
}
}
return 0;
}
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <spawn.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
// pid_t parent = getpid();
while(true) {
pid_t child;
char spawn_argv0[] = "date";
char * const spawn_argv[2] = {spawn_argv0, NULL};
int result = posix_spawnp(
&child,
"date",
NULL,
NULL,
spawn_argv,
environ);
if(result < 0) {
fprintf(stderr, "posix_spawnp() failed\n");
return 1;
}
int status;
waitpid(child, &status, 0);
}
return 0;
}
#!/usr/bin/make -f
EXECS = a b
CC = gcc
CFLAGS = -std=c99 -O2 -Wall -Wextra -ggdb
all: $(EXECS)
clean:
$(RM) $(EXECS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment