1 Replies - 393 Views - Last Post: 05 October 2012 - 10:27 PM Rate Topic: -----

#1 miked89  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 04-October 12

Question regarding processes after a fork and execvp() call

Posted 05 October 2012 - 06:18 PM

Hello Friends!

I am working on a shell right now and I have a question about processes and pids.
My shell is supposed to fork and execute a new program and then print out the name of the file and the process id.


if(*t == '&')
{
kidpid = fork();

if (kidpid == -1)
{
perror("Cannot Fork");
exit(1);
}

if (kidpid == 0)
{
//we know we are in the child

execvp(newargv[0], newargv);
pid = getpid();
printf("[" + pid + "]");









My question is regarding which pid is it going to print with this code. Does the execvp change the pid or is it going to be 0 each time since it is only executing this block of code if(kidpid == 0)?

Is This A Good Question/Topic? 0
  • +

Replies To: Question regarding processes after a fork and execvp() call

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1579
  • View blog
  • Posts: 3,007
  • Joined: 30-May 10

Re: Question regarding processes after a fork and execvp() call

Posted 05 October 2012 - 10:27 PM

> My question is regarding which pid is it going to print with this code.
Nothing is going to get printed by that code (unless there is an error)

> Does the execvp change the pid
No, it changes the executable image, but keeps the PID.
Any code after an exec() call simply does not execute (unless the exec call itself fails).
So the only thing following an exec() call should be an error message print, and a call to exit().

You need to do this kind of printing in the parent process, where the PID of the child is directly available as the result of the fork call.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1