LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-15-2002, 07:50 AM   #1
benjaminrtz
Member
 
Registered: Jul 2002
Location: India
Distribution: Ubuntu, Gentoo
Posts: 99

Rep: Reputation: 15
Executing commands with child process


I need to write a program that will fork a new process and have the child execute a command (supplied during run time) using execl() or its brothers or sisters (execlp ......) .
NB :- The command should be supplied at run time. The parent should wait for is child o finish execion . It should be able to run any program .


****GOd is real , unless declared integer
 
Old 12-15-2002, 11:31 AM   #2
Azrael
Member
 
Registered: Sep 2002
Location: Germany
Distribution: SuSE 8.0
Posts: 96

Rep: Reputation: 15
Ok, and where exactly is your problem?
 
Old 12-18-2002, 09:54 AM   #3
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
Maybe this'll help. It's not complete but I can post the whole prog if necessary.

Code:
	    pid_t cpid;

	    /* 
	     * fork and attempt to execute the command
	     */
	    cpid = fork();

	    switch(cpid)
	    {
		case -1: // oops
		{
		    printf("Unable to claim system resources.\n");
		    break;
		}
		case 0: // i am child process
		{
		    // call execvp to start the user's command
		    if( execvp(progArgsArray[0], progArgsArray) == -1 )
		    {
			switch(errno)
			{
			    case EACCES:
			    {
				printf("access is denied: %s\n", progArgsArray[0]);
				break;
			    }
			    case EPERM:
			    {
				printf("operation not permitted: %s\n", progArgsArray[0]);
				break;
			    }
			    case ENOEXEC:
			    {
				printf("ENOEXEC\n");
				break;
			    }
			    case ENOENT:
			    {
				printf("command not found: %s\n", progArgsArray[0]);
				break;
			    }
			    default:
			    {
				printf("Error code: %d\n", errno);
			    }
			}
		    }

		    _exit(0);
		    break;
		}
		default: // i am parent process
		{
		    int status;

		    /* // this code for background processes
		    if( cmdStr[strlen(cmdStr)] != '&' )
		    {
		    */
			waitpid(cpid, &status, 0);
		    /*
		    }
		    else
		    {
			printf("process id: %d\n", cpid);
		    }
		    */

		    /*
		     * this'll show exit status
		     
		    if( WIFEXITED(status) ) 
		    {
			printf("%d [%d] exited\n", WEXITSTATUS(status), cpid);
		    }
		    */
		    
		    // this'll have to be rewritten when fg/bg are implemented
		    for( i = 0; progArgsArray[i] != NULL; i++ )
		    {
			free(progArgsArray[i]);
			progArgsArray[i] = NULL;
		    }
		    break;
		}
	    }

Last edited by no2nt; 12-18-2002 at 09:55 AM.
 
Old 12-18-2002, 12:58 PM   #4
Azrael
Member
 
Registered: Sep 2002
Location: Germany
Distribution: SuSE 8.0
Posts: 96

Rep: Reputation: 15
Nice program, I just don't understand what your problem is. Doesn't it work? If yes, where do you get stuck?
 
Old 12-18-2002, 01:31 PM   #5
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
hehe, benjaminrtz posted the question, i only posted code that might help him :-)
 
Old 12-18-2002, 03:42 PM   #6
Azrael
Member
 
Registered: Sep 2002
Location: Germany
Distribution: SuSE 8.0
Posts: 96

Rep: Reputation: 15
Uuups
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
Killing a child process from another child marri Programming 6 10-01-2004 07:08 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM
about parent and child process winwar Solaris / OpenSolaris 3 07-23-2003 06:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration