LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   need help forking pings (https://www.linuxquestions.org/questions/programming-9/need-help-forking-pings-30124/)

penguinboy 09-11-2002 09:26 AM

need help forking pings
 
i want to make a program that pings a list of ips every 5 mins, and logs the pings

but i need to fork the pings
because if one of the ips goes down it will screw up my log


for example

ping x
ping y
ping z

x time=51.8 ms
y time=did not respond
z time=32.4 ms

well now imagin if i was pinging 50 ips, and 10 of them did not respond, waiting on those 10 would screw up my log unless i could find a way to make it "go around" them but still finish the full ping...

like

ping x
ping y
ping z

x time=51.8 ms
y time=not responding--- forks to ping z
z time=32.4 ms
y time=finishes pinging-did not respond...

i'm sorry if this is confusing.. i'm having a hard time explaining it..

thanks for any help

--bret



no2nt 09-11-2002 10:41 AM

This sorta does it. You'll have to modify it heavily for your use.

Code:

#include <stdio.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>

int main( int argc, char *argv[] )
{
    int a;
    char cmd[256];

    for( a = 1; a < argc; a++ )
    {
        pid_t cpid;
        cpid = fork();

        switch(cpid)
        {
            case -1: // uh-oh
            {
                printf("Unable to claim system resources.\n");
                break;
            }
            case 0: // i am the child proc
            {
                char cmd[256];
               
                sprintf(cmd, "ping -c 1 -w 2 %s", argv[a]);
                system(cmd);
                _exit(0);
                break;
            }
            default: // all your child process are belong to us
            {
                int status;

                //waitpid(cpid, &status, 0);
                break;
            }
        }
    }

    return 0;
}

And in a scriptable (bash) format:

Code:

#!/bin/bash

IPS="172.26.12.1 172.26.26.2"

for ip in $IPS;
do
    ping -w 2 $ip&
done

Enjoy

penguinboy 09-12-2002 08:10 AM

hey thanks alot guy, that helped out quite a bit.


All times are GMT -5. The time now is 10:42 PM.