Created
April 27, 2012 07:10
-
-
Save stetro/2506779 to your computer and use it in GitHub Desktop.
C Prozesse Erzeugen und Ausgaben generieren
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#define PROCESS 3 | |
#define LOOP 20 | |
// Prozessinstruktionen | |
void run_process(int i); | |
int main(void) | |
{ | |
int i; // Tempvariable | |
// Starte die 3 Prozesse | |
for(i=0;i<PROCESS;i++) | |
{ | |
// Prozess starten | |
int proc = fork(); | |
// Bei Problemen | |
if(proc <0){ | |
printf("Fehler beim starten des Prozesses"); | |
return EXIT_FAILURE; | |
} | |
else if(proc == 0) | |
{ | |
// Prozess instruktionen ausfuehren | |
run_process(i); | |
return EXIT_SUCCESS; | |
} | |
} | |
wait(); // Warte bis alle Kindprozesse beendet wurden | |
fflush(stdout); // Puffer leeren und Ausgabe erzwingen | |
// INFO | |
printf("\nAlle Prozesse wurden beendet\n"); | |
return EXIT_SUCCESS; | |
} | |
// Prozessinstruktionen | |
void run_process(int i){ | |
int j; // Laufvariable | |
// INFO | |
printf("Prozess mit der Kennung %d wurde gestartet - pid = %d \n",i+1,getpid()); | |
for(j=0;j<LOOP;j++) | |
{ | |
printf("%d",i+1); // Ausgabe der Kennung | |
usleep(1000000); // Eine Sekunde warten | |
fflush(stdout); // Puffer leeren und Ausgabe erzwingen | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment