The idea of fork() is to clone the original process code/data to a new one. At the end of each "fork()" call, you get two different processes running up to that exact point in their own execution flow. Unless you want to try something like POSIX threads (see man 3 pthread_create()), this is how fork() works.
Suppose your original process is A. After calling, fork() returns the PID of newly cloned process B to your original one (A). It returns 0 to B, and this new process immediately begins to perform something different. That is why it is so-called fork as the execution path of two processes diverses.
Code:
#include <stdio.h>
main(){
....
if(fork()==0){
/* The newly created process will run into here */
FILE *src, *dest;
char buffer[1024];
int count;
if((src=fopen("source_file.txt", "r"))==NULL){
/* Failed to open source file */
exit(-1);
}
if((dest=fopen("destination_file.txt", "r"))==NULL){
/* Failed to open destination file */
exit(-2);
}
while((count=fread(buffer, 1, 1024, src))>0)
fwrite(buffer, 1, count, dest);
fclose(src);
fclose(dest);
exit(0);
}
else {
/* Original process will run into here */
printf("Begining file copy...\n");
/* Do your thing */
....
}
}
Alternatively you can use POSIX thread. A new thread will start from a function that you specify and terminate when it exits the function. You can use global or static variables for IPC as they share the same process data space.