Re: What is going on with ash / sh
On Nov 2, 2011, at 1:28 PM, Mark Saad wrote: > Hackers > What is going on here, if I run the following shell script, what is > the expected output . The script is named xxx > > #!/bin/sh > ps -ax | grep -v grep | grep xxx > > Here is what I see > > # sh xxx > 88318 p0 S+ 0:00.00 sh xxx > 88320 p0 R+ 0:00.00 sh xxx > 88321 p0 R+ 0:00.00 sh xxx > > Can someone explain this ? Here's my understanding: * 'sh xxx' starts (process 88318); let's call this the "parent" process. * It reads and parses the command line 'ps -ax | grep -v grep | grep xxx' * The parent process forks a copy of itself for the last 'grep xxx'. * The fork returns to the parent, the child (pid 88320) is scheduled to run later * The parent forks a copy of itself for the 'grep -v grep' * The fork returns to the parent, the child (pid 88321) is scheduled to run later * The parent runs 'ps -ax', which captures three copies of "sh xxx" (the parent which is waiting on 'ps -ax' to finish, and the two children that have not had a chance to run; note that the two children are both in state 'R' which means they'll run as soon as they get a chance, while the parent process is 'S'leeping waiting for 'ps -ax' to finish) * The two children (which started as copies of 'sh xxx') finally get a chance to run and convert themselves into the respective grep commands (via the exec() system call). The "expected output" is anywhere from one to three copies of 'sh xxx' and maybe a copy of 'grep xxx', depending on what shell you're using, how the shell parses the command line, the order in which it spawns children, and whether the fork returns to the parent or child first. The number of processors can also impact exactly when the child 'sh xxx' processes get a chance to call exec(2). Tim ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"
Re: What is going on with ash / sh
On Wed, Nov 2, 2011 at 4:37 PM, Garrett Cooper wrote: > On Wed, Nov 2, 2011 at 1:28 PM, Mark Saad wrote: >> Hackers >> What is going on here, if I run the following shell script, what is >> the expected output . The script is named xxx >> >> #!/bin/sh >> ps -ax | grep -v grep | grep xxx >> >> Here is what I see >> >> >> # sh xxx >> 88318 p0 S+ 0:00.00 sh xxx >> 88320 p0 R+ 0:00.00 sh xxx >> 88321 p0 R+ 0:00.00 sh xxx > > No idea. jobs isn't a shell builtin so you can't verify what jobs > are currently running via the shell if there are any. What version of > FreeBSD are you using and what do your $ENV and .profile files look > like? > Thanks, > -Garrett > I ran this test on 7.3-RELEASE and 8.2-RELEASE and I do not have anything weird in my .profiles or env. This is on a fresh install of each os. -- mark saad | nones...@longcount.org ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"
Re: What is going on with ash / sh
On Wed, Nov 2, 2011 at 4:35 PM, Doug Barton wrote: > On 11/02/2011 13:28, Mark Saad wrote: >> Hackers >> What is going on here, if I run the following shell script, what is >> the expected output . The script is named xxx >> >> #!/bin/sh >> ps -ax | grep -v grep | grep xxx >> >> Here is what I see >> >> >> # sh xxx >> 88318 p0 S+ 0:00.00 sh xxx >> 88320 p0 R+ 0:00.00 sh xxx >> 88321 p0 R+ 0:00.00 sh xxx >> >> >> Can someone explain this ? > > I only see one. What happens if you run this on the command line? > > if you run it from the command line you only see on process. > > -- > > "We could put the whole Internet into a book." > "Too practical." > > Breadth of IT experience, and depth of knowledge in the DNS. > Yours for the right price. :) http://SupersetSolutions.com/ > > -- mark saad | nones...@longcount.org ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"
Re: What is going on with ash / sh
In the last episode (Nov 02), Dan Nelson said: > In the last episode (Nov 02), Mark Saad said: > > Hackers > > What is going on here, if I run the following shell script, what is > > the expected output . The script is named xxx > > > > #!/bin/sh > > ps -ax | grep -v grep | grep xxx > > > > Here is what I see > > > > # sh xxx > > 88318 p0 S+ 0:00.00 sh xxx > > 88320 p0 R+ 0:00.00 sh xxx > > 88321 p0 R+ 0:00.00 sh xxx > > > > Can someone explain this ? > > What does your script do? If it contains subshells or pipelines, the main > process will fork child processes to handle those. Sorry; I misread your original post. Yes, that script forks off two subshells to handle the pipeline, and the ps command caught the state where the subshells had been created but had not yet exec'ed their grep commands. -- Dan Nelson dnel...@allantgroup.com ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"
Re: What is going on with ash / sh
In the last episode (Nov 02), Mark Saad said: > Hackers > What is going on here, if I run the following shell script, what is > the expected output . The script is named xxx > > #!/bin/sh > ps -ax | grep -v grep | grep xxx > > Here is what I see > > # sh xxx > 88318 p0 S+ 0:00.00 sh xxx > 88320 p0 R+ 0:00.00 sh xxx > 88321 p0 R+ 0:00.00 sh xxx > > Can someone explain this ? What does your script do? If it contains subshells or pipelines, the main process will fork child processes to handle those. -- Dan Nelson dnel...@allantgroup.com ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"
Re: What is going on with ash / sh
On Wed, Nov 2, 2011 at 1:37 PM, Garrett Cooper wrote: > On Wed, Nov 2, 2011 at 1:28 PM, Mark Saad wrote: >> Hackers >> What is going on here, if I run the following shell script, what is >> the expected output . The script is named xxx >> >> #!/bin/sh >> ps -ax | grep -v grep | grep xxx >> >> Here is what I see >> >> >> # sh xxx >> 88318 p0 S+ 0:00.00 sh xxx >> 88320 p0 R+ 0:00.00 sh xxx >> 88321 p0 R+ 0:00.00 sh xxx > > No idea. jobs isn't a shell builtin so you can't verify what jobs > are currently running via the shell if there are any. What version of > FreeBSD are you using and what do your $ENV and .profile files look > like? Also, psauxww | grep -v grep | grep xxx would help. I would wait for a reply from Jilles, because it might be creating a subshell for each instance. -Garrett ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"
Re: What is going on with ash / sh
On Wed, Nov 2, 2011 at 1:28 PM, Mark Saad wrote: > Hackers > What is going on here, if I run the following shell script, what is > the expected output . The script is named xxx > > #!/bin/sh > ps -ax | grep -v grep | grep xxx > > Here is what I see > > > # sh xxx > 88318 p0 S+ 0:00.00 sh xxx > 88320 p0 R+ 0:00.00 sh xxx > 88321 p0 R+ 0:00.00 sh xxx No idea. jobs isn't a shell builtin so you can't verify what jobs are currently running via the shell if there are any. What version of FreeBSD are you using and what do your $ENV and .profile files look like? Thanks, -Garrett ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"
Re: What is going on with ash / sh
On Wed, Nov 2, 2011 at 4:28 PM, Mark Saad wrote: > Hackers > What is going on here, if I run the following shell script, what is > the expected output . The script is named xxx > > #!/bin/sh > ps -ax | grep -v grep | grep xxx > > Here is what I see > > > # sh xxx > 88318 p0 S+ 0:00.00 sh xxx > 88320 p0 R+ 0:00.00 sh xxx > 88321 p0 R+ 0:00.00 sh xxx > > > Can someone explain this ? ps is happening to run after sh has forked off the two grep processes but before they exec'ed grep? ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"
Re: What is going on with ash / sh
On 11/02/2011 13:28, Mark Saad wrote: > Hackers > What is going on here, if I run the following shell script, what is > the expected output . The script is named xxx > > #!/bin/sh > ps -ax | grep -v grep | grep xxx > > Here is what I see > > > # sh xxx > 88318 p0 S+ 0:00.00 sh xxx > 88320 p0 R+ 0:00.00 sh xxx > 88321 p0 R+ 0:00.00 sh xxx > > > Can someone explain this ? I only see one. What happens if you run this on the command line? -- "We could put the whole Internet into a book." "Too practical." Breadth of IT experience, and depth of knowledge in the DNS. Yours for the right price. :) http://SupersetSolutions.com/ ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"
What is going on with ash / sh
Hackers What is going on here, if I run the following shell script, what is the expected output . The script is named xxx #!/bin/sh ps -ax | grep -v grep | grep xxx Here is what I see # sh xxx 88318 p0 S+ 0:00.00 sh xxx 88320 p0 R+ 0:00.00 sh xxx 88321 p0 R+ 0:00.00 sh xxx Can someone explain this ? -- mark saad | nones...@longcount.org ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to "freebsd-hackers-unsubscr...@freebsd.org"