Hi I'm working through some exercise from Advanced Programming Unix System. I'm interested in how the fork and execlp function works. From the text the author specifies that fork creates a new process. It is called once - by the parent - but returns twice - in the parent and in the child.
So fork returns a non negative pid to the parent and 0 to the child. I would like to step through this sequence of calls with GDB, however my break points causes the child not to run or interrupt system calls which cause the parent to terminate.
1 - if I set a break point - else if(pid == 0) -> the process does not run.
2 - if I set a break point - execlp(buf, buf, (char *)0);
I the get the following error:
waitpid error: Interrupted system call [Inferior 1 (process 461) exited with code 01]
What options do I have to set in GDB to debug the parent and child? Where should set the breakpoints?
int main(int argc, char *argv[])
{
char buf[MAXLINE];
pid_t pid;
int status;
printf("%% ");
while(fgets(buf, MAXLINE, stdin) != NULL)
{
if(buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = 0;
if((pid = fork()) < 0)
{
err_sys("fork error");
}
else if(pid == 0)
{
execlp(buf, buf, (char *)0);
err_ret("could'nt execute: %s", buf);
exit(127);
}
if((pid = waitpid(pid, &status, 0)) < 0)
err_sys("waitpid error");
printf("%% ");
}
exit(0);
}