c - Understanding fork() in linux -


i have following code

void main() {  pid_t pid,pid1;  pid = fork();  if(pid==0) {   pid1= getpid();    printf("\n child %d" ,pid);   printf("\n child b %d",pid1); } else {    pid1 = getpid();   printf("\n parent c %d:",pid);    printf("\nparent d %d:",pid1);   } } 

i not understanding why getting same process id b , c. can me here?

pid1 = getpid(); 

that run in child process , hence gives child process id.

pid = fork(); 

that initiated parent process return value available both parent , child. however, returns different value parent , child processes. straight fork man page:

the pid of child process returned in parent, , 0 returned in child

so in both cases (b , c), pid of child process.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -