Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

execvp and fork problems

Posted on 2011-03-20
5
Medium Priority
?
660 Views
Last Modified: 2013-12-17
Hi all,
Can anyone explain the use of execvp, fork and dup2?
As i need to understand how this func works first in order to write a program that will output each 'command' (separated by pipes) with a separate process along with its process id.
we'll need two codes to get this compiled, one is given which is the makeargv.c (to make the commands into token form) and we should write another one which i've stated above.

the makeargv.c (given)
#include "makeargv.h"
#include <string.h>
#include <stdlib.h>
/*
* Make argv array (*arvp) for tokens in s which are separated by
* delimiters.   Return -1 on error or the number of tokens otherwise.
*/
int makeargv(char *s, char *delimiters, char ***argvp)
{
  char *t;
  char *snew;
  int numtokens;
  int i;
   /* snew is real start of string after skipping leading delimiters */
  snew = s + strspn(s, delimiters);
                             /* create space for a copy of snew in t */
  if ((t = calloc(strlen(snew) + 1, sizeof(char))) == NULL) {
     *argvp = NULL;
     numtokens = -1;
  } else {                     /* count the number of tokens in snew */
     strcpy(t, snew);
     if (strtok(t, delimiters) == NULL)
        numtokens = 0;
     else
        for (numtokens = 1; strtok(NULL, delimiters) != NULL;
             numtokens++)
             ;
               /* create an argument array to contain ptrs to tokens */
     if ((*argvp = calloc(numtokens + 1, sizeof(char *))) == NULL) {
        free(t);
        numtokens = -1;
     } else {            /* insert pointers to tokens into the array */
        if (numtokens > 0) {
           strcpy(t, snew);
           **argvp = strtok(t, delimiters);
           for (i = 1; i < numtokens + 1; i++)
              *((*argvp) + i) = strtok(NULL, delimiters);
        } else {
          **argvp = NULL;
          free(t);
        }
     }
  }
  return numtokens;
}

Open in new window


and this should what i should be writing to output each 'command' (separated by pipes) with a separate process along with its process id. I'm not sure how should i write this code, this is just a start for my code.
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>

extern makeargv (char *, char *, char ***)

int main ()
{
          char buf[80];
          int i, j;
          char **argp;
          int pid;
          write(1,"prompt ",7);
          fgets(buf, 80, stdin);
          j = makeargv(buf, "|\n", &argp);
          pid = fork();
          for (i=0, i<j, i++)
                printf("%s process ID: %d",argp[i], pid);
          execvp(argp[i], &argp[i]);

}

Open in new window


eg: ls -l | cmd1 | cmd2

output should be something like that:
ls -l process ID 41
cmd1 process ID 42
cmd2 process ID 43

Any help will be greatly appreciated:)
0
Comment
Question by:crazy4s
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
Deepu Abraham earned 1500 total points
ID: 35180869
0
 

Author Comment

by:crazy4s
ID: 35184855
I've edited some of my codes for the  output each 'command' (separated by pipes) with a separate process along with its process id.

test.c
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include "ourhdr.h"

extern int makeargv(char *, char * , char ***);

int main()
{
        char **argp;    // THE POINTER TO THE ARRAY OF POINTERS TO BE FILLED BY
        int i,j,vpret;
        char buf[80];   // command line
        pid_t pid;      // process ids
        while(strcmp(fgets(buf, 80, stdin), "exit\n")!=0)  // USER COMMAND
        {
                j=makeargv(buf,"|",&argp); // J = NUMBER OF TOKENS
                for (i=0;i<j;i++)
                printf("%s\n",argp[i]);
                pid = fork();
                switch (pid)
                {
                        case -1:
                                err_sys("fork failed");
                                break;
                        case 0: // child
                                execvp(argp[i],argp);
                                printf("%s process ID: %d\n", argp[i], pid);
                                exit(1);
                        default: // parent
                                waitpid(pid, NULL, 0);
                                write(1,"\nPrompt>",8);
                                break;
                }
        }
        printf(" make argv test exited\n"); //successfully exit
}

Open in new window


however my output is just like this:

bronco:~> gcc test.c makeargv.c error.c
bronco:~> ./a.out
ls -l | cat test.c | cat test2.c
ls -l
 cat test.c
 cat test2.c


Prompt>ls -l | cat test.c | cat test2.c
ls -l
 cat test.c
 cat test2.c


Prompt>exit
 make argv test exited

Can somebody help me what should i do in order to output the process along its ids???
thanks in advance:)
0
 

Author Closing Comment

by:crazy4s
ID: 35224102
only get part of the solutions...
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question