# Duplicate this code in C

**URL:** <https://rubytalk.org/t/duplicate-this-code-in-c/55304>\
**Category:** ruby-talk\
**Created:** [9 September 2009 06:35 UTC](https://rubytalk.org/t/duplicate-this-code-in-c/55304 "2009-09-09T06:35:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_B](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@Mike\_B](https://rubytalk.org/u/Mike_B)\
**Post date:** [9 September 2009 06:35 UTC](https://rubytalk.org/t/duplicate-this-code-in-c/55304/1 "2009-09-09T06:35:08Z")

</div>

Is there a way to duplicate this code using C?

[http://pastie.org/610698](http://pastie.org/610698)

The goal is to launch multiple ruby processes from a C program.

Mike B.

---

<div class="post-metadata">

**Author:** ![Kaspar\_Schiess](https://avatars.discourse-cdn.com/v4/letter/k/d78d45/32.png) [@Kaspar\_Schiess](https://rubytalk.org/u/Kaspar_Schiess)\
**Post date:** [9 September 2009 06:45 UTC](https://rubytalk.org/t/duplicate-this-code-in-c/55304/2 "2009-09-09T06:45:22Z")

</div>

barjunk wrote:

> Is there a way to duplicate this code using C?

A very probably yes. Passenger does it. Others (mongrel, etc..) do it. So can you 😉

kaspar

---

<div class="post-metadata">

**Author:** ![Reid\_Thompson1](https://avatars.discourse-cdn.com/v4/letter/r/6de8d8/32.png) [@Reid\_Thompson1](https://rubytalk.org/u/Reid_Thompson1)\
**Post date:** [9 September 2009 13:08 UTC](https://rubytalk.org/t/duplicate-this-code-in-c/55304/3 "2009-09-09T13:08:28Z")

</div>

from [gidforums.com](http://www.gidforums.com/t-11552.html)

mod to meet needs....google for more info...

#include \<stdio.h\>  
#include \<sys/types.h\>  
#include \<sys/wait.h\>  
#include \<unistd.h\>  
#include \<stdlib.h\>  
int main()  
{  
&nbsp;&nbsp;&nbsp;&nbsp;int i;  
&nbsp;&nbsp;&nbsp;&nbsp;pid\_t cpid;  
&nbsp;&nbsp;&nbsp;&nbsp;pid\_t child\_pid;  
&nbsp;&nbsp;&nbsp;&nbsp;cpid = fork();

&nbsp;&nbsp;&nbsp;&nbsp;switch (cpid) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case -1: printf("Fork failed; cpid == -1\n");  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 0: child\_pid = getpid();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (i = 0; i \< 10; i++) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("%d: this is the child, pid = %d\n", i, child\_pid);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sleep(1);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(0);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default: printf("This is the parent: waiting for %d to finish\n", cpid);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;waitpid(cpid, NULL, 0);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("Ttttthat's all, folks\n");  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;return 0;  
}

> **···**
>
> On Wed, 2009-09-09 at 15:35 +0900, barjunk wrote:
> 
> > Is there a way to duplicate this code using C?
> > 
> > [http://pastie.org/610698](http://pastie.org/610698)
> > 
> > The goal is to launch multiple ruby processes from a C program.
> > 
> > Mike B.

---

<div class="post-metadata">

**Author:** ![Mike\_B](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@Mike\_B](https://rubytalk.org/u/Mike_B)\
**Post date:** [9 September 2009 23:05 UTC](https://rubytalk.org/t/duplicate-this-code-in-c/55304/4 "2009-09-09T23:05:09Z")

</div>

Thanks for this. I should have been more specific and say that I was  
trying to write ruby code in C to then launch the forks....so I really  
mean to duplicate the ruby code to be ruby code ...in C....

Kaspar,

Passenger does it, but using ruby. Mongrel doesn't install yet in  
1.9, although I need to still go through the online code...I'm going  
to guess they do it in ruby as well.

Any chance you could point me to actual code that does this? I looked  
at eventmachine too, but I'm not good enough to figure out what I  
need, which is kinda why I'm here.

Again, thanks for any tips.

Mike B.

> **···**
>
> On Sep 9, 5:08 am, Reid Thompson \<reid.thomp...@ateb.com\> wrote:
> 
> > On Wed, 2009-09-09 at 15:35 +0900, barjunk wrote:  
> > \> Is there a way to duplicate this code using C?
> > 
> > \>[http://pastie.org/610698](http://pastie.org/610698)
> > 
> > \> The goal is to launch multiple ruby processes from a C program.
> > 
> > \> Mike B.
> > 
> > fromhttp://www.gidforums.com/t-11552.html
> > 
> > mod to meet needs....google for more info...
> > 
> > #include \<stdio.h\>  
> > #include \<sys/types.h\>  
> > #include \<sys/wait.h\>  
> > #include \<unistd.h\>  
> > #include \<stdlib.h\>  
> > int main()  
> > {  
> > int i;  
> > pid\_t cpid;  
> > pid\_t child\_pid;  
> > cpid = fork();
> > 
> > ```
> > switch \(cpid\) \{
> > case \-1: printf\(&quot;Fork failed; cpid == \-1\\n&quot;\);
> > break;
> > 
> > case 0: child\_pid = getpid\(\);
> > for \(i = 0; i &lt; 10; i\+\+\) \{
> > printf\(&quot;%d: this is the child, pid = %d\\n&quot;, i, child\_pid\);
> > sleep\(1\);
> > \}
> > exit\(0\);
> > 
> > default: printf\(&quot;This is the parent: waiting for %d to finish\\n&quot;, cpid\);
> > waitpid\(cpid, NULL, 0\);
> > printf\(&quot;Ttttthat&#39;s all, folks\\n&quot;\);
> > \}
> > return 0;
> > 
> > ```
> > 
> > }

---

<div class="post-metadata">

**Author:** ![Reid\_Thompson1](https://avatars.discourse-cdn.com/v4/letter/r/6de8d8/32.png) [@Reid\_Thompson1](https://rubytalk.org/u/Reid_Thompson1)\
**Post date:** [10 September 2009 13:32 UTC](https://rubytalk.org/t/duplicate-this-code-in-c/55304/5 "2009-09-10T13:32:59Z")

</div>

perhaps a look at ruby2c...

> **···**
>
> On Thu, 2009-09-10 at 08:05 +0900, Mike Barsalou wrote:
> 
> > On Sep 9, 5:08 am, Reid Thompson \<reid.thomp...@ateb.com\> wrote:  
> > \> On Wed, 2009-09-09 at 15:35 +0900, barjunk wrote:  
> > \> \> Is there a way to duplicate this code using C?  
> > \>  
> > \> \>[http://pastie.org/610698](http://pastie.org/610698)
> 
> > Thanks for this. I should have been more specific and say that I was  
> > trying to write ruby code in C to then launch the forks....so I really  
> > mean to duplicate the ruby code to be ruby code ...in C....
> > 
> > Kaspar,
> > 
> > Passenger does it, but using ruby. Mongrel doesn't install yet in  
> > 1.9, although I need to still go through the online code...I'm going  
> > to guess they do it in ruby as well.
> > 
> > Any chance you could point me to actual code that does this? I looked  
> > at eventmachine too, but I'm not good enough to figure out what I  
> > need, which is kinda why I'm here.
> > 
> > Again, thanks for any tips.
> > 
> > Mike B.

---

<div class="post-metadata">

**Author:** ![Mike\_B](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@Mike\_B](https://rubytalk.org/u/Mike_B)\
**Post date:** [10 September 2009 17:00 UTC](https://rubytalk.org/t/duplicate-this-code-in-c/55304/6 "2009-09-10T17:00:07Z")

</div>

Another good idea. I'm having trouble getting that to work as  
well... There is an upgrade to 1.0.0.7 so I'm going to give that a  
whirl.

Mike B.

> **···**
>
> On Sep 10, 5:32 am, Reid Thompson \<reid.thomp...@ateb.com\> wrote:
> 
> > On Thu, 2009-09-10 at 08:05 +0900, Mike Barsalou wrote:  
> > \> On Sep 9, 5:08 am, Reid Thompson \<reid.thomp...@ateb.com\> wrote:  
> > \> \> On Wed, 2009-09-09 at 15:35 +0900, barjunk wrote:  
> > \> \> \> Is there a way to duplicate this code using C?
> > 
> > \> \> \>[http://pastie.org/610698](http://pastie.org/610698)  
> > \> Thanks for this. I should have been more specific and say that I was  
> > \> trying to write ruby code in C to then launch the forks....so I really  
> > \> mean to duplicate the ruby code to be ruby code ...in C....
> > 
> > \> Kaspar,
> > 
> > \> Passenger does it, but using ruby. Mongrel doesn't install yet in  
> > \> 1.9, although I need to still go through the online code...I'm going  
> > \> to guess they do it in ruby as well.
> > 
> > \> Any chance you could point me to actual code that does this? I looked  
> > \> at eventmachine too, but I'm not good enough to figure out what I  
> > \> need, which is kinda why I'm here.
> > 
> > \> Again, thanks for any tips.
> > 
> > \> Mike B.
> > 
> > perhaps a look at ruby2c...

---

<div class="post-metadata">

**Author:** ![Mike\_B](https://avatars.discourse-cdn.com/v4/letter/m/977dab/32.png) [@Mike\_B](https://rubytalk.org/u/Mike_B)\
**Post date:** [10 September 2009 17:40 UTC](https://rubytalk.org/t/duplicate-this-code-in-c/55304/7 "2009-09-10T17:40:08Z")

</div>

ruby2c still not working, I filed a bug report:

[http://rubyforge.org/tracker/index.php?func=detail&aid=27086&group\_id=443&atid=1766](http://rubyforge.org/tracker/index.php?func=detail&aid=27086&group_id=443&atid=1766)

Mike B.

> **···**
>
> On Sep 10, 8:57 am, Mike Barsalou \<barj...@attglobal.net\> wrote:
> 
> > On Sep 10, 5:32 am, Reid Thompson \<reid.thomp...@ateb.com\> wrote:
> > 
> > \> On Thu, 2009-09-10 at 08:05 +0900, Mike Barsalou wrote:  
> > \> \> On Sep 9, 5:08 am, Reid Thompson \<reid.thomp...@ateb.com\> wrote:  
> > \> \> \> On Wed, 2009-09-09 at 15:35 +0900, barjunk wrote:  
> > \> \> \> \> Is there a way to duplicate this code using C?
> > 
> > \> \> \> \>[http://pastie.org/610698](http://pastie.org/610698)  
> > \> \> Thanks for this. I should have been more specific and say that I was  
> > \> \> trying to write ruby code in C to then launch the forks....so I really  
> > \> \> mean to duplicate the ruby code to be ruby code ...in C....
> > 
> > \> \> Kaspar,
> > 
> > \> \> Passenger does it, but using ruby. Mongrel doesn't install yet in  
> > \> \> 1.9, although I need to still go through the online code...I'm going  
> > \> \> to guess they do it in ruby as well.
> > 
> > \> \> Any chance you could point me to actual code that does this? I looked  
> > \> \> at eventmachine too, but I'm not good enough to figure out what I  
> > \> \> need, which is kinda why I'm here.
> > 
> > \> \> Again, thanks for any tips.
> > 
> > \> \> Mike B.
> > 
> > \> perhaps a look at ruby2c...
> > 
> > Another good idea. I'm having trouble getting that to work as  
> > well... There is an upgrade to 1.0.0.7 so I'm going to give that a  
> > whirl.
> > 
> > Mike B.

---

<div class="post-metadata">

**Author:** ![Kaspar\_Schiess](https://avatars.discourse-cdn.com/v4/letter/k/d78d45/32.png) [@Kaspar\_Schiess](https://rubytalk.org/u/Kaspar_Schiess)\
**Post date:** [15 September 2009 06:48 UTC](https://rubytalk.org/t/duplicate-this-code-in-c/55304/8 "2009-09-15T06:48:42Z")

</div>

Hi Mike,

> > \> Any chance you could point me to actual code that does this? =A0I  
> > looke=
> 
> d
> 
> > \> at eventmachine too, but I'm not good enough to figure out what I  
> > \> need, which is kinda why I'm here.

I think you need to look at ruby as just another executable. There are  
plenty of examples on the net on how to do

&nbsp;&nbsp;if (! fork()) {  
&nbsp;&nbsp;&nbsp;&nbsp;exec(SOME\_PROGRAM);  
&nbsp;&nbsp;}

on the net. Ruby isn't any different - when forked and execed it is  
just a binary.

kaspar

PS: Didn't see your reply on my nttp server ☹

> **···**
>
> --  
> I'm trying a new usenet client for Mac, Nemo OS X, since 0 days.
> 
> You can download it at [http://www.malcom-mac.com/nemo](http://www.malcom-mac.com/nemo)
