crazy
crazy

Reputation: 11

Can printf suspend after printing half the contents

Can printf suspend in the middle of printing an output? If yes can someone provide me a relevant context. For e.g

Is it possible that following printf() prints printf("Hello World\n")

First prints "H" then sleeps for say 10 second and then print "ello World"

Upvotes: 1

Views: 512

Answers (4)

Adam Siemion
Adam Siemion

Reputation: 16039

Yes, it is possible. Since you are using printf (and not fprintf) the output will be placed on the standard output, now if you redirect your output to a file/device that blocks writes then this simple call to printf can sleep for e.g. 10 seconds.

Scenario:

  • create echo.c:

    #include <stdio.h>
    main() {
         printf("Hello World\n");
    }
    
  • create a named fifo:

    mkfifo fifo
    
  • compile echo.c:

    make echo
    
  • run echo and redirect its output to fifo:

    ./echo > fifo
    
  • this blocks until some other process reads the fifo file, so after 10 seconds in a different shell run:

    cat fifo
    
  • only then the echo program is able to continue and write "Hello World\n" to the fifo file

Upvotes: 4

Oleg Olivson
Oleg Olivson

Reputation: 499

I see 2 solutions:

  1. Simply divide printf() into two printf()'s

    printf("H");
    sleep(10);
    printf("ello World\r\n");
    
  2. Another way is to rewrite the putc() function. It is used by printf() to output characters, so if you add some delay in putc() you'll get your 10 seconds between symbols are printed. Note: this delay will be implemented to any of your characters, not for only one and once.

    int putc(int c, FILE *fp){
        char ch;
        ch = c;
        write(fp->fd, &ch, 1);
        sleep(10);
        return(c);
    }
    
    int main(){
        char test[] = "Hello World\r\n";
        printf("%s", test);
        return 0;
    }
    

PS: I took putc() source from some internet page, please recheck it before using (maybe find any other putc() implementation).

Upvotes: 0

DiJuMx
DiJuMx

Reputation: 524

You can write your own version of printf (say printf_delay) which takes your string to print, and iterates over each character, printing and then sleeping for a specified length of time.

e.g.:

void printf_delay(char* str, unsigned int delay){
    int c=0;
    while (str[c] != '\0'){
        /* Print character */
        /* Sleep for */
        /* Increment c */
    }
}

Upvotes: 0

jim mcnamara
jim mcnamara

Reputation: 16389

printf ultimately calls write(). If the write() system call returns a -1 because the system call received EINTR, for example, then it is possible for some data to have been written. However, the tty (screen) is line buffered. This means nothing appears on the screen until a \n character is placed on the output stream.

The EINTR would happen on a system that is being really hammered by I/O, for example. Because write() is not guaranteed to complete, it can be interrupted.

So, I do not get what you are seeing. What problem do you see?

Upvotes: 2

Related Questions