Solved

execvp and fork problems

Posted on 2011-03-20
5
625 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
5 Comments
 
LVL 11

Accepted Solution

by:
DeepuAbrahamK earned 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Open dialog with server side controls in it 3 46
Loading flat file data in tables 2 58
VB.Net creating Contact in Outlook 1 47
How to repeat the data 4 30
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

713 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