<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def guard(pidfile, guardfile=None):
 """ Raise an EnvironmentError when the "guardfile" doesn't exist, or the process with the ID found in "pidfile" is still active. """

    # Check guard
    if guardfile and not os.path.exists(guardfile):
        raise EnvironmentError("Guard file '%s' not found, won't start!" % guardfile)

    if os.path.exists(pidfile):
        running, pid = check_process(pidfile)
        if running:
            raise EnvironmentError("Daemon process #%d still running, won't start!" % pid)
        else:
            logging.getLogger("daemonize").info("Process #%d disappeared, continuing..." % pid)

    # Keep race condition window small, by immediately writing launcher process ID
    _write_pidfile(pidfile)