Duplicate this code in C

K

Kaspar Schiess

barjunk said:
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
 
R

Reid Thompson

Is there a way to duplicate this code using C?
=20
http://pastie.org/610698
=20
The goal is to launch multiple ruby processes from a C program.
=20
Mike B.
=20

from 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()
{
int i;
pid_t cpid;
pid_t child_pid;
cpid =3D fork();

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

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

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

Mike Barsalou

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("Fork failed; cpid == -1\n");
                 break;

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

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

}

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.
 
R

Reid Thompson

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....
=20
Kaspar,
=20
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.
=20
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.
=20
Again, thanks for any tips.
=20
Mike B.
=20

perhaps a look at ruby2c...
 
M

Mike Barsalou

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.
 
K

Kaspar Schiess

Hi Mike,

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

if (! fork()) {
exec(SOME_PROGRAM);
}

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 :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top