UPDATE
I had very long lines in the file, and I put a newline after each 80th character (I used the sed command for that). Now the programs work fine. I can time them, and the results make sense.
Though, I have no idea why that caused a problem.
TLDR;
Using the /usr/bin/time command on my C programs does not work as expected. The time command ends before the C program, sometimes without output. E.g.
/usr/bin/time ./cat_8 lorem_ipsum.txt Problem description
I am doing the exercise 8.1 of K&R of chapter 8.
Exercise 8-1. Rewrite the program cat from Chapter 7 using read, write, open and close instead of their standard library equivalents. Perform experiments to determine the relative speeds of the two versions.
Excerpt From: Brian W. Kernighan. The C Programming Language, Second Edition
You can see the programs below. The problem I have is with both programs.
I try to time the programs to compare them. The file size lorem_ipsum.txt is 35M.
me@virtualbox:~$ ls -hs lorem_ipsum.txt 35M lorem_ipsum.txt The cat programs run without issue, but I cannot get a result from the time command for some reason. Google cannot help and I am clueless.
I compiled the programs with gcc, no options except the -o of course. I work on an Ubuntu virtual machine.
Does anyone know what the problem might be or what I am doing wrong?
Chapter 8 (cat_8 program)
#include <fcntl.h> #include <syscall.h> #include <stdio.h> void error(char *fmt, ...); /* cat: concatenate file */ int main(int argc, char** argv) { int fd; void filecopy(int, int); char *prog = argv[0]; /* program name for errors */ if (argc == 1) filecopy(0, 1); else while (--argc > 0) if ((fd = open(*++argv, O_RDONLY, 0)) == -1) error("%s: can't open %s\n", prog, *argv); else { filecopy(fd, 1); close(fd); } return 0; } /* filecopy: copy file ifp to file ofp */ void filecopy(int fdin, int fdout) { char buf[BUFSIZ]; int n; while ((n = read(fdin, buf, BUFSIZ)) > 0) write(fdout, buf, n); } Chapter 7 (cat_7 program)
#include <stdio.h> #include <stdlib.h> /* cat: concatenate file, version 2 */ int main(int argc, char* argv[]) { FILE *fp; void filecopy(FILE *, FILE *); char *prog = argv[0]; /* program name for errors */ if (argc == 1) filecopy(stdin, stdout); else while (--argc > 0) if ((fp = fopen(*++argv, "r")) == NULL) { fprintf(stderr, "%s: can't open %s\n", prog, *argv); exit(1); } else { filecopy(fp, stdout); fclose(fp); } if (ferror(stdout)) { fprintf(stderr, "%s: error writing stdout\n", prog); exit(2); } return 0; } /* filecopy: copy file ifp to file ofp */ void filecopy(FILE *ifp, FILE *ofp) { int c; while ((c = getc(ifp)) != EOF) putc(c, ofp); }
straceshowtimesometimes doing? E.g.strace -f -o trace time ...stracehas the same issue. sometimes means that I can see the output somewhere in the middle of the text that is displayed on the screen. It's just so much text that I cannot scroll all the way up to the command in the shell. It could be that the output is somewhere before the visible shell; in other words thetimecommand and the C program might be running concurrently. Not sure though.closeon aFILE*which is wrong; your compiler should have complained unless it is using the 27-year-old C standard (and IMO even then it should warn). This is likely to lose some of the output, but on no reasonable system should it lose 35M.vimandgedit; it looks ok to me. I don't know where to search....