...making Linux just a little more fun!

### Endless children

Cenk Yusuf Ustabas [cenk_ustabas at yahoo.com]

Tue, 6 Mar 2007 10:02:17 -0800 (PST)

[[[ This thread was originally entitled "Help please " Just a reminder that it would be helpful for people to put descriptive subject lines in their queries to TAG. -- Kat ]]]

Hi, I am new at operations on processes.I wanna create child from parent and after ending the process of child, child will create another child (like grandchild),and this child will create another child it will goes like that but ? dont know how will ? do this.Will it be in a loop or it will be recursive?Thank you for your help.

Top Back

clarjon1 [clarjon1 at gmail.com]

Tue, 6 Mar 2007 13:14:05 -0500

First things first: Please, don't do HTML emails. I know that's a nice font, but some people will only see this type of thing when they open your mail:

[Warning, I'm about to give a copy of his message, beware!]

    --===============0215995091==
    Content-Type: multipart/alternative; boundary="0-18625040-1173204137=:39761"

    --0-18625040-1173204137=:39761
    Content-Type: text/plain; charset=utf-8
    Content-Transfer-Encoding: quoted-printable

    Hi,=0AI am new at operations on processes.I wanna create child from parent =
    and after ending  the process of child, child will create another child (li=
    ke grandchild),and this child will create another child it will goes like t=
    hat but =C4=B1 dont know how will =C4=B1 do this.Will it be in a loop or it=
     will be recursive?Thank you for your help.=0A =0A=0A=0A=0A=0A =0A_________=
    ___________________________________________________________________________=
    =0A8:00? 8:25? 8:40? Find a flick in no time =0Awith the Yahoo! Search movi=
    e showtime shortcut.=0Ahttp://tools.search.yahoo.com/shortcuts/#news
    --0-18625040-1173204137=:39761
    Content-Type: text/html; charset=utf-8
    Content-Transfer-Encoding: quoted-printable

    <html><head><style type=3D"text/css"><!-- DIV {margin:0px;} --></style></he=
    ad><body><div style=3D"font-family:times new roman,new york,times,serif;fon=
    t-size:14pt">Hi,<br>=0AI am new at operations on processes.I wanna create c=
    hild from parent=0Aand after ending&nbsp; the process of child, child will =
    create another=0Achild (like grandchild),and this child will create another=
     child it=0Awill goes like that but =C4=B1 dont know how will =C4=B1 do thi=
    s.Will it be in a=0Aloop or it will be recursive?Thank you for your help.<b=
    r><div>&nbsp;</div><br>=0A</div><br>=0A=0A<hr size=3D1>=0A<a href=3D"http:/=
    /us.rd.yahoo.com/evt=3D49678/*http://smallbusiness.yahoo.com/domains/?p=3DB=
    ESTDEAL"> Get your own web address.</a><br> Have a HUGE year through <a hre=
    f=3D"=0Ahttp://us.rd.yahoo.com/evt=3D49678/*http://smallbusiness.yahoo.com/=
    domains/?p=3DBESTDEAL">Yahoo! Small Business.</a></body></html>

On 3/6/07, Cenk Yusuf Ustabas <cenk_ustabas at yahoo.com> wrote:

    >
    > Hi,
    >  I am new at operations on processes.I wanna create child from parent and
    > after ending  the process of child, child will create another child (like
    > grandchild),and this child will create another child it will goes like that
    > but ? dont know how will ? do this.Will it be in a loop or it will be
    > recursive?Thank you for your help.
    >
    >

You don't normally need to worry about processes, unless you want one of them to 'die', because it is not behaving, or another reason. When a child of a child is created, that's not a grandchild. Just a new child process. I don't know how you will do this, but why don't you ask your teacher? And will it be in a loop, or recursive? As far as I know, the words recursive and loop seem to go together quite well, as they seem to mean pretty much the same thing.

Top Back

Kapil Hari Paranjape [kapil at imsc.res.in]

Tue, 6 Mar 2007 10:15:01 -0800

Hello,

On Tue, 06 Mar 2007, Cenk Yusuf Ustabas wrote:

    > I am new at operations on processes.I wanna create child from parent
    > and after ending the process of child, child will create another
    > child (like grandchild),and this child will create another child it
    > will goes like that but ? dont know how will ? do this.Will it be
    > in a loop or it will be recursive?Thank you for your help.

Does the parent process need to wait for the child process to complete or not?

Regards,

Kapil. --

Top Back

Steve Brown [steve.stevebrown at gmail.com]

Tue, 6 Mar 2007 18:24:29 +0000

Hi All,

I just wanted to share this, it struck me as funny. YMMV.

On 06/03/07, Cenk Yusuf Ustabas <cenk_ustabas at yahoo.com> wrote:

    >
    > Hi,
    >  I am new at operations on processes.I wanna create child from parent and
    > after ending  the process of child, child will create another child (like
    > grandchild),and this child will create another child it will goes like that
    > but ? dont know how will ? do this.Will it be in a loop or it will be
    > recursive?Thank you for your help.
    >

Reading this in Gmail web interface, one of the Google Ads was:

    Potty Training In 3 Days
    Potty Training Secrets That Work Say Good Bye To Diapers Forever

Just made me laugh.

Perhaps I need to go home.

Steve

Top Back

Lew Pitcher [lpitcher at sympatico.ca]

Tue, 06 Mar 2007 15:06:04 -0500

Cenk Yusuf Ustabas wrote:

    > Hi,
    > I am new at operations on processes.
    > I wanna create child from parent and after ending
    > the process of child, child will create another child (like grandchild)
    > and this child will create another child it will goes like that but
    > I dont know how will I do this.
    > Will it be in a loop or it will be recursive? Thank you for your help.

It is unclear to me whether you want to do this in program code or as part of a shell script. Assuming that you want program code, you need to read the documentation on the fork(1) syscall ("man 1 fork"). Your program code will looks something like...

       for (;;)
       {
         /* do some useful work here */
         if (fork()) exit(0);
       }

