(a) The presiding judge of any circuit court or district court having reason to believe that the testimony of any convict serving a sentence in the penitentiary or to hard labor for the county is necessary in any criminal prosecution for the state and that other evidence cannot be obtained on behalf of the state may order a writ to be issued by the clerk, commanding the Board of Corrections to have the convict before the court on a specified day to give testimony in the particular case for the state. Moreover, upon the sworn petition of the defendant in a criminal prosecution showing that a convict serving sentence in the penitentiary knows facts which would be beneficial to him, the judge may, if he believes the ends of justice will be served thereby, order the issuance of such a writ to secure the appearance of the convict to testify on behalf of the defendant. The writ shall be served on the board at least one week before the day appointed to have the witness in court; and the board must have the convict before the court on the specified day in accordance with the mandate of the writ, employing a trustworthy deputy, with a sufficient guard, to convey such convict to the court. After he has testified, the convict must be returned forthwith to the place from which he was brought.
(b) For conveying any convict under the provisions of subsection (a) of this section, the board is entitled to the actual expenses incurred in such removal, including the hire of necessary guards and their expenses; the guards and the convict are subject to the same liabilities and penalties for an escape or attempt to escape; and the sheriff or jailer of the county must, on demand of the officer having charge of the convict, receive and safely keep such convict in the county jail during his attendance on the court or while delayed in passing through the county and is entitled to the usual legal charge for feeding prisoners.