/* ------------------ fork.c -----------------------------*/
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int g = 2;

int main(void)
{
	pid_t pid;
	int l = 3;
        int exit_status;

	printf("process id(%d): parent g=%d, l=%d\n", getpid(), g, l);

	if((pid=fork())<0){
		perror("fork error");
		exit(1);
	} else if(pid == 0) {
		g++;
		l++;
		printf("I am a new child and my proced id is: %d \n", getpid());
		printf("My parent process id is: %d \n", getppid());
		printf("\n The child process now terminates....., "
                       "g and l values of child process are as follows: g=%d, l=%d\n", g, l);
	} else {
	        pid = wait(&exit_status);
	        g = g * 100;
		l = l * 300;
		printf("I am the PARENT process of a new child process %d \n and my proced id is: %d \n", pid, getpid());
		printf("Parent process id of the Parent Process is: %d \n", getppid());
	        printf("\n The parent process now terminates");
	}

	printf("\n\n ....Who Am I ?? (%d): g=%d, l=%d\n", getpid(), g, l);
	printf("\n\n ....Who is my parent ?? (%d): ", getppid());

	return 0;
}
---------------- gcc –o fork fork.c ------------------

[gjung@localhost LinuxTasks]$ gcc -o fork fork.c
[gjung@localhost LinuxTasks]$ ./fork
process id(21739): parent g=2, l=3
I am a new child and my proced id is: 21740 
My parent process id is: 21739 

 The child process now terminates....., g and l values of child process are as follows: g=3, l=4

 ....Who Am I ?? (21740): g=3, l=4

 ....Who is my parent ?? (21739): I am the PARENT process of a new child process 21740 
 and my proced id is: 21739 
Parent process id of the Parent Process is: 4642 

 The parent process now terminates

 ....Who Am I ?? (21739): g=200, l=900

 ....Who is my parent ?? (4642): [gjung@localhost LinuxTasks]$ 
[gjung@localhost LinuxTasks]$

/* ----------------- fork_exec.c --------------------------- */
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	pid_t pid;
	int exit_status;

	if((pid=fork())<0){
		perror("fork error");
		exit(1);
	} else if(pid == 0) {
		printf("Hi\n");
		execl("./fork", "fork", (char *)0);
		printf("Can I print out after execl....?? \n my process id is: %d\n my parentid is: %d\n", getpid(), getppid());
	} else {
		pid = wait(&exit_status);
		if(exit_status == 0) printf("Child process exits successfully...\n");
		printf("\n\nI am the parent of fork_exec and my process id is: %d\n my parentid is: %d\n", getpid(), getppid());
	}

	return 0;
}

================ gcc –o fork_exec fork_exec.c
./fork_exec

[gjung@localhost LinuxTasks]$ gcc -o fork_exec fork_exec.c
[gjung@localhost LinuxTasks]$ ./fork_exec
Hi
process id(21770): parent g=2, l=3
I am a new child and my proced id is: 21771 
My parent process id is: 21770 

 The child process now terminates....., g and l values of child process are as follows: g=3, l=4

 ....Who Am I ?? (21771): g=3, l=4

 ....Who is my parent ?? (21770): I am the PARENT process of a new child process 21771 
 and my proced id is: 21770 
Parent process id of the Parent Process is: 21769 

 The parent process now terminates

 ....Who Am I ?? (21770): g=200, l=900

 ....Who is my parent ?? (21769): Child process exits successfully...

I am the parent of fork_exec and my process id is: 21769
 my parentid is: 4642
[gjung@localhost LinuxTasks]$ 
[gjung@localhost LinuxTasks]$