How to determine the entropy change in a system with heat and mass transfer? Consider the system as shown in the picture. It consists of two chambers filled with a gas and connected with a tube. One chamber is closed with a piston. Everything is thermally insulated.

We make two assumptions:


*

*The total amount of gas is constant;

*The connecting tube is such that the pressure in both chambers gets equilibrated.


The initial state is such that the pressures are equal, i.e., $p_1=p_2$ and the temperatures are different: $T_1\neq T_2$.
Let the piston move s.t. the volume in the second chamber changes by $\delta V$. So, we have (here the prime $'$ denotes the state after the transition)
$$V_1'=V_1,\, V_2'=V_2+\delta V,\, N_1'=N_1+\delta N,\, N_2'=N_2-\delta N,$$
and $\delta N$ is determined from the condition $p_1=p_2=p$.
So, my question is

How to describe the change in entropy: $S_1'=?, S_2'=?$

Let $S_1'=S_1+\delta S_1, S_2'=S_2+\delta S_2$. We can compute the change in total energy:
$$(U_1'+U_2')-(U_1+U_2)=T_1\delta S_1 +\mu_1\delta N + T_2\delta S_2 -p\delta V -\mu_2\delta N,$$
which is equal to work done on the system, $\Delta W=-p\delta V$. So, we conclude that it must hold that 
$$0=T_1\delta S_1 +\mu_1\delta N + T_2\delta S_2 -\mu_2\delta N.$$
The terms $\delta S_1$ and $\delta S_2$ should satisfy this equality. However, I'm still not clear what would be the exact form of these terms. I tried several options, but couldn't convince myself. 
Any help on this problem will be very much appreciated.

EDIT1: First let me note that this is a simplified version of a more complex system, so it might look somehow artificial at first sight. Let me know if something is missing. I'll try to refine my assumptions to make the problem more tractable.
So, we assume that


*

*the time interval is finite, i.e., we do not consider any slow processes like, e.g., heat transfer through the tube.

*the piston moves sufficiently slowly$^*$ so that pressure equilibrates immediately.

*the section of the tube is sufficiently small so that we can neglect the heat transfer through the tube.

*the only heat transfer source is due to the mass transfer associated with the move of piston. 

*the transferred amount of gas mixes ideally with the contents of the chamber thus resulting in some entropy production.


$^*$ according to Callen it should be at most 0.1 of sound speed.
 A: The total entropy of the two volume $S=S_1+S_2$ . The entropy will not change because it is insulated, $\Delta Q=0$. In the other word, assuming the piston moves really slow, $\Delta S = 0$ or $S_1+S_2 =0$.
We know that volume 1 received gas from volume 2 at different temperature. When it receives gas, its entropy increases. And from the above equation, the entropy of volume 2 will decrease.  

I guess I need to use this equation to do the work.
$$dS = \frac{dU}T+\frac PTdV-\frac{\mu}TdN$$ 
If there is no piston motion, the above equation becomes
$$dS = \frac{dU}T-\frac{\mu}TdN$$ 
Initially, there is no flow between the two volumes because the pressures equal to each other. Thus, $dN=0$. 
The above process has heat transfer through the pipe. Therefore, the internal energy changes. Assuming $T_1>T_2$, then $U_1$ decreases and $U_2$ increases with the same amount. The entropies change accordingly. Because of the temperature difference, the entropy changes are different. (So I was wrong saying that the total entropy is constant. It should be $dQ(-\frac 1 {T_1} + \frac 1 {T_2})$, where dQ is the amount of heat transferred from volume 1 to volume 2.)

Now back to the original question. Let's use the following equation again for volume 1 and volume 2. 
$$dS = \frac{dU}T+\frac PTdV-\frac{\mu}TdN$$ 
For volume 1,
$$dS_1 = \frac{dU_1}{T_1}-\frac{\mu_1}{T_2}dN_1$$
And for volume 2, 
$$dS_2 = \frac{dU_2}{T_2}+\frac {P_2}{T_2}dV_2-\frac{\mu_2}{T_2}dN_2$$ 
So we know that $dN_1=-dN_2$ and $dN_1>0$ because the flow is from volume 2 to volume 1. This will reduce $S1$ and increase $S_2$. 
We also know that $dV_2<0$, which will reduce $S_2$.
On the internal energy change, it is complicated and depends on $T_1$ and $T_2$. Using an iterative method and equation of state, we may get final pressure and temperature. But so far, I would say the problem is undetermined. 
