0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

理解 fork() マルチプロセス

Posted at
fork_test.c
# include <unistd.h>
# include <stdio.h>
int main ()
{
    pid_t fpid;
    int count=0;
    printf("This will not be forked.\n");
    fpid=fork();
    printf("Will be forked from here.\n");
    if (fpid < 0)
        printf("error in fork!");
    else if (fpid == 0) {
        printf("i am the child process, my process id is %d\n",getpid());

        count++;
    }
    else {
        printf("i am the parent process, my process id is %d\n",getpid());
        count++;
    }
    printf("count= %d\n",count);
    return 0;
}

gcc fork_test.c
a.out

 ./a.out
This will not be forked.
Will be forked from here.
Will be forked from here.
i am the parent process, my process id is 28355
count= 1
i am the child process, my process id is 28356
count= 1


 ./a.out
This will not be forked.
Will be forked from here.
i am the parent process, my process id is 28369
Will be forked from here.
count= 1
i am the child process, my process id is 28370
count= 1


 ./a.out
This will not be forked.
Will be forked from here.
i am the parent process, my process id is 28383
count= 1
Will be forked from here.
i am the child process, my process id is 28384
count= 1

./a.out
This will not be forked.
Will be forked from here.
i am the parent process, my process id is 28420
count= 1
Will be forked from here.
i am the child process, my process id is 28421
count= 1





0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?