Linear pencils on graphs and on real curves