Message-optimal algorithm for detection and resolution of generalized deadlocks in distributed systems