Issue with the work of a reversible process 
You consider a cylinder with a piston inside which can move without friction, and with diathermic walls. The piston divides the cylinder into two rooms: A-B, filled with two ideal gases. There is also a valve that connects B and the outside. Let's consider just the room A as our thermodynimic system.

In the figure above are drawn two consecutive equilibria states of a generic reversible process, each described with a vector of thermodynamics variables. We assume that during the disequilibrium state, the piston moves of $dx$. You can observe that the only way to make the piston move is to use the valve; if the valve stays closed you can only have an isochoric process. 
However, now we are intrested in calculating the work that our system (room A) has exchanged during the transition 1->2. In the equilibria states the net force acting on the piston is $0N$ because the pressure in the two rooms is the same, so the piston is firm. Using the kinetic energy theorem between the two equilibra states you obtain:
$$dk=dW=0 $$
While I now that the right expression should be:
$$dW=p dv$$
But I don't understand where it comes from, since it is in contrast with my treatment. And also, which pressure should I consider in this formula? Someone could show me exactly which point of my treatment is wrong?
 A: Here's a mechanical example. Suppose you lift up a weight using a massless string, by a distance $L$. During this process, the tension in the string is $F$. Since the tension inside the string is uniform, the work done on any piece of the string is zero. But this does not imply that you did zero work.
Specifically, you did work $+FL$ on the string, and the weight did an equal and opposite amount of work $-FL$ on the string. The net work done on the string is zero, while the work done by you, and the weight, are both individually nonzero. 
In the thermodynamic case, $dW$ refers to the work done by room $A$, not the net work done on the piston. (For the math, just replace $F$ with $p$ and $L$ with $dV$.) In the ideal, quasistatic case, the work done on the piston is always zero, as you showed, so this is not a very interesting quantity.

Here's a bit more information on the quasistatic vs. non-quasistatic case. In the quasistatic case, the gases in chambers $A$ and $B$ have well-defined pressures $p_A$ and $p_B$ throughout, where $p_A$ and $p_B$ satisfy the equation of state. By the work-kinetic energy theorem, we have
$$dW_A = p_A dV_A, \quad dW_B = p_B dV_B.$$
If we further assume that the piston moves slowly and on frictionless rails, then $p_A = p_B$, and the net work done on the piston is zero.
Now consider the general non-quasistatic case. During this process, the gas in chamber $A$ does not have a well-defined pressure throughout. However, there is a well-defined local pressure $p_A^\text{p}$ right at the piston, and
$$dW_A = p_A^\text{p} dV_A, \quad dW_B = p_B^{\text{p}} dV_B.$$
For example, if I pull the piston quickly to the right, before the gas in chamber $A$ can react, then $p_A^{\text{p}} \approx 0$. I also form a shock wave of gas in chamber $B$ ahead of the piston, so that $p_B^{\text{p}}$ is very large. The resulting energy balance equation is
$$\int p_A^\text{p} dV_A + p_B^{\text{p}} dV_B + dW_{\text{me}} = 0$$
where the final term is the work I did. In principle, this tells you something about $p_A^\text{p}$ and $p_B^\text{p}$, but calculating them is a total nightmare. 
A: In general, the 1st law would say that $dU=\delta Q +\delta L$ but you have postulated a purely mechanical energy exchange, that is one with $dU=\delta L$. This is called an adiabatic process. 
If you further assume that the gas is internally frictionless, then the partition will oscillate back and forth just as an ideal spring would do. 
If the gas has internal friction, then the oscillation of the partition will be dissipated and the partition will stop at a point where the pressures will be equal, i.e, $\delta p = 0$. Read the subject called "the adiabatic piston".
