Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <stdlib.h>
- int main() {
- pid_t potomki[10];
- switch (potomki[1] = fork()) {
- case 0:
- /*proces potomny */
- switch(potomki[2] = fork()){
- case 0:
- {
- //proces P4
- switch(potomki[4] = fork()){
- case 0:
- {
- printf("P4:%d:%d\n", getpid(), getppid());
- exit(0);
- }
- case -1:
- printf("Blad funkcji fork");
- exit(1);
- default:
- wait(NULL); //oczekiwanie na zakończenie procesu potomnego
- }
- //proces P5
- switch(potomki[5] = fork()){
- case 0:
- {
- printf("P5:%d:%d\n", getpid(), getppid());
- exit(0);
- }
- case -1:
- printf("Blad funkcji fork");
- exit(1);
- default:
- wait(NULL); //oczekiwanie na zakończenie procesu potomnego
- }
- //proces P6
- switch(potomki[6] = fork()){
- case 0:
- {
- //proces P10
- switch(potomki[10] = fork()){
- case 0:
- {
- printf("P10:%d:%d\n", getpid(), getppid());
- exit(0);
- }
- case -1:
- printf("Blad funkcji fork");
- exit(1);
- default:
- wait(NULL); //oczekiwanie na zakończenie procesu potomnego
- }
- printf("P6:%d:%d\n", getpid(), getppid());
- exit(0);
- }
- case -1:
- printf("Blad funkcji fork");
- exit(1);
- default:
- wait(NULL); //oczekiwanie na zakończenie procesu potomnego
- }
- printf("P2:%d:%d:%d\n", getpid(), getppid(), potomki[9]);
- exit(0);
- }
- case -1:
- printf("Blad funkcji fork");
- exit(1);
- default:
- wait(NULL); //oczekiwanie na zakończenie procesu potomnego
- }
- switch(potomki[3] = fork()){
- case 0:
- {
- //proces P7
- switch(potomki[7] = fork()){
- case 0:
- {
- printf("P7:%d:%d\n", getpid(), getppid());
- exit(0);
- }
- case -1:
- printf("Blad funkcji fork");
- exit(1);
- default:
- wait(NULL); //oczekiwanie na zakończenie procesu potomnego
- }
- //proces P8
- switch(potomki[8] = fork()){
- case 0:
- {
- printf("P8:%d:%d\n", getpid(), getppid());
- exit(0);
- }
- case -1:
- printf("Blad funkcji fork");
- exit(1);
- default:
- wait(NULL); //oczekiwanie na zakończenie procesu potomnego
- }
- //proces P9
- switch(potomki[9] = fork()){
- case 0:
- {
- printf("P9:%d:%d\n", getpid(), getppid());
- exit(0);
- }
- case -1:
- printf("Blad funkcji fork");
- exit(1);
- default:
- wait(NULL); //oczekiwanie na zakończenie procesu potomnego
- }
- printf("P3:%d:%d:%d\n", getpid(), getppid(), potomki[9]);
- exit(0);
- }
- case -1:
- printf("Blad funkcji fork");
- exit(1);
- default:
- wait(NULL); //oczekiwanie na zakończenie procesu potomnego
- exit(0);
- }
- case -1:
- printf("Blad funkcji fork\n");
- exit(1);
- default:
- /* proces macierzysty*/
- printf("PM1: pid przodka: %d ppid: %d\n",
- getpid(), getppid());
- /*oczekiwanie na zakonczenie potomka*/
- wait(NULL);
- exit(0);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment