Take the 2-minute tour ×
int status=0;
int PID = fork();
if(PID == 0)
   char *path = strcat(pathToken,strcat("/",command));
   printf("path: %s\n",path);
else if(PID>0)
   printf("pid: %d. ",PID);
   printf("I'm parent process\n");


pid: 20027. I'm parent process

Why it doesn't enter if(PID==0)?

share|improve this question
Most probably the child starts, but crashes immediately when trying to execute strcat("/",command). –  alk Nov 5 '12 at 18:27

3 Answers 3

up vote 5 down vote accepted

You are trying to modify a string literal in:


which leads to undefined behavior.

The declaration for strcat is:


And it appends a copy of the source string to the destination string.

share|improve this answer
And this would have been caught by the compiler, if you passed it the -Wall flag, you'll have a warning.... –  Basile Starynkevitch Nov 5 '12 at 18:31

Q: Why doesn't it enter "if pid == 0")?

A: It probably does :)

Two suggestions:

1) step through under the debugger and see what "path" is before you call "execl()". I think you'll be surprised.

2) Be sure to check the error status of "execl()" in your code. Never assume that it won't fail, and be sure to print a meaningful error message (and/or recover gracefully) if it does.

share|improve this answer

Try changing this line



char * path = malloc(strlen(pathtoken) +
                     strlen(command) + 2); /* 1 for null + one for / */
strcpy(path, pathToken);
strcat(path, "/");
strcat(path, command);

Me thinks that should fix the problem.

share|improve this answer
Thank you. I overlooked strcat, thought ill figure it out later, as long as im inside "if(pid==0)". I thought it should give error at least –  Nazerke Nov 5 '12 at 18:56
@nzrksafina - An aside - treat writing a computer program as baking a cake. Take once step at a time. Trying to do two things at once you either have a burnt cake or the cooker explodes! –  Ed Heal Nov 5 '12 at 18:59

Your Answer


