My Tech notes: Use of Execlp Functions C program Explained
Subscribe

Unix Documentation

Free Online Unix Training Materials

Lists many links to free Unix training materials.

Pointers and Arrays Materials

Pointers and Arrays materials Explained for C beginners

C FAQ and General Questions C Interview Questions

Powered By

Free XML Skins for Blogger

Powered by Blogger

Sunday, November 16, 2008

Use of Execlp Functions C program Explained

Below code listing illustrates usage of execl function series, and talks about how to redirect the output from these functions.
Here is trick to redirect the output from execlp command series
In other words implematiation of "ls -l >/var/test.log"
first open file using "open" system call

#include
#include
#include

int open(const char *pathname, int flags);

then duplicate this file pointer with stdio file pointers
stdio file pointer are 0 for stdin
1 for stdout and 2 for stderr

use of dup2

dup and dup2 create a copy of the file descriptor oldfd.
int dup2(int oldfd, int newfd);


here is c example implementation in function;

int spawn(char* cmd)
{
pid_t child_pid;
int fptr,fptr2;
char tmp_devfs[20];
int child_status=-1,ret=-1;

// printf("executing cmd=%s\n",cmd);
fptr=open("/var/test.log",O_RDWR);
/*create new process */
child_pid = fork();
if (child_pid != 0){
/* This is the parent process. */
ret = child_pid;
//printf("c_pid=%d",child_pid);
}
else
{
/*Here is trick to direct or redirect the output to execlp*/
dup2(fptr,1);
close(fptr);
execl("/bin/sh","sh","-c",cmd,(char*)NULL);
/* The execl function returns only if an error occurs. */
fprintf (stderr, "an error occurred in execl sfdisk\n.");
exit -1;
}
//printf("done with cmd=%s child ret=%d\n",cmd,ret);

return ret;
}

The child process starts executing in another shell replacing the current process image or child;
parent process continues to execute.

if one has to wait for child to finish one can make use of wait() functions;

wait(&child_status);
if (WIFEXITED (child_status))
ret = WEXITSTATUS (child_status);
else
ret = -1;

Add this code before return statement.
In a simillar way if one needs to read input from file while using exece series function open that file in read mode and duplicate the file descriptor with stdin file descriptor.
ie simply change

dup2(fptr,0);
close(fptr);

Related questions:
use the execlp command in my C program to run the PS command and send the output into a file specified in the Argument.
Man pages for fork, open, exec and wait function.
man page for open
The fork system call creates a new child process.
The exec functions (there are more than one) are a family of functions that execute some program within the current process space.
The two versions of exec that we will be using are execlp and execvp. The difference is in the way they accept arguments, however both of them are typically called the same way.
The wait function is in and it’s a special function that allows a parent process to wait for its child processes to exit.

No comments:

Post a Comment