Thanks a lot for your help. I highly appreciate your immediate help nugent.
Best Regards
Abdul Hameed
: you you need to do is loop through the command line arguments (argv in the example) and call the fork_n_execute() function in the code. This function attempts to use fork() to create a new process and then use the new process uses the system() function to execute the command.
:
:
: #include <stdio.h>
: #include <stdlib.h>
: #include <sys/types.h>
:
: int fork_n_execute(const char *command)
: {
: int status;
: pid_t pid;
:
: pid = fork ();
:
: if(pid == 0)
: {
: /* This is the child process */
: system(command); // execute the command
: // we call exit() when system() returns to complete child process
: exit(EXIT_SUCCESS);
: }
:
: else if(pid < 0)
: {
: /* The fork failed */
: printf("Failed to fork(): %s\n", command);
: status = -1;
: }
:
: /* This is the parent process
: * incase you want to do something
: * like wait for the child process to finish
: */
: /*
: else
: if(waitpid(pid, &status, 0) != pid)
: status = -1;
: */
: return status;
: }
:
: int main(int argc, char *argv[])
: {
: if(argc == 1)
: {
: printf("Usage:\n\t%s <command 1> ... <command n>\n", argv[0]);
: return(-1);
: }
:
: int i;
:
: /* Loop through argv */
: for(i = 1; i < argc; i++)
: fork_n_execute(argv[i]);
:
: return 0;
: }
:
:
:
: this program will exit once it has called fork() on all command line arguments.
:
: we cannot control which process has control of the stdout at any one time, so if the commands output to stdout it will be all messed up
:
:
:
:
:
: ------
: nugent
:
:
:
: