Solved

Multithreading in gcc

Posted on 2009-04-02
6
2,273 Views
Last Modified: 2012-05-06
I need to write a very small app in C that runs on linux. I need to make sure my arp tables are both current and full, so I need to write a little app that will loop through 254 addresses on a network and execute ping [ipaddress] -c 1.

While my other programming languages are strong (.net, php), I am a C noob. I am pretty sure I can figure out the for loop, passing the command line args with argv. How would you multithread this?

Using the GCC compiler on Ubuntu 8.10, but ultimately this will run on an rPath (Fedora like) server.

Thanks in advance!
0
Comment
Question by:DrDamnit
6 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24054324
I would use fork() instead of multi-threading. With Linux the process will be copy-on-write anyway, so it won't use much more memory than threading and is a little easier to write.

Use a parent that forks() off N children and waits for each to exit. You might fork off 10 children and let them process 25 IPs per, or you just iteratively let each child do 1 ping, then exit, which you reap by calling wait/waitpid.

Have you considered using something like nmap or fping instead? They already can do this sort of thing.
0
 
LVL 32

Author Comment

by:DrDamnit
ID: 24054356
Thanks... I'll look up fork. Do you have some sample code that I can go by?

I love nmap, and we were originally going to use it for this process, but licensing prohibits us from doing what we need to do with it.
0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24054611
Warning, this blasts off 255 processes in parallel and pings each ip. The parent does not wait for children to exit, so you will have zombies, which area really not proper, but when your program is going to exit, its not going to keep it from running.

Proper form is to add a signal handler for SIG_CHLD and/or throttle the total number of processes by keeping count. Everytime your receive SIG_CHLD, call wait() then it will reap the child status.

Again, this is just a starting point for you.

You might want to get "Advanced Programming in the UNIX Environment" by W Richard Stevens, its a classic.



#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>
 

void ping(const char *);
 

int main() {

    char ip[256];

    for(int i = 1; i<=255; i++) {

        sprintf(ip, "192.168.2.%d", i);

        ping(ip);

    }

}
 

void ping(const char * ip) {

    int pid;

    if((pid = vfork()) == 0) {

        printf("Ping %s\n", ip);

        execl("/bin/ping", "ping", "-c", "1", ip, 0);

        /* exec failed */

        perror("ping");

        exit(0);

    }

    else {

        return;

    }

}

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 34

Expert Comment

by:Duncan Roe
ID: 24058415
Threads share memory and forked processes don't. In many cases therefore, threads are the only way to go. There are lots of inter-thread communication mechanisms that just aren't there for forked processes. Still, it's valid to consider whether you really need threads for what you want to do.
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 24058487
For multiple i/o as you are doing, there is no need to use threads at all. In the microsoft languages you mentioned, you might well use threads because microsoft doesn't have a select() call that can monitor all file handles - they only have the old-fashioned kind that can monitor all sockets. Linux also has poll() as an alternative - use that or select() accroding to which you find more convenient.
No good if you're using system() though - looking more at mrjoltcola's suggested solution appeals - it's certainly a small app. If you *are* happy to fork, you could write it as a shell script - no need to use C at all. It will briefly consume 255 process slots while a thread program would only consume 1.
0
 

Expert Comment

by:hi_cyberwarrior
ID: 24343730
I too agree on a shell script. It is simpler.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now