Beginners question about fork

Hi everyone:

I'm developing a dynamic library for notifications, this library is used for a daemon that i've programmed, when something goes wrong the library should send an email to an administrator, but since sending an email is a non-vital process then it can fail (it should work as an asynchronous process) so i'm considering to do that by using the fork function, however the entire process of daemon will be forked and i would be responsible for destroying the child process, so my question is, does exist any way more efficient to do this?

thanks in advance

Sending email using a command like mailx or sendmail requires a fork/exec on the child
side then wait on the parent side. This is exactly what the system() call does.

The alternative is to do what sendmail does in C code. system("/usr/bin/sendmail ... ") being infinitely simpler. IF you're sending 100K mail messages per day you should incorporate sendmail code, otherwise for a few dozen messages per day, consider system().

Start here to see what sendmail does, it is open source.

http://www.sendmail.org/

Thanks a lot, you gave me new ideas

or a very naive solution:

#include <stdio.h>

int main (int argc, char ** argv) {

    FILE * fp = popen("mailx -s popen billy", "w");

    fputs("\n\nHello John!\n\n", fp);

    return 0;
}

(yes it does work)

Forking is actually a fairly efficient way to create a process. Nearly all the memory of the old process can be shared or at least copy-on-write.