miguelrr_11
miguelrr_11

Reputation: 13

execlp time doesn't show anything

int main(int argc, char* argv[]){
 
   execlp("time", "time", NULL);

   return 0;
}

Nevertheless, if I add an argument to time like ls, it does work:

int main(int argc, char* argv[]){
 
   execlp("time", "time", "ls", NULL);

   return 0;
}

Output:

LICENCIA                main.c                  msh                     parser.y                prueba.c                scanner.o               y.tab.h
Makefile                main.o                  parser.o                prueba                  scanner.l               tempCodeRunnerFile.c
        0,00 real         0,00 user         0,00 sys

Any idea?

Upvotes: 0

Views: 62

Answers (1)

Eric Postpischil
Eric Postpischil

Reputation: 223389

This works as expected. execlp("time", "time", NULL); is equivalent to executing /usr/bin/time (assuming that is what it resolves to; it could be different environments different from mine), and executing /usr/bin/time (again on my system, macOS 10.14.6) with no arguments does not print anything. I suppose other implementations of the time utility could print a message that no command to time was given and/or exit with an error code.

In contrast, execlp("time", "time", "ls", NULL); is equivalent to executing /usr/bin/time ls, in which case the time program executes ls and reports its times.

in the linux terminal if you type time it shows you the shell and child processes times…

When you type time in a command-line shell, it executes the shell’s built-in time function, not the system time program. To reproduce this using execlp, you must use execlp to start a shell, not time, and you must pass the shell arguments to give it a time command to execute.

Upvotes: 1

Related Questions