Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 386 Vote(s) - 3.61 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fork and Execlp

#1
I trying a program with fork and execlp where parent address space is replaced with "ls" command.

<!-- language: lang-c -->

#include<stdio.h>
main()
{
int pid,j=10,fd;
pid=fork();
if(pid==0)
{
printf("\nI am the child\n");
execlp("/bin/ls","ls",NULL);
printf("\nStill I am the child\n");

}
else if (pid > 0)
{
printf("\n I am the parent\n");
wait();
}
}

When I execute the program the last line of child

printf("\nStill I am the child\n");

is not printed. Why?
Reply

#2
`exec` family functions do not return when successful.

[To see links please register here]


>The exec family of functions shall replace the current process image with a new process image. The new image shall be constructed from a regular, executable file called the new process image file. There shall be no return from a successful exec, because the calling process image is overlaid by the new process image. <br/>
>
>If one of the exec functions returns to the calling process image, an error has occurred; the return value shall be -1, and errno shall be set to indicate the error.
Reply

#3
`exec` functions will not merely execute your command. They will actually replace the execution context of the process by your selected executable (in your case `/bin/ls`).

In other words, since the `ls` function ends by terminating its process (thorugh 'exit' or returning the main function or whatever), your child process will be killed at the end of the execution of `ls`.

You can actually use this printf call to print some errors, for instance:

if(pid==0)
{
printf("\nI am the child\n");
execlp("/bin/ls","ls",NULL);
printf("\nError: Could not execute function %s\n", "/bin/ls");
_exit(0); //make sure you kill your process, it won't disappear by itself.
}
Reply

#4
after the function execlp() does not get executed as per the documentation of execlp
hence your printf() statement "Still I'm the child" does not get executed ...!!
Reply

#5
The reason is simple : The exec() functions only return if an error has have occurred. For the same refer man pages of exec() functions.

What exactly is happening when exec() functions are called :

execl() does not create a new process - it modifies the
VADS and associated contents - in addition, execution context
is also modified.

- old execution context is no longer used - a new execution context is created.
- a new, fresh context is created for the newly loaded application and
control is passed to the scheduler- scheduler resumes the same child
process with the newly available execution context - using this, a jump
is executed to the entry point of the new application, in user-space -
the new application starts executing in the same child process.
- system stack is overwritten with new hw context for
resuming the main() of the new program in user-space.
- execution context and code/data/heap/stack of old application in the
child process are completely destroyed - no longer available.
- only time execve() or execl() will return to the same application/code
of the current process is when execve() or execl() fails to load
a new application in the current process - meaning, the only time
execv()/execvl() or family of calls will return is when there
is error in completing execv()/execl()/family of calls.

Note: you must validate the return value of exec() family system call APIs for
errors / error codes - based on the error/error codes,
you may terminate the current process or take some other
action.
Reply

#6
1. You are taking process id as `int` type but actually , to store process id you should use `pid_t`
2. When you use exec family function the entire address space of called process replaces the calling process. So,now the last `printf` statement is not there in the new process , actually even the process id of the process is also not changed
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through