Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Parallel processing using fork()

Good day everyone.

I have a project that asks us to execute multiple shell commands in parallel using fork().

e.g. shell ls ps
the program should creates two childs and execute them in parallel.

the question is how can I create multiple proecesses and execute them in parallel.

Please if anyone knows reply to me as soon as possible.

Best Regards
Abdul Hameed

Comments

  • nugentnugent Posts: 87Member
    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.

    [code]
    #include
    #include
    #include

    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
    ", 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:
    %s ...
    ", argv[0]);
    return(-1);
    }

    int i;

    /* Loop through argv */
    for(i = 1; i < argc; i++)
    fork_n_execute(argv[i]);

    return 0;
    }
    [/code]


    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



  • hameedohhhameedohh Posts: 8Member
    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.
    :
    : [code]
    : #include
    : #include
    : #include
    :
    : 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
    ", 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:
    %s ...
    ", argv[0]);
    : return(-1);
    : }
    :
    : int i;
    :
    : /* Loop through argv */
    : for(i = 1; i < argc; i++)
    : fork_n_execute(argv[i]);
    :
    : return 0;
    : }
    : [/code]
    :
    :
    : 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
    :
    :
    :
    :

  • user11user11 Posts: 3Member
    hi ... hope u'll will be fine..

    well i have some sort of question regarding implementation of some algorithm in linux related to parallel processing...kindly help me out..

    well i need the
    1.code of implementing one to all broadcast and all to one reduction for a ring..
    2.implementing ALL to All broadcast and reduction for mesh
    3.Implemenation of ALl reduction for hypercube..
    kindly if any one have the stuff related to my post kindly do paste the code if u have..


    Regards
    USer11

Sign In or Register to comment.