If we had the this issue, we can try the following.
sudo lsof /var/lib/dpkg/lock
From the above command we can know the PID of the process causing dpkg to lock. Then we do
sudo kill -TERM <PID>
Next, we do
sudo dpkg –configure -a
and waited for the result. After that we can do
sudo apt-get update && sudo apt-get upgrade