...
Code Block |
---|
#include < sys/types.h > #include < unistd.h > int glob = 6; /\* external variable in initialized data \*/ char buf\[\] = "ateksti writestdout to stdoutvirtaan\n"; int main(void) < 0)perror("fork error");else if (pid == 0){ /* child */ glob++; /* modify variables */ var++; " >{ { int var; /\* automatic variable on the stack \*/ =88; pid_t pid; var = 88; if (write(STDOUT_FILENO, buf, sizeof(buf)-1) \!= sizeof(buf)-1) perror("write errorkirjoitusvirhe"); printf("beforeennen fork-funktiota\n"); /\* we don't flush stdout \*/ if ( (pid = fork()) < 0) perror("fork errorvirhe"); else if (pid == 0) { /\* childlapsiprosessi \*/ glob++; /\* modifymodifioidaan variablesmuutujia \*/ var++; } else sleep(2); /\* parentvanhempi \*/ printf("pid=%d,glob=%d,var=%d\n",getpid(), glob, var); exit(0); } |
Lapsiprosessi luodaan, kun:
...