Chosen Solution
my iMac is heating and the fan does not work so at that time the machine stop
Check out the status on the fan using SMC Fan Control (free) or TG Pro ($18) https://www.macupdate.com/app/mac/23049/… https://www.macupdate.com/app/mac/49154/…
The fan might be clogged with dust/dirt. Try cleaning it out. Also, which fan is not working specifically? (e.g. CPU fan, Optical drive fan, Hard drive fan)
Do a guide on replacing it at: iMac Intel 27" EMC 2309 & 2374