Thread: cannot do threading but maybe forking ???

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    cannot do threading but maybe forking ???

    hi,

    how would one pass a vector to a forked process and afterwords collect the data from that array:


    example:

    Code:
    using namespace std;
    int main (){
    
      pid_t pid;
      int status;
      vector<pid_t> pidarray(2);
      vector<int> vec(10,0);
      vector<vector<int>> a(2,vec);
      for (int i = 0; i< 2; i++){
        pid = fork();
    
        if (pid == pid_t(0)) {
            for(int z = 0; z <  10;z++){a[i][z] = z;}
            exit(0);
        }
        else if (pid > pid_t(0)){
            pidarray[i] = pid;
        }else{
            cerr << "failed\n";
            exit(1);
        }
      }
    
      for(int i =0; i<2;i++)
         waitpid(pidarray[i], &status, 0);
    
      cout << endl;
      for(int i =0; i< 2; i++){
         cout << endl;
         for(int z = 0; z <10;z++){
            cout << a[i][z]<< endl;
         }
      }
    
    
      return 0;
    
    }

    so i would like for my results obtained in forked processes to be in my a vector. Is this possible to do ??

    thnx
    Last edited by baxy; 03-11-2014 at 04:04 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why do you need to fork instead of using a thread? Using fork will lock you to POSIX/Linux. Threads are platform independent.
    fork is also problematic. While it copies all the current memory over to the new process, the two processes do not share the same memory space, so you cannot easily copy back the results. That means you need some inter-process communication.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    Hi,

    well this is related to my previous post : What to do when valgrind cannot find the problem? i just cannot go around it. I have a library that was written in 1990 in c. It is a library designed for a scientific computation of some higher dimensional vector trajectories. Back then this was "useless" (as marked by many) piece of code but now it perfectly fits into my project . Since it wasn't very popular back then and had no practical side i guess no one bordered to make thread safe version of it. I also must admit that i don't understand a large fraction of math behind it and therefore do not consider myself to be an expert in making drastic changes to the code. Thus i decided that maybe it would be better to just fork it. I only need the code to work on linux anyway since all clusters i have at my disposal are unix machines. Also I know that a semi solution would be to print results down on HDD and then after forking is done read it back again. This is definitely what i'll do if vector passing is too complicated.

    thank you

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Once you're in the domain of creating processes, then the simple and obvious way of sharing information is to use the file system.

    An alternative is to setup a pipe() between the parent and child processes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forking
    By sean in forum Linux Programming
    Replies: 5
    Last Post: 02-24-2009, 02:45 PM
  2. forking
    By Cmaniac in forum C Programming
    Replies: 6
    Last Post: 04-14-2007, 03:54 AM
  3. forking processes
    By osal in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2005, 01:44 AM
  4. forking
    By saphiroth in forum C Programming
    Replies: 3
    Last Post: 01-28-2005, 03:42 PM
  5. Forking
    By XSquared in forum Linux Programming
    Replies: 13
    Last Post: 04-22-2004, 03:13 AM

Tags for this Thread