Here, this seams to work:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
pid_t NewProcess(char *file, char **args)
{
pid_t ForkPid;
ForkPid=fork();
if (ForkPid==0)
{
fprintf(stderr, "Ya baby!\n");
//execv(file, args);
system("cat test.cpp > test.txt");
fprintf(stderr, "Done!\n");
exit(0);
}
else if (ForkPid<0)
return -1;
else
return ForkPid;
return ForkPid;
}
int main(int argc, char **argv)
{
pid_t Pid;
if (argc<2)
{
fprintf(stderr, "I need at least one argument!\n");
return 1;
}
fprintf(stderr, "Starting New Process: %s\n", argv[1]);
Pid=NewProcess(argv[1], &argv[2]);
fprintf(stderr, "Process Created: %d!\n", Pid);
return 0;
}
The output is as follows:
Code:
[nerd@The_Nerd programming]$ g++ -O3 -o test test.cpp
[nerd@The_Nerd programming]$ ./test dsf
Starting New Process: dsf
Process Created: 1885!
[nerd@The_Nerd programming]$ Ya baby!
Done!
[nerd@The_Nerd programming]$
The line:
[nerd@The_Nerd programming]$ ./test dsf
Is where I start the program, the dsf is because origionally in my testing I was using the first arg as a program name, however this changed and I didn't take out the if statement to test for the arg, so I just typed some random junk so the program would run.
Output:
Starting New Process: dsf <---- This is where it is starting a new proccess
Process Created: 1885! <---- Finished (this is the same as printf("he"); )
<---- Program terminates
[nerd@The_Nerd programming]$ Ya baby! <----- This is our new proccess (cat test.cpp > test.txt)
Done! <---- Proccess tirminates
Hope that helps!