Start New Discussion within our Software Development Community

can anyone explain this programing codes?


proc1.c
#include <stdio.h>
int main()
{
int fork_return;
fork_return = fork();
printf("PID=%d, PPID=%d, fork_return=%d\n", getpid(), getppid
(), fork_return);
sleep(10); /* wait for 10 sec */
return 0;
}

proc2.c
#inlcude <stdio.h>
int main()
{
int chpid=fork();
if (chpid < 0)
{
printf("ERROR\n");
exit(1);
}
else if(chpid == 0)
{
printf("Child process: ");
printf("PID=%d, PPID=%d\n", getpid(), getppid());
sleep(10); /* waits 10 sec */
}
else
{
printf("Parent process: ");
printf("PID=%d, PPID=%d\n", getpid(), getppid());
sleep(10); /* waits 10 sec */
}
return 0;
}

Comments
Use code tags.

The getpid() function gets the (unique) process identification from the system. You need to include the process.h header. Beware, this function is not standard C.

#include <stdio.h>
#include <process.h>

int main()
{
   printf("This program's process identification number (PID) "
          "number is %X\n", getpid());
   printf("Note: under DOS it is the PSP segment\n");
   return 0;
}
This question has already been answered. Start a new discussion instead.