The fork() call creates the child process The return value from the fork() call will be non-zero in the parent process, and zero in the child process. The child process, detecting that it /is/ the child (because of the zero return value from fork() ) will continue on, loop back to the top of the for(;;) loop, and drop back into the part that does the useful work. The parent process, detecting that it /is/ the parent (because of the non-zero return value from fork() ) will terminate using the exit() call. If the parent process did not terminate, then there would soon be too many processes for the system to handle, and the system would soon start denying the fork() call. (think about it, the parent would keep spawning processes, and each of those processes would spawn processes, and ever onwards).

HTH

    --
    Lew

Top Back

Thomas Adam [thomas.adam22 at gmail.com]

Tue, 6 Mar 2007 20:49:05 +0000

[ Re-added the querent. ]

On Tue, Mar 06, 2007 at 03:06:04PM -0500, Lew Pitcher wrote:

    > It is unclear to me whether you want to do this in program code or as part
    > of a shell script. Assuming that you want program code, you need to read the
    > documentation on the fork(1) syscall ("man 1 fork"). Your program code will
    > looks something like...

Except of course, that's section 2, not section 1.

-- Thomas Adam

    --
    "Wanting to feel; to know what is real.  Living is a lie." -- Purpoise
    Song, by The Monkees.

Top Back

Kapil Hari Paranjape [kapil at imsc.res.in]

Tue, 6 Mar 2007 13:12:03 -0800

Hello,

The original querent responded to my e-mail address rather than to the list so I'm forwarding it here.

On Tue, 06 Mar 2007, Cenk Yusuf Ustabas wrote:

    > first of all I want to say thank you because you answered very
    > quickly.Yes the main parent will wait for all children.?f you want
    > can ? explain all problem?
    >
    > Yusuf Ustabas Yeditepe University Faculty of
    > Engineering&Architecture Computer Engineering

Regards,

Kapil. --

Top Back

Cenk Yusuf Ustabas [cenk_ustabas at yahoo.com]

Tue, 6 Mar 2007 13:13:26 -0800 (PST)

No,I don't want to code.If you gice the code I can't learn I just want to know how to do that as an algorithm.thank you for you help.

Top Back

Thomas Adam [thomas.adam22 at gmail.com]

Tue, 6 Mar 2007 21:16:52 +0000

On Tue, Mar 06, 2007 at 01:13:26PM -0800, Cenk Yusuf Ustabas wrote:

    > No,I don't want to code.If you gice the code I can't learn I just want
    > to know how to do that as an algorithm.thank you for you help.

An algorithm in what sense, and for what? To be used where?

-- Thomas Adam

    --
    "Wanting to feel; to know what is real.  Living is a lie." -- Purpoise
    Song, by The Monkees.

