Milan Stephan
Fotografie & IT

Zurück zur SPiC Übersicht

run.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

static void die(const char *msg) {
	perror(msg);
	exit(EXIT_FAILURE);
}

int main(int argc, char *argv[]) {

	for (int i = 2; i < argc; i++) {

		printf("Programm wird gestartet: %s mit Paramter %s\n", argv[1], argv[i]);

		pid_t p = fork();
		if (p < 0) {
			die("fork");
		}
		if (p == 0) {
			// Kindprozess
			execlp(argv[1], argv[1], argv[i], NULL);
			die(argv[1]);
			// Im Kindprozess IMMER exit'en!!
		}

		// Hier geht's ganz normal im Vaterprozess weiter

		int status;
		pid_t c = waitpid(-1, &status, 0);
		if (c < 0) {
			die("waitpid");
		}

		if (WIFEXITED(status)) {
			printf("exit mit %d\n", WEXITSTATUS(status));
		} else if (WIFSIGNALED(status)) {
			printf("signal mit %d\n", WTERMSIG(status));
		}

	}
}