GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Sep-2006, 21:02
sac_garg sac_garg is offline
New Member
 
Join Date: Sep 2006
Posts: 2
sac_garg is on a distinguished road

Program on creating a process!!


Hi guys. can u please help me this program. It is as follows:

WAP fork.c to do the following:

1) First print your name and student id;
2) Accept and parse user's input, which may consist of multiple (for simplicity at most 2) shell commands separated by '&' (ampersand) character
(for example ls -a & cat fork.c).
3) Create a child process using system call fork;
4) Execute the first command in the child process using system call exec; and
5) Execute the second command, if exists, in the parent process using system call exec.

Student ID is like any other ID. User will input it.
The fork() function is used to create a new process from an existing process.
The new process is called the child process, and the existing process is called the parent. You can tell which is which by checking the return value from fork(). The parent gets the child's pid returned to him, but the child gets 0 returned to him.
  #2  
Old 29-Sep-2006, 00:40
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Program on creating a process!!


Quote:
Originally Posted by sac_garg
can u please help me this program.

What part have you done already? With what part would you like some help? What kind of help?

In other words: I respectfully suggest that you Show Us Some Code, And Then Ask Your Questions.

Regards,

Dave
  #3  
Old 01-Oct-2006, 02:35
sac_garg sac_garg is offline
New Member
 
Join Date: Sep 2006
Posts: 2
sac_garg is on a distinguished road

Re: Program on creating a process!!


i have created the child and parent process. now i'm getting confused in inputting the second command if exists in the parent process using system call exec. also how to accept multiple commands separated by '&'

my work is as follows:

CPP / C++ / C Code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

main()
{
    char buf[1024];
    char *args[64];

    for (;;) {
        printf("Command: ");

        if (gets(buf) == NULL) {
            printf("\n");
            exit(0);
        }

        /*
         * Split the string into arguments.
         */
        parse(buf, args);

        /*
         * Execute the command.
         */
        execute(args);
    }
}

/*
 * parse--split the command in buf into
 *         individual arguments.
 */
parse(buf, args)
char *buf;
char **args;
{
    while (*buf != NULL) {
        /*
         * Strip whitespace.  Use nulls, so
         * that the previous argument is terminated
         * automatically.
         */
        while ((*buf == ' ') || (*buf == '\t'))
            *buf++ = NULL;

        /*
         * Save the argument.
         */
        *args++ = buf;

        /*
         * Skip over the argument.
         */
        while ((*buf != NULL) && (*buf != ' ') && (*buf != '\t'))
            buf++;
    }

    *args = NULL;
}

/*
 * execute--spawn a child process and execute
 *           the program.
 */
execute(args)
char **args;
{
    int pid, status;

    /*
     * Get a child process.
     */
    if ((pid = fork()) < 0) {
        perror("fork");
        exit(1);
    }

    /*
     * The child executes the code inside the if.
     */
    if (pid == 0) {
        execvp(*args, args);
        perror(*args);
        exit(1);

}
Last edited by admin II : 01-Oct-2006 at 05:01. Reason: Please surround your C code with [cpp] ... [/cpp]
  #4  
Old 01-Oct-2006, 10:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Program on creating a process!!


Quote:
Originally Posted by sac_garg
i have created the child and parent process.
my work is as follows:


Point number 1:
The following lines were deleted from the Original Program:

CPP / C++ / C Code:
    }
    /*
     * The parent executes the wait.
     */
    while (wait(&status) != pid)
        /* empty */ ;

Since the closing brace '}' was deleted in the above lines, your code won't compile.

Point number 2:
The comment lines from the Original Code are helpful, and, more importantly, the Original Code (from http://www.cs.cf.ac.uk/Dave/C/section2_22_22.html) accompanied a pretty good explanation of how forks and child processes, etc. work. The code itself was copyrighted in 1994 by Dave Marshall. The particular example shows how to create a child process and to make the parent to wait until the child process was complete before returning to the command prompt. You can see lots of good stuff here: http://www.cs.cf.ac.uk/Dave/C/CE.html

Did I say it was a "pretty good" explanation? Change that to "really good".

Note that the code uses old-style function headers, and in my opinion it should be cleaned up and modernized before you work on it. (Including elimination of gets() in favor of fgets()). Since parse() and execute() don't return anything, I think it's appropriate to declare them with "void" instead of using legacy C's int default. The example works exactly as it should, but part of the learning process is to build on experience and bring something new to the party.

In particular, the other stuff that was commented out is the action that will occur if the current process is the parent.

So, if I were doing the assignment, the execute() function might look something like the following. See footnote.

CPP / C++ / C Code:
void execute(char **args)
{
    /* 
     * maybe some extra variables to hold argument lists for
     * parent and child calling execvp.
     * I just called them args1 and args2 below. Of course
     * it is possible just to rearrange args to be able to use
     * it without having new stuff here, and that's how I
     * might do it.
     */
    pid_t cpid;

    cpid = fork();

    switch (cpid) {
    case -1:
        perror("fork");
        break;

    case 0:
        printf("This is the child process\n");
        printargs(args);
        /* do whatever the child is supposed to do with args.
         * that is: create an argument list for execvp that
         * has the first shell command from args
         */

        /*execvp(*args1, args1); */ /* args1 feeds first shell command to execvp */
        /*perror(*args1); */
        exit(1);

    default:
        printf("This is the parent process\n");
        printargs(args);
        /* do whatever the parent is supposed to do with args.
         * that is: create an argument list for execvp that
         * has the second shell command from args
         */

        /*execvp(*args2, args2); */
        /*perror(*args2); */
        exit(1);
    }
}

My style is a little different from the original author, and you can use whatever style appeals to you (maybe something different from either).


So your main program prompts "Command " and gets user input. The parse() function makes the args[i] values point to the separate words on the user comand line. Here's a way to print out what the user entered:

CPP / C++ / C Code:
void printargs(char **args)
{
    int i = 0;
    while (args[i]) {
        printf("args[%d]: <%s>\n", i, args[i]);
        i++;
    }
}

Now, the execute() routine can go through the list and use the ones it needs for the child and parent process.

With the actual execvp() commented out as I showed, here's what a run could look like:

Code:
Command: abc xyz & qq This is the child process args[0]: <abc> args[1]: <xyz> args[2]: <&> args[3]: <qq> This is the parent process args[0]: <abc> args[1]: <xyz> args[2]: <&> args[3]: <qq>

How you process the argument list into shell commands is up to you (and is, obviously part of the assignment). I just wanted to show how the example could be used to build the application that you need.


Regards,

Dave

Footnote: refer to previous thread here for some more discussion of forking and a little about execvp http://www.gidforums.com/t-11552.html
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipeline freeze simulation darklightred C++ Forum 6 27-Jul-2006 20:37
How to read particular memory location ? realnapster C Programming Language 10 10-May-2006 10:11
Creating a "varargs" addition program crystalattice Python Forum 2 08-Oct-2005 23:06
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 14:30
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:08.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.