The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Multi-threading-- calling same function through different threads arunchaudhary19 High Level Programming 1 12-10-2007 11:54 AM
Multi threading? enuenu High Level Programming 4 05-29-2007 06:17 AM
Regarding Multi-Threading matrixmadhan High Level Programming 5 07-20-2005 06:54 AM
Multi-threading questions DreamWarrior High Level Programming 1 11-02-2003 11:34 PM
Multi threading using posix thread library shushilmore High Level Programming 2 09-09-2002 08:12 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-27-2003
sureshraju_ma sureshraju_ma is offline
Registered User
  
 

Join Date: Nov 2003
Posts: 5
Question Multi threading using fork

Hi,
I have written a code which will run a set of process using
fork.
I want to know from You how can i start another job when one of my job in my loop is completed

My code is

Code:
#include<stdio.h>
#include<ctype.h>


main() {
int pid,cid;
ChildProcess();
          pid=getpid();
         printf("pid  : %d\n",pid);

          printf(" FORK DEMO ");
     if(!fork()) {
     cid=getpid();
            printf(" cid  : %d ",cid);
     printf("i m the child process");
     ChildProcess();
     exit(0);
   }

  printf("waiting for child");
   wait(NULL);
  printf(" child finished ");
}
ChildProcess() {
       int i;
       for(i=0;i<10;i++) {
          system("sh runcpty.sh"); //here i wrote code to run  pgms
          printf(" %d ",i);
          sleep(0);
      }
    }

This program will run only start processing 10 programs, But
i want to start another program automatically when one of process out of ten is completed (i.e Multi Tasking)
Can U please solve this problem

I added code tags for readability -- Perderabo

Last edited by Perderabo; 11-27-2003 at 06:00 PM..
  #2 (permalink)  
Old 11-27-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Well, you're not off to very good start here.

The text of your post says your program "will run a set of process using fork." But even the comments in your code disagrees with that. When you run system() in a loop as you are doing, you will run one program after another. After the tenth program finishes, the ChildProcess function will return. Then your program finally performs it's first and only fork() which is a demo. And then it exits.

To get the ball rolling, you need to call fork() 10 times in a loop. And each child process will need run one of your jobs. One of the exec() calls would be much better than system().

I think that you should get this much working first.

But basicly you will need to install a signal handler that catches SIGCHLD to detect the death of a child.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0