Thread: making 2 processes run simultaniously

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    23

    making 2 processes run simultaniously

    Hi,

    Is there a ay to get lineRead and testModule to run at the same time. I have kind of implemented the fork method. but since it goes into an if statement lineread is executed and completed before testModule has a chance to run. In what order, or if i'm on the wrong track, what method should i use to make these modules run in a multitasking environment!!

    Matt

    Code:
    pid_t pid = fork();
            if ( pid == 0 ) 
            {
                lineRead(fptr2, fptr1);
            // stuff for child only
            // this normally exits the process when its done
            } 
            else if ( pid != -1 ) 
            {
                testModule();
      // stuff in parent, if child runs
            } else {
                testModule1();
      // stuff in parent, with no child
            }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    They will run simultaneously the way you have it, although they don't share the same memory space.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    23
    Maybe I see concurrently in a different perspective,

    at present the lineRead executes and completes, before the testModule is implemented.

    To help, the code for lineRead and testModule are included.

    Code:
    void lineRead(FILE *fin, FILE *fout)
    {
        char buff[MAX_LEN];
        
        while (fgets(buff, MAX_LEN, fin) != NULL)
        {
           
           fputs(buff, fout);
           printf("%s and pidvalue is %d \n", buff, getpid());
           sleep(3);
        }
    }
    
    void testModule()
    {
        int i = 0;
        for(i=0; i<10; i++)
        {
            printf("Hi");
            sleep(2);
        }
    }
    Matt

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    If you want to control the order in which things run, then you'll have to use some form of interprocess communication/synchronization.
    Otherwise, the kernel decides who runs when and for how long.

    The code never truly runs at the same time unless your running on a machine with more than 1 CPU.

    gg

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Maybe you should explain what you're trying to achieve, so that we can advise you better. I see no direct relationship between lineRead() and testModule(), so what makes you think one should wait for the other?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    23
    I don't want one to wait for the other . .

    I want them to work simultaniously.. testModule will eventually be the entire program, and lineRead will retrieve a customer from the customer file periodically at Ts time. Therefore it must run whilst the main program is working. It is kinda like a sub-program.

    Matt

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Might want to then look into threads.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Processes not dying
    By Elkvis in forum Linux Programming
    Replies: 12
    Last Post: 04-23-2008, 08:59 AM
  2. Stopping Processes Question
    By brett in forum Linux Programming
    Replies: 3
    Last Post: 06-24-2007, 10:15 PM
  3. How I can Run exe file in C++
    By palang in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2006, 11:55 AM
  4. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM
  5. Computer Processes.... Which can be stopped?
    By Sevrin in forum Tech Board
    Replies: 3
    Last Post: 06-08-2003, 08:13 PM