Top Back

Cenk Yusuf Ustabas [cenk_ustabas at yahoo.com]

Tue, 6 Mar 2007 13:49:23 -0800 (PST)

First of all I can't find a link about fork(1).I will write a program which is like that:

Parent produce some numbers and it will create child and this child read these numbers and this child will create another child and this child will read these numbers it will continue like that until loop will end but last child doesnt create any child.Then at the end all these process will end.I guess this is clear now.I know how to create a child but I don't know how this child create another child... it will goes like that.I'm thinking something for example: after pid=fork(); for one child we can do our process in separate parts child and parent then both of them will end by one by.But How will I create other child?

    case -1:
        error no child
    case 0: (this is child part)

do I create other child in this part?

    default: (parent)

Thank you.

Top Back

Thomas Adam [thomas.adam22 at gmail.com]

Tue, 6 Mar 2007 21:54:48 +0000

On Tue, Mar 06, 2007 at 01:49:23PM -0800, Cenk Yusuf Ustabas wrote:

    > First of all I can't find a link about fork(1).I will write a program
    > which is like that:

I see. You want us to do your homework. This isn't something we do.

    > Parent produce some numbers and it will create child and this child
    > read these numbers and this child will create another child and this
    > child will read these numbers it will continue like that until loop
    > will end but last child doesnt create any child.Then at the end all

If the same sets of numbers are being passed down continually, how will you decide when the loop ends? This is potentially disasterous on a system that will just keep spawning processes.

    > But How will I create other child?

In exactly the same way you did initially.

-- Thomas Adam

    --
    "Wanting to feel; to know what is real.  Living is a lie." -- Purpoise
    Song, by The Monkees.

Top Back

Cenk Yusuf Ustabas [cenk_ustabas at yahoo.com]

Tue, 6 Mar 2007 14:12:58 -0800 (PST)

so which part exactly not understandable?

Top Back

MNZ [mnzaki at gmail.com]

Wed, 7 Mar 2007 16:58:18 +0400

On Tue, Mar 06, 2007 at 09:54:48PM +0000, Thomas Adam wrote:

    > On Tue, Mar 06, 2007 at 01:49:23PM -0800, Cenk Yusuf Ustabas wrote:
    > > First of all I can't find a link about fork(1).I will write a program
    > > which is like that:
    >
    > I see.  You want us to do your homework.  This isn't something we do.
    >
    > > Parent produce some numbers and it will create child and this child
    > > read these numbers and this child will create another child and this
    > > child will read these numbers it will continue like that until loop
    > > will end but last child doesnt create any child.Then at the end all
    >
    > If the same sets of numbers are being passed down continually, how will
    > you decide when the loop ends?  This is potentially disasterous on a
    > system that will just keep spawning processes.

On the contrary, this is quite useful to crash a system. Long Long ago, when the earth was young and so was I, I did this on visual basic. Loop forever creating children that loop forever creating children that loop forever creating children.... It was fun watching the screen fill up.

    > > But How will I create other child?

by child, do you mean a copy of the same parent, iw run the parent again as a child? if so then maybe you can pass on the "number" to the child as an argument, and it will just run a copy of itself with a smaller (or bigger ) number as an argument. Is that what you meant?

    --
    //MNZ\\  "We all need mirrors to remind ourselves who we are"
    							-- Leonard Shelby

Top Back

Thomas Adam [thomas.adam22 at gmail.com]

Wed, 7 Mar 2007 09:09:10 +0000

On Wed, 7 Mar 2007 16:58:18 +0400 MNZ <mnzaki at gmail.com> wrote:

    > On the contrary, this is quite useful to crash a system. Long Long

That doesn't mean to say you should do it. There's plenty of other ways to "crash" a system.

-- Thomas Adam

Top Back

MNZ [mnzaki at gmail.com]

Wed, 7 Mar 2007 17:14:12 +0400

On Wed, Mar 07, 2007 at 09:09:10AM +0000, Thomas Adam wrote:

    > On Wed, 7 Mar 2007 16:58:18 +0400
    > MNZ <mnzaki at gmail.com> wrote:
    >
    > > On the contrary, this is quite useful to crash a system. Long Long
    >
    > That doesn't mean to say you should do it.  There's plenty of other
    > ways to "crash" a system.

Ummmm, yeah, I guess you are right. Anyway that's exactly what will happen if that's how he wants to design his program. right?

Is there something that stops such a thing from happening on linux?

    --
    //MNZ\\  "We all need mirrors to remind ourselves who we are"
    							-- Leonard Shelby

Top Back

Thomas Adam [thomas.adam22 at gmail.com]

Wed, 7 Mar 2007 09:33:32 +0000

On Wed, 7 Mar 2007 17:14:12 +0400 MNZ <mnzaki at gmail.com> wrote:

    > On Wed, Mar 07, 2007 at 09:09:10AM +0000, Thomas Adam wrote:
    > > On Wed, 7 Mar 2007 16:58:18 +0400
    > > MNZ <mnzaki at gmail.com> wrote:
    > >
    > > > On the contrary, this is quite useful to crash a system. Long Long
    > >
    > > That doesn't mean to say you should do it.  There's plenty of other
    > > ways to "crash" a system.
    >
    > Ummmm, yeah, I guess you are right. Anyway that's exactly what will
    > happen if that's how he wants to design his program. right?

That depends. Under Linux, there's a kernel facet known as the OOM-killer (Out Of Memory killer) which will attempt to stop processes from becoming all-consuming in their operations. Whilst the amount of memory each child takes up is likely to be small, cumulatively it's going to be quite large if a parent continually starts to fork various processes off. The kernel notices this fact and will try and kill a process of.

But it's not as as simple as that. Due to several complex reasons, the OOM-killer is rarely able (or unlikely) to kill off the process or processes which are becoming a problem. Hence it's likely that the OOM-killer will kill anything off. This is again potentially a nuisance as you can imagine, since it can lead to a number of processes being orphaned to init, often in a D or Z state such that the process may never recover [1].

You can mitigate this in general though. The builtin command for most shells: ulimit has an option of trying to contain any process from consuming too much memory:

    ulimit -v 90000

Although that's not process specific of course -- ulimit was never designed to be so. On system which use PAM (Pluggable Authentication Manager) one can use /etc/security/limits.conf to set per-process limits such as memory. This is generally better where a known application cough*Firefox*cough just eats any memory it can.

I'll let you decide which of the two methods is likely to be more applicable to this so-called forking situation. And it is only theoretical. Have you never seen what the following is doing:

    ){ :|:& };:

(This is specific to bourne shells). This creates a function called ':' which then calls itself recursively, all the time forking a new instance. Can you see now, why in the situation with this proposed parent->child forking many processes why this is a bad idea? It's the same thing as the above example -- potentially bad.

Again, ulimit can help in this instance. It's not just memory limitations which can mitigate the above. Ulimit can also limit the number of processes a user can run, but even this is still no guarantee of control.

I have no doubt that it's simply the querent's description of what he's trying to do -- and even if it's a mis-communication on his part, it's still useful that he should have mentioned it.

    > Is there something that stops such a thing from happening on linux?

See above.

-- Thomas Adam

[1] If it is in a zombie state, there's always a chance it will get reaped, but this can take anything from two to thirty minutes, without any guarantees at all.

Top Back

MNZ [mnzaki at gmail.com]

Wed, 7 Mar 2007 17:56:47 +0400

On Wed, Mar 07, 2007 at 09:33:32AM +0000, Thomas Adam wrote:

    > That depends.  Under Linux, there's a kernel facet known as the
    > OOM-killer (Out Of Memory killer) which will attempt to stop processes
    > from becoming all-consuming in their operations.  Whilst the amount of
    > memory each child takes up is likely to be small, cumulatively it's
    > going to be quite large if a parent continually starts to fork various
    > processes off.  The kernel notices this fact and will try and kill a
    > process of.
    >
    > But it's not as as simple as that.  Due to several complex reasons, the
    > OOM-killer is rarely able (or unlikely) to kill off the process or
    > processes which are becoming a problem.  Hence it's likely that the
    > OOM-killer will kill anything off.  This is again potentially a
    > nuisance as you can imagine, since it can lead to a number of processes
    > being orphaned to init, often in a D or Z state such that the process
    > may never recover [1].

Google sayeth:

    It is the job of the linux 'oom killer' to sacrifice one or more processes in
    order to free up memory for the system when all else fails. It will also kill
    any process sharing the same mm_struct as the selected process, for obvious
    reasons. Any particular process leader may be immunized against the oom killer
    if the value of it's /proc/<pid>/oomadj is set to the constant OOM_DISABLE
    (currently defined as -17).

