3 ms·
Following is the c-programming example which explains how fork system call works. shashi@linuxtechi ~}$ vim 1_fork.c #include<stdio.h> #include<unistd.h>
by joosters 7y ago
Following is the c-programming example which explains how fork system call works.
shashi@linuxtechi ~}$ vim 1_fork.c
#include<stdio.h>
#include<unistd.h>
Int main(void)
{
printf("Before fork\n");
fork();
printf("after fork\n");
}
shashi@linuxtechi ~}$
shashi@linuxtechi ~}$ cc 1_fork.c
shashi@linuxtechi ~}$ ./a.out
Before fork
After fork
shashi@linuxtechi ~}$
How exactly is that demonstrating how fork() works? The article is completely missing any explanation - like why 'After fork' was only printed once and not twice as would be initially expected (It's unclear to me: while stdio buffering can cause strangeness if you don't flush buffers before a fork(), surely in this case both processes will exit cleanly and should flush their post-fork outputs?)
- DSMan195276 7y agoIt should print `after fork` two times, they just wrote it wrong. They clearly wrote the whole thing manually, since they used `vim` and not `cat` to display the contents of the file, and they show it printing `After fork` and not `after fork`. I think they just forgot to put the line twice. The fact that they have `Int` and not `int` means their example doesn't actually compile, so I don't think they tested this one.
- tyingq 7y agoSure but doesn't address the article mentions of vfork, wait, waitpid, kill, etc. Or other unmentioned stuff, like variations of kill/killpid().
- moltensodium 7y agoThis example is wrong, it should print "after fork" twice as both processes would run that.