Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

An interrupt handler can call function sprintf()?

76 views
Skip to first unread message

fl

unread,
May 19, 2013, 1:11:18 PM5/19/13
to
Hi,

I remember it said that C lib function printf() cannot be called in a device driver in Linux. Today I come across the following code snippet. I wonder that sprintf function can be called in the interrupt function?

Interrupt function is part of device driver or not?

Please clarify those concepts if you can. Thanks.






......
irqreturn_t short_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
struct timeval tv;
int written;

do_gettimeofday(&tv);
/* Write a 16 byte record. Assume PAGE_SIZE is a multiple of 16 */
written = sprintf((char *)short_head,"%08u.%06u\n",
(int)(tv.tv_sec % 100000000), (int)(tv.tv_usec));
BUG_ON(written != 16);
short_incr_bp(&short_head, written);
wake_up_interruptible(&short_queue); /* awake any reading process */
return IRQ_HANDLED;
}

Jerry Peters

unread,
May 19, 2013, 4:38:56 PM5/19/13
to
fl <rxj...@gmail.com> wrote:
> Hi,
>
> I remember it said that C lib function printf() cannot be called in a device driver in Linux. Today I come across the following code snippet. I wonder that sprintf function can be called in the interrupt function?
>
It can't, because it writes to a file, stdout. Sprintf "writes" to an
existing string so there's no i/o. Sprintf is just a convenient way to
construct a fancy formatted string.

There is another function, kprintf, which can be used similiarly to
printf, but it wrties to a kernel buffer.

Jerry

Jerry Peters

unread,
May 19, 2013, 4:58:45 PM5/19/13
to
Jerry Peters <je...@example.invalid> wrote:
> fl <rxj...@gmail.com> wrote:
>> Hi,
>>
>> I remember it said that C lib function printf() cannot be called in a device driver in Linux. Today I come across the following code snippet. I wonder that sprintf function can be called in the interrupt function?
>>
> It can't, because it writes to a file, stdout. Sprintf "writes" to an
> existing string so there's no i/o. Sprintf is just a convenient way to
> construct a fancy formatted string.
>
> There is another function, kprintf, which can be used similiarly to
> printf, but it wrties to a kernel buffer.

Oops, meant printk, not kprintf.

Jerry
0 new messages