Is there any historical truth to the dictators of communist regimes of the past killing farmers during famines because of the famines? Or was agriculture during these periods the only "safe" (as in not starving to death) profession?
During the most infamous famines, the great famine of '32 in the Soviet Union and the Great Leap Forward in China, the rural population was hit the hardest: The state took away food from them for export (to pay for industrialisiation) and to feed the cities. One survival strategy that was tried, and worked sometime, was indeed to migrate into a city where rations where higher. Police and military tried to stop this (also to keep the famines a secret from the urban population) and succeeded for the most part, though. This is true for both China and the SU.
The SU suffered several famines, most importantly immediately after WWI, during WWII and again shortly after WWII. In these cases, the city population was hit harder.
Source: Felix Wemheuer: Famine Politics in Maoist China and the Soviet Union (Der große Hunger. Hungersnöte unter Stalin und Mao), the author also did extensive research and interviews with survivors of the famine in China and published several books about this.
To begin with, there was no "farmers" in Russia or Soviet Union until 1990s. And for most of population, agriculture was not a "profession", but social status, "class" as it was called. These people were called peasants. If you are born into a peasant family, you are a peasant, by default. Peasants were a "class", not a profession. In different times it was more difficult or less difficult to move to a city, or to obtain some other social status, by education, for example.
In the period we are talking about, peasant lived on the land which was owned by the state. They possessed plots but these plots could not be bought or sold. They had plots which they developed. Sometimes it was possible to escape to the city (without any of your property) and find work there, and place to live.
Still in the beginning of the Soviet state, most peasants had individual plots, and could sell their product on the market. In the process of collectivization, in the early 1930s, these plots were united to large collective farms. Their production was simply taken by the state. This led to a large and widespread famine.
During the famine, the movement from the country to cities was blocked by the police and troops. Moreover, during the Stalin rule, internal passports were introduced for all except peasants. One could not legally travel or live in a city without a passport. This changed only in 1960 when peasant obtained passports.
Shortly speaking, agriculture was not a profession, and perhaps the most unsafe "occupation". Peasants were just deprived of all means of existence, and for most of them there was no way out. Many of them died of starvation.