To answer your first question, yes attacks were frequent.
This is what prompted many of the earliest 'settlements' to build a surrounding and protective wall in the first place, to stop constant raids from stronger 'warrior' nomadic tribes killing, enslaving people and stealing food.
To answer your second question, the farmers and dry grain/food supplies would have been brought inside the walled city and stockpiled in the event of a lengthy siege or the threat of starvation by blockading the walled city.
Many of the earliest walled settlements that were most resilient to this kind of 'siege' or blockade were deliberately cited close to water, usually a river so that in the event of land blockade, supplies could still be brought in by water.
It needs to be remembered I think, that walled fortresses are much easier to defend by smaller armed forces against much larger attacking forces. In other words, smaller cities or villages that don't possess many 'warrior' class would still be able to defend a city or village against a much stronger and larger attacking force by simply possessing a strong defensive wall.
The author Sam Barone in his Akkad series of books detailing the rise of the worlds first walled cities of Akkad and Sumeria in Mesopotamia (which many historians cite as the worlds first empires) talks about this very subject.
The fact they became empires is very much down to the fact that they were the earliest civilisations to utilise the 'walled' city philosophy as a defence against attack.
I highly recommend Sam Barone's books as not only an entertaining read but a very useful historical one.