Suppose a container is fitted with a massless and frictionless piston lying on a table such that pressure due to gas is greater than pressure due to external atmosphere. Hence piston will move.
Now the molecular motion of gaseous molecules inside container exert forces that will do "work" on the piston and correspondingly by newton's third law it will be equal to the negative of the "work" done by piston on the gas.
Similarly molecular motion due to atmospheric molecules will exert forces on piston that will do "work" on the piston correspondingly by newton's third law it will be equal to the negative of the "work" done by piston on the atmosphere.
My question is that most textbook says that they calculate "work" done by external pressure( i.e. external force) due to atmosphere on the gas but actually the forces due to external atmosphere are not in direct contact with gas since our system (which includes gas only) has only forces exerted by the piston, so how can they calculate "work" done by those forces which do not directly act on the system?