Вопрос по c – Как использовать Fork () для создания только 2 дочерних процессов?

17

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

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

int main ()
{
    /* Create the pipe */
    int fd [2];
    pipe(fd);

    pid_t pid;
    pid_t pidb;


    pid = fork ();
    pidb = fork ();

    if (pid < 0)
    {
        printf ("Fork Failed\n");
        return -1;
    }
    else if (pid == 0)
    {
        //printf("I'm the child\n");
    }
    else 
    {
        //printf("I'm the parent\n");
    }

    printf("I'm pid %d\n",getpid());

    return 0;
}

Error: User Rate Limit Exceeded

I'm pid 6763
I'm pid 6765
I'm pid 6764
I'm pid 6766

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded TwilightSparkleTheGeek
Error: User Rate Limit Exceededman forkError: User Rate Limit Exceeded mimoralea
Error: User Rate Limit Exceededfork()Error: User Rate Limit Exceededfork()Error: User Rate Limit Exceeded user3629249
fork (2)Error: User Rate Limit Exceeded dmckee

Ваш Ответ

5   ответов
3

and

31

pid = fork (); #1
pidb = fork (); #2

if(fork()) # parent
    if(fork()) #parent
    else # child2
else #child1
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded mimoralea
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
13

fork()

if (pid = fork()) {
    if (pid = fork()) {
        ;
    } 
} 

for (i = 0; i < n; ++i) {
    pid = fork();
    if (pid > 0) {   /* I am the parent, create more children */
        continue;
    } else if (pid == 0) { /* I am a child, get to work */
        break;
    } else {
        printf("fork error\n");
        exit(1);
    }
}
0

int main (void) {
    pid_t pid, pid2;
    int status;

    pid = fork();

    if (pid == 0) { //child process
        pid2 = fork();
        int status2;

        if (pid2 == 0) { //child of child process
            printf("friends!\n");
        }
        else {
            printf("my ");
            fflush(stdout);
            wait(&status2);
        }
    }
    else { //parent process
        printf("Hello ");
        fflush(stdout);
        wait(&status);
    }

    return 0;
}

Hello my friends!
0

Похожие вопросы