Solved

execvp and fork problems

Posted on 2011-03-20
5
602 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
Comment Utility
0
 

Author Comment

by:crazy4s
Comment Utility
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
Comment Utility
only get part of the solutions...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now