On Fri, Jul 12, 2002 at 07:09:06PM +0200, Stefano Simonucci wrote: > Visto che ricorre cosi' spesso potreste spiegarmi che significa forcare > (o che un processo forca ecc....).
e' una chiamata di sistema che permette di "dividere" il programma in due processi (denominati padre e figlio) allego qui due sorgenti c scritti da richard w. stevens per "Advanced programming in the unix Environment" che sono chiarificatori buona compilazione ciao Samuele -- Samuele Giovanni Tonon <[EMAIL PROTECTED]> http://www.linuxasylum.net/~samu/ Acid -- better living through chemistry. Timothy Leary
#include <sys/types.h> #include <unistd.h> #include <stdio.h> int glob = 6; /* external variable in initialized data */ char buf[] = "a write to stdout\n"; int main(void) { int var; /* automatic variable on the stack */ pid_t pid; var = 88; if (write(STDOUT_FILENO, buf, sizeof(buf)-1) != sizeof(buf)-1) perror("write error"); printf("before fork\n"); /* we don't flush stdout */ if ( (pid = fork()) < 0) perror("fork error"); else if (pid == 0) { /* child */ glob++; /* modify variables */ var++; } else sleep(2); /* parent */ printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var); exit(0); }
#include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <stdio.h> int main(void) { pid_t pid; if ( (pid = fork()) < 0) perror("fork error"); else if (pid == 0) { /* first child */ if ( (pid = fork()) < 0) perror("fork error"); else if (pid > 0) exit(0); /* parent from second fork == first child */ /* We're the second child; our parent becomes init as soon as our real parent calls exit() in the statement above. Here's where we'd continue executing, knowing that when we're done, init will reap our status. */ sleep(2); printf("second child, parent pid = %d\n", getppid()); exit(0); } if (waitpid(pid, NULL, 0) != pid) /* wait for first child */ perror("waitpid error"); /* We're the parent (the original process); we continue executing, knowing that we're not the parent of the second child. */ exit(0); }