Solved

Execvp function

Posted on 2009-07-06
6
335 Views
Last Modified: 2013-12-26
I have written  a program to parse user commands and send them to execvp to execute, which it does, but after the execvp nothing happens. I want it to print out a message, but it just gets stuck. Am I missing something after the execvp call?
#include <stdio.h>
#include <string.h>
 
int main(int argc, char *argv[])
{
char comm[100];
char *pnt[100];
int i=0;
int pid;
int status;
 
if (argc==1 ||  argc >2){
printf("Incorrect number of arguments. Exiting\n");
exit(0);
}
printf("%s ", argv[1]);
gets(comm);
 
pnt[i] = strtok (comm," ");
 
while (pnt[i] != NULL){
pnt[++i]=strtok(NULL," ");
}
if ((pid = fork()) < 0){
printf("Error unable to fork");
exit(1);
}
else if (pid == 0){
int count=1;
printf("Executing command no:%d\n",count);
execvp(*pnt,pnt);
printf("Command no. %d was executed\n",count);
count++;}
else{ while (waitpid(pid));
printf("Would you like to enter another command?");
exit(0);
}
}

Open in new window

0
Comment
Question by:wannabe_cpgeek
[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
  • 2
6 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 250 total points
ID: 24791319
One thing you must understand about the exec*() functions. When you call it, the program you call actually _replaces_ the running program, in memory, so no lines of code after the exec() call will be executed, unless exec() failed.

To kick off a shell command like that you can use popen() or fork() + exec()

0
 

Author Comment

by:wannabe_cpgeek
ID: 24791338
So is there a way I can go back to my program and run the other commands?
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24791371
use popen() to run the commands. That opens the called program as a process and you can read from the "stream" just like reading from a FILE * (actually popen returns a FILE*).

Read the man page on popen or google for a sample.

0
 

Author Comment

by:wannabe_cpgeek
ID: 24791466
Thank you, but my assignment saids I have to use execvp, so is there a way using it?
0
 
LVL 12

Assisted Solution

by:Let_Me_Be
Let_Me_Be earned 250 total points
ID: 24792067
The answer was already supplied in the first reply. If you want to execute another process running in parallel with the original one, you must use a combination of fork and exec. Fork will create a copy of your running program, exec with replace the running program (use the child for this) with a new one.

Here is a more detailed overview: http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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
child constructor and parent constructor, overriding and overloading 6 95
unix example issues 18 114
Not needed 13 134
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 96
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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 opening and reading files in the C programming language.

751 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