Solved

execvp and fork problems

Posted on 2011-03-20
5
605 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

895 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

12 Experts available now in Live!

Get 1:1 Help Now