Find the answer to your Linux question:
Results 1 to 8 of 8
Hi All, How does the write() system call work if it's writing it to a device port and how would I know if the data is being sent immediately and ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2006
    Posts
    3

    device write() system calls


    Hi All,

    How does the write() system call work if it's writing it to a device port and how would I know if the data is being sent immediately and not to a buffer? For example,

    int fd;
    fd = open( "/dev/ttyAM0", 0_RDWR );
    write( fd, "abc\r", 4 )

    I would imagine that after invoking the above system call, "abc\r" is being immediately sent to the device port at the path "/dev/ttyAM0". Is that true or is "abc\r" sent to somewhere else?

    Thanks in advance for the help.

  2. #2
    Linux User fernape's Avatar
    Join Date
    Dec 2005
    Location
    Holland
    Posts
    301
    It depends on the device: block devices (hard disks, cdroms, etc...) are buffered while character devices are not.

    Since you are dealing with a character device, your string will be sent inmediately. But if you need an "inmediate" writing for block devices, use fflush(filedescriptor).

    And remember: you are not working with write() system call, but with write() form libc that performs a lot of tasks before calling the actual write system call.

    Best regards

  3. #3
    Just Joined!
    Join Date
    May 2006
    Posts
    3
    Hi fernape,

    Thanks for the reply. One follow-up question, is there a way that I can buffer the characters sent to a character device? So the device that the program talks to can receive "abc\r" at once rather than 'a', 'b', 'c' '\r'.

    Cheers!

  4. #4
    Linux User fernape's Avatar
    Join Date
    Dec 2005
    Location
    Holland
    Posts
    301
    I think not. You can delay the delivery using a temporary variable, but then, the device will receive the characters one by one.

    Best regards

  5. #5
    Just Joined!
    Join Date
    May 2006
    Posts
    3
    Hi fernape,

    When the device receives the character one by one, is this process the same as transmitting a line of text with character delay?

    I am trying to mimic the process of Windows' hyperterminal transfering a 3-line text file with 1000 ms line delay and 0 ms char. delay by using the low-level write() system call. Any idea of how I can achieve that? Thanks.

    Cheers!

  6. #6
    Linux User fernape's Avatar
    Join Date
    Dec 2005
    Location
    Holland
    Posts
    301
    I think yes, but it is possible that if you delay characteres for a long time, the device "closes conection" or performs some action (I'm thinking about a serial modem dealing with AT command.)

    I think you can assume that when you send something like "abcdefgh" the delay between characters is 0 unless problems with the line or similar. So you need a way to hold the emission of each complete line (use a string).

    Best regards

  7. #7
    Just Joined!
    Join Date
    Oct 2008
    Posts
    3

    What is the maximum buffer size write uses for blok devices?

    Hi All,

    This is in continuation with the original question asked by the OP. So, what is the maximum buffer size write() uses for the block devices?

    Is this buffer a configurable thing?

    Thanks in advance,

    Regards,

  8. #8
    Just Joined!
    Join Date
    Oct 2008
    Posts
    3
    Hi All,

    How does the write() system call work if it's writing it to a device port and how would I know if the data is being sent immediately and not to a buffer? For example,

    int fd;
    fd = open( "/dev/ttyAM0", 0_RDWR );
    write( fd, "abc\r", 4 )

    I would imagine that after invoking the above system call, "abc\r" is being immediately sent to the device port at the path "/dev/ttyAM0". Is that true or is "abc\r" sent to somewhere else?

    Thanks in advance for the help.
    If you want this to be written into a file immediately, use the O_FSYNC option too while opening the file. Or, alternatively you can use fsync() after the write. Hope, this helps,
    Last edited by vkv.raju; 10-30-2008 at 03:20 PM. Reason: EDIT1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •