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;
}

Dave Sinkula commented: Use code tags. +0

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.