process

how do differentiate a parent process and child process through the process id while it is executing through fork() command.

can i get the process id of both processes and can i do any operation on the process by using process id

You check the return value from fork. The man page says...

fork() causes a process to duplicate. The child process is an almost-exact
duplicate of the original parent process; it inherits a copy of its parent's
code, data, stack, open file descriptors, and signal table. However, the parent
and child have different process id numbers and parent process id numbers.

 If fork\(\) succeeds, it returns the PID of the child to the parent process, 

and returns 0 to the child process. If it fails, it returns -1 to the parent
process and no child is created.

fork() is a strange system call, because one process(the original) calls it, but
two processes(the original and its child) return from it. Both processes continue
to run the same code concurrently, but have completely separate stack and data
spaces.

 A process may obtain its own process id and parent process id numbers by 

using the getpik() and getppid() system calls, respectively. Here's a synopsis
of these system calls:

System Call: int getpid()
int getppid()

getpid() and getppid() return a process's id and parent process's id numbers,
respectively. They always succeed. The parent process id number of PID 1 is 1.

To illustrate the operation of fork(), here's a small program that duplicates and
then branches based on the return value of fork():

$ cat fork.c
#include <stdio.h>
main()
{
    int pid;
    printf("I'm the original process with PID %d and PPID %d.\n",
           getpid(),getppid());
    pid=fork();  /* Duplicate. Child and parent continue from here.*/
    if (pid!=0)  /* pid is non-zero, so I must be the parent  */
        {
           printf("I'm the parent process with PID %d and PPID %d.\n",
                   getpid(),getppid());
           printf("My child's PID is %d.\n", pid);
        }
    else  /* pid is zero, so I must be the child. */
        {
           printf("I'm the child process with PID %d and PPID %d.\n",
                   getpid(),getppid());
        }
    printf("PID %d terminates.\n",pid);  /* Both processes execute this */
}

$fork.exe ... run the program.
I'm the original process with PID 13292 and PPID 13273.
I'm the parent process with PID 13292 and PPID 13273.
My child's PID is 13293.
I'm the child process with PID 13293 and PPID 13292.
PID 13293 terminates. ... child terminates.
PID 13292 terminates. ... parent terminates.

thank you very much for your reply.
Now I could clearly understand the concept about fork,the example you have given is an added advantage to understand the concept very clearly
-Raja

click here

Collins :cool:

i can't understand the following lines about fork()

Due to the fact that the CPU scheduler will assign a time quantum to each process, the parent or the child process will run for some time before the control is switched to the other and the running process will print some lines before you can see any line printed by the other process. Therefore, the value of MAX_COUNT should be large enough so that both processes will run for at least two or more time quanta. If the value of MAX_COUNT is so small that a process can finish in one time quantum, you will see two groups of lines, each of which contains all lines printed by the same process.

#include <stdio.h>
#include <sys/types.h>

#define MAX_COUNT 200

void ChildProcess(void); /* child process prototype /
void ParentProcess(void); /
parent process prototype */

void main(void)
{
pid_t pid;

 pid = fork\(\);
 if \(pid == 0\) 
      ChildProcess\(\);
 else 
      ParentProcess\(\);

}

void ChildProcess(void)
{
int i;

 for \(i = 1; i &lt;= MAX_COUNT; i\+\+\)
      printf\("   This line is from child, value = %d\\n", i\);
 printf\("   *** Child process is done ***\\n"\);

}

void ParentProcess(void)
{
int i;

 for \(i = 1; i &lt;= MAX_COUNT; i\+\+\)
      printf\("This line is from parent, value = %d\\n", i\);
 printf\("*** Parent is done ***\\n"\);

}

My doubt is ,Even though we split the process by using the process id and we set the time quantum is minimum,
is it run by the same proces, i mean either by the parent only or child only

#include <stdio.h>
#include <string.h>
#include <sys/types.h>

#define MAX_COUNT 200
#define BUF_SIZE 100

void main(void)
{
pid_t pid;
int i;
char buf[BUF_SIZE];

 fork\(\);
 pid = getpid\(\);
 for \(i = 1; i &lt;= MAX_COUNT; i\+\+\) \{
      sprintf\(buf, "This line is from pid %d, value = %d\\n", pid, i\);
      write\(1, buf, strlen\(buf\)\);
 \} 

}

if i execute the code the process would be like

but ,do both processes execute the while loop seperately
can i get the result like that

parent 1 ...............200

child 1...............200