Quantcast
Channel: Forum Pasja Informatyki - Najnowsze pytania
Viewing all articles
Browse latest Browse all 65225

[C] Fork ()

$
0
0

Zadanie: Stwórz drzewo procesów następujący sposób:

  - 3 - 4
1
  - 2- 5

Każdy proces uśpij . Główny proces ma czekać na zakończenie procesu dzieci.

Kod:

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

int main (void){
pid_t child_a, child_b;

child_a = fork();

if (child_a == 0) {
    printf ("Process 2 \n");
    sleep(10);
	pid_t child = fork();
	if (child == 0){
	printf ("Process 3");
	sleep(10);
	}
	else{
	wait(NULL);
	}
}
 else {
	printf("Process 1 /n");
    child_b = fork();

    if (child_b == 0) {
        /* Child B code */
        printf("Process 4 \n");
        sleep(10);
        pid_t child1 = fork();
        if (child1 == 0 ){
			printf ("Process 5 \n");
			sleep(10);
		}
		else {
		wait (NULL);
		}
        //ParentChild Code
    }
    wait(NULL);
    printf ("Process Ended...\n");
    
}
	
}

I wszystko pięknie się robi, ale nie wiem dlaczego zawsze drukuje process ended gdy jakikolwiek sie zakonczy. A nie wiem jak to naprawic. Ma ktos jakieś pomysły?


Viewing all articles
Browse latest Browse all 65225

Trending Articles