LINUX programming

Moderators: ITA
Number of threads: 1347
Number of posts: 2935

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Parallel processing using fork() Posted by hameedohh on 6 Nov 2006 at 3:10 AM
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
Report
Re: Parallel processing using fork() Posted by nugent on 6 Nov 2006 at 9:41 AM
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



Report
Re: Parallel processing using fork() Posted by hameedohh on 7 Nov 2006 at 3:46 AM
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
:
:
:
:

Report
Parallel processing using fork() Posted by user11 on 21 Mar 2011 at 11:00 AM
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




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.