Skip to content

Instantly share code, notes, and snippets.

@nskvortsov
Created April 10, 2014 19:06
Show Gist options
  • Save nskvortsov/10412764 to your computer and use it in GitHub Desktop.
Save nskvortsov/10412764 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void) {
int status, w;
pid_t pid;
pid = fork();
if (pid == -1) /* Fehler! */
{
perror ("fork"); /* gibt den entsprechenden Fehlercode aus (globale Variable "errno") */
exit(EXIT_FAILURE);
}
if (pid == 0) /* Child */
{
printf ("\t\tChild says: My PID is %lu so I am.\n", (unsigned long) getpid());
fflush (stdout);
sleep (20); /* 5 Sekunden warten; Prozess wird in den Zustand "Blockiert" versetzt */
printf ("\t\tChild says: Exit in one second...\n");
fflush (stdout);
sleep(1);
exit (EXIT_SUCCESS);
}
else /* Parent */
{
printf ("Parent says: I'm waiting for PID %d...\n", pid);
fflush (stdout);
w = waitpid (pid, &status, 0); /* Warte, bis Kind-Prozess beendet */
printf ("Parent says: Child exited with status %d.\n", status);
fflush (stdout);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment