Work done by isothermal expansion from two different viewpoints Consider an adiabatic system as follows. It consists of a gas in a container and a piston. 
Initially, the system is at equilibrium and the gas inside it occupies a volume $V_i$ at a pressure $p_i$ which is equal to the outside pressure. Suddenly, the outside pressure changes and reduces to $p_{atm}$. The piston moves to equalize the pressure and the gas expands isothermally to obtain equilibrium. The gas now occupies a volume $V_f$ at a pressure of $p_f$ which is equal to $p_{atm}$
Now, two textbooks I have define the work done from two different viewpoints.
1: From the viewpoints of the surroundings: 
The work done on the system by the surroundings equals $-p_{atm}\Delta V$. Since $p_{atm}$ is pretty much constant for the whole of the whole of the process, we can say that the work done equals:
$$W_1 = -p_{atm}(V_f - V_i)\tag1$$
2: From the viewpoint of the system:
We can write the internal pressure of the system as a function of its volume: $p_{in}(V)$. As during the expansion, the internal pressure changes, the work done by the system equals 
$$W_2 = \int_{V_i}^{V_f}p_{in}(V)dV$$
Now, I don't know which definition to use. The work is done by the system (from definition 2) is done on the surroundings. But what about the negative work that the surroundings did on the system? Where did that energy go? Maybe, the two definitions express the same thing: the work done by the system. The negative sign in definition 1 signifies that the work is done by the system. But that would mean that 
$$W_1 = W_2$$
We can simplify $W_2$ as follows. Clearly, 
$$p_{in}(V) = \frac{p_iV_i}{V}$$
$$W_2 = \int_{V_i}^{V_f}\frac{p_iV_i}{V}dV = p_iV_i \ln{\frac{V_f}{V_i}} \tag2$$
The internal pressure when the volume is equal to $V_f$ is $p_{atm}$
$$\implies p_{atm} = p_{in}(V_f) = \frac{p_iV_i}{V_f}$$
$$\implies V_f = \frac{p_iV_i}{p_{atm}}$$
Putting this into  $(1), (2)$ gives us, 
$$W_2 = p_iV_i\ln{\frac{p_{i}}{p_{atm}}}$$
$$W_1 = -p_{atm}\left(\frac{p_iV_i}{p_{atm}} - V_i\right) = -V_i(p_{atm} - p_i)$$
Consider some values. Let $p_i = 5 \ \rm{Pa}, V_i = 1 \ \rm{m^3}, p_{atm} = 1 \ \rm{Pa}$.
$$W_1 = -1\cdot(1-5) = 4$$
$$W_2 = 1\cdot1\cdot\ln{\frac{5}{1}} = 1.609$$
Where am I making a mistake?
 A: Your own answer is along the right lines: the key is that this is not a quasi-static process, and the system goes out of equilibrium.
However, in practice the piston probably won't move so rapidly that you can't define the pressure at all. Instead, the system will lose energy equal to $W_2$ (to a good approximation), and the environment will gain energy equal to $W_1$. The difference between the two will be found in the piston plug itself, in the form of kinetic energy.
Since the piston has energy it will keep moving, expanding the gas. Then the pressure difference will decelerate the piston until it starts moving the other way, and so on, leading to oscillations. Gradually, it will stop oscillating, due to frictional dissipation (either inside the gas itself, or by friction between the piston and its housing). Since the system is isothermal, the resulting heat will go into the surroundings.
The end result is that, after the piston has stopped oscillating, the piston has done an amount of work $W_1$ on the surroundings, but it has also given off an amount of heat equal to $W_2 - W_1$, and therefore its internal energy has changed by $W_2$.
A: After some thinking, I got the answer. When you decrease the ambient pressure from $p_i$ to $p_{atm}$ , the system goes out of equilibrium, and so the expansion of the gas is not quasi-static. So, all the state variables like pressure and volume aren't defined. So we can't use them for calculating the work done in definition 2.
