'Check whether a lock file exists in the Tribler directory. If not, create the file. If it exists,
check the PID that is written inside the lock file.'

def __init__(self, state_directory=None):

    self.already_running = False
    self.state_directory = (state_directory or TriblerConfig().get_state_dir())
    self.lock_file_path = os.path.join(self.state_directory, LOCK_FILE_NAME)
    if os.path.exists(self.lock_file_path):
        try:
            file_pid = int(self.get_pid_from_lock_file())
        except ValueError:
            self.remove_lock_file()
            self.create_lock_file()
            return
        if (file_pid == os.getpid()):
            self.already_running = False
        elif ((file_pid != os.getpid()) and (not ProcessChecker.is_pid_running(file_pid))):
            self.remove_lock_file()
            self.create_lock_file()
        else:
            self.already_running = True
    else:
        self.create_lock_file()
