0

I was reading about processes and I came across this:

Usually, the child process then executes execve or a similar system call to change its memory image

what I can derive from this is this pseudocode:

if(child_created_sucessfully)
{
    do_ABC_and_ignore_the_part_of_the_parent's_control_flow   //is this what it meant to "change its memory image"?
}

(Question asked in the pseudocode's comment)

I completely don't understand this other part:

example, when a user types a command, say, sort, to the shell, the shell forks off a child process and the child executes sort. The reason for this twostep process is to allow the child to manipulate its file descriptors after the fork but before the execve in order to accomplish redirection of standard input, standard output, and standard error.

Ivan Kaloyanov
  • 1,748
  • 6
  • 18
  • 24
infinite
  • 319
  • 1
  • 10

1 Answers1

2

Regarding the first part

Usually, the child process then executes execve or a similar system call to change its memory image

This simply means that when you create a child process it initializes it's own stack and heap memory although this is not 100% true. Since the new process is forked at time T at the time T + 1 when the process starts to run it is pretty much identical when it comes to the data in memory so there is a smart optimization called 'copy on write' more here.

Regarding the second part

example, when a user types a command, say, sort, to the shell, the shell forks off a child process and the child executes sort. The reason for this twostep process is to allow the child to manipulate its file descriptors after the fork but before the execve in order to accomplish redirection of standard input, standard output, and standard error.

Simply put this means that when you execute a shell command (like ls, ps, grep, nstat...) the OS forks the current process which executes the command and the command itself is executed by this new process. An easy way to understand this is by using ps | grep ps this will first fork and create a new process, then this part comes to play

this twostep process is to allow the child to manipulate its file descriptors after the fork but before the execve

and the standard output of the process is changed. After the new ps process executes the ps it will then fork and create one more process for the grep ps which will execute the grep and you should be able to see the ps process which created this grep process.

Ivan Kaloyanov
  • 1,748
  • 6
  • 18
  • 24
  • `the OS forks the current process which executes the command` - the current process is the shell? – infinite Dec 29 '19 at 19:02
  • 1
    @infinite yes. I suggest you the dinosaur book about the OS or regarding this topic here is a well-detailed explanation https://medium.com/meatandmachines/what-really-happens-when-you-type-ls-l-in-the-shell-a8914950fd73 – Ivan Kaloyanov Dec 29 '19 at 19:45