I guess OOM-Killer wasn't made to stop hostile process but rather to stop ones that take up too much memory on "accident". Or there wouldn't be any "immunization" against it

    > (--snip--)
    >
    > ```
    > ){ :|:& };:
    > '''
    >
    > (This is specific to bourne shells).  This creates a function called
    > ':' which then calls itself recursively, all the time forking a new
    > instance.  Can you see now, why in the situation with this proposed
    > parent->child forking many processes why this is a bad idea?  It's the
    > same thing as the above example -- potentially bad.
    >
    > Again, ulimit can help in this instance.  It's not just memory
    > limitations which can mitigate the above.  Ulimit can also limit the
    > number of processes a user can run, but even this is still no guarantee
    > of control.

Quite interesting, thanks.

    > I have no doubt that it's simply the querent's description of what he's
    > trying to do -- and even if it's a mis-communication on his part, it's
    > still useful that he should have mentioned it.

Cenk Yusuf, if you would like to explain the problem in arabic (guessed from your name, but not sure if you speak arabic) I'd be more than happy to translate.

    --
    //MNZ\\  "We all need mirrors to remind ourselves who we are"
    							-- Leonard Shelby

Top Back

Kapil Hari Paranjape [kapil at imsc.res.in]

Wed, 7 Mar 2007 07:18:25 -0800

[[[ I have re-attached this portion to the main thread. Kapil had originally retitled this "Crashing the system" -- Kat ]]]

(Removing original querent from the recipients as this is out-of-context)

On Wed, 07 Mar 2007, MNZ wrote:

    > On the contrary, this is quite useful to crash a system. Long Long ago, when
    > the earth was young and so was I, I did this on visual basic. Loop forever
    > creating children that loop forever creating children that loop forever
    > creating children.... It was fun watching the screen fill up.

There is a different program for crashing the system called "crashme" which works as follows:

    1. Trap all signals
    2. Generate a random sequence of bytes.
    3. Treat that as code by jumping into it.
    4. Loop if necessary.

This was used as proof of concept for Linux in the early days (when it was still being compared to Minix-386). I seem to recall that Linux (1.0) did not crash whereas Minix-386 did.

Regards,

Kapil. --

Top Back

MNZ [mnzaki at gmail.com]

Wed, 7 Mar 2007 19:45:06 +0400

On Wed, Mar 07, 2007 at 07:18:25AM -0800, Kapil Hari Paranjape wrote:

    > > On the contrary, this is quite useful to crash a system. Long Long ago, when
    > > the earth was young and so was I, I did this on visual basic. Loop forever
    > > creating children that loop forever creating children that loop forever
    > > creating children.... It was fun watching the screen fill up.
    >
    > There is a different program for crashing the system called "crashme"
    > which works as follows:
    >
    > 1. Trap all signals
    > 2. Generate a random sequence of bytes.
    > 3. Treat that as code by jumping into it.
    > 4. Loop if necessary.
    >
    > This was used as proof of concept for Linux in the early days (when
    > it was still being compared to Minix-386). I seem to recall that
    > Linux (1.0) did not crash whereas Minix-386 did.
    >
    > Regards,
    >
    > Kapil.

You don't need all that complexity(!) to crash wind0ze. A simple program that merely runs itself is enough. About linux, well sometimes I wonder if it is crashable. The only one time it died on me was long ago, kjournald said something then it just crashed. As to whether or not the simple call-myself program can crash linux, I'm not sure.

    --
    //MNZ\\  "We all need mirrors to remind ourselves who we are"
    							-- Leonard Shelby

Top Back

Ben Okopnik [ben at linuxgazette.net]

Thu, 8 Mar 2007 15:01:58 -0500

On Wed, Mar 07, 2007 at 05:56:47PM +0400, MNZ wrote:

    >
    > Cenk Yusuf, if you would like to explain the problem in arabic (guessed from
    > your name, but not sure if you speak arabic) I'd be more than happy to
    > translate.

The amazing language expansion in TAG goes on...

MNZ: just FYI, this is turning into a (wonderful) tradition in TAG: a couple of months ago, we had a question asked here in Melayu (IIRC), and - lo and behold - Mulyadi Santosa stepped up and translated it. Totally unexpected, and great fun (although the querent never wrote back, it was still a very cool experience.)

    --
    * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *

Top Back