I'm having a problem with fork running in a function I'm using to create a terminal for a school project.
This function is called to handle the user input when they type in a command:
void handleExternalCommands(char ** arr)
{
pid_t pid;
pid = fork();
printf("pid is ", (long) pid);
if (pid == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
int status = execvp(arr[0], arr);
printf("im in the child process");
if (status == -1)
{
// error handle
}
}
wait();
}
However, no matter what I type in, I can't actually make it inside of the if statement. If I try to find out what's going on with pid by using:
printf(pid);
I get a segmentation fault error?
Thanks for your help in advance, everyone.
UPDATE:
I've changed the code to what it is above. Printing the pid only happens once and it prints nothing: "pid is "