open() and read() system calls...program not executing open() and read() system calls...program not executing unix unix

open() and read() system calls...program not executing


The reason you don't see the printed message is because you don't flush the buffers. The text should show up once the program is done though (which never happens, and why this is, is explained in a comment by trojanfoe and in an answer by paxdiablo). Simply add a newline at the end of the strings to see them.

And you have a serious error in the read/write loop. If you read less than the requested 512 bytes, you will still write 512 bytes.

Also, while you do check for errors when opening, you don't know which of the open calls that failed. And you still continue the program even if you get an error.

And finally, the functions are very simple: They call a function in the kernel which handles everything for you. If you read X bytes the file pointer is moved forward X bytes after the call is done.


The reason you don't see the message is because you're in line-buffered mode. It will only be flushed if it discovers a newline character.

As to why it's waiting forever, you'll only get -1 on an error.

Successfully reading to end of file will give you a 0 return value.

A better loop would be along the lines of:

int bytes_left = 512;while ((bytes_left > 0) {    bytes_read = read(src, tmp_buf, bytes_left);    if (bytes_read < 1) break;    write(dest, tmp_buf, bytes_read);    bytes_left -= bytes_read;}if (bytes_left < 0)    ; // error of some sort