Reputation: 35522
So I'm new to C and this is one problem I have and cannot understand. First, execlp() executes a program just by name(by searching for it) with parameters. Here I want to execute "who" with "-u" as parameter, but it does not return anything from execlp. Why? Is this normal?
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
main()
{
int j = 0;
if(fork() == 0)
{
j++;
if(execlp("who", "who", "-u", (char*)0) == -1)
{
j++;
}
else
{
printf("\nStoinostta na j = %d", j);
return;
}
}
else
{
--j;
printf("\nStoinostta na j=%d", j);
}
printf("\nStoinostta na j ravno na %d", ++j);
}
Upvotes: 1
Views: 677
Reputation: 76715
As noted by @Charles Bailey, execlp()
replaces the current process. So it never returns after running a program.
If you want to just run something, a simple way is with system()
: http://linux.die.net/man/3/system
If you want to run something and control input and output, possibly collecting the output from the command, a good way is popen()
: http://linux.die.net/man/3/popen
Upvotes: 3
Reputation: 19504
Perhaps you want the system
function. It will run another program and then return.
Another option would be to exec
after calling fork
.
fork() || execlp(...);
Upvotes: 2
Reputation: 791929
On success, execlp
replaces the current process with the command that you specify. It will only return if it fails. The manpage should make this clear.
Upvotes: 7