Solved

execvp and fork problems

Posted on 2011-03-20
5
630 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

627 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