in this EXAMPLE you can see the execlp() function and the fork() function:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()
{
pid_t pid;
char *pathvar;
char newpath[1000];
pathvar = getenv("PATH");
strcpy(newpath, pathvar);
strcat(newpath, ":u/userid/bin");
setenv("PATH", newpath);
if ((pid = fork()) == -1)
perror("fork error");
else if (pid == 0) {
execlp("newShell", "newShell", NULL);
printf("Return not expected. Must be an execlp error.n");
}
}
Here you can see how to make a pipe between two process:
/*
“ls -R | grep <pat>” where <pat> is a pattern
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv [])
{
int pipe1[2];
int pid1;
int pid2;
char stringa[100];
pipe(pipe1); // creo la pipe
if((pid1=fork())==0)
{
close(pipe1[0]);
dup2(pipe1[1],1);
close(pipe1[1]);
execlp("ls","ls","-R",NULL);
}
if((pid2=fork())==0)
{
close(pipe1[1]);
dup2(pipe1[0],0);
close(pipe1[0]);
execlp("grep","grep",argv[1],NULL);
}
close(pipe1[0]);
close(pipe1[1]);
waitpid(pid1,NULL,0);
waitpid(pid2,NULL,0);
exit(0);
}