Thread: another problem with functions & parameters

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    32

    another problem with functions & parameters

    Hi there

    i've got this type of thing in a function

    Code:
    int function1(char *buf, int *size, int options)
    {
    
      forking
      if child{
               function2(buf,size);
               printf("buf of child:%s",buf); //OK
       }
      if parent{
         //using semaphores i made this part guaranteed to run after the child  returned from function2 
         printf("buf of parent:%s",buf);//empty :s
      }
    
    }
    function 2 then changes the value of the buf and are returns the changed value through the parameter... I want such that buf is ok when printed from the parent that is will also be the new updated value for whoever calls function1 in the first place... the problem is not how to make the parent run after i did that already... i need to know how to return buf...

    Thanks
    Last edited by Cmaniac; 04-17-2007 at 01:18 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I'm kind of confused as to what you're actually trying to do. I understand that you're receiving buf as a parameter in the first function. After you fork(), the child will modify buf by means of the second function, but the parent will have the original version.

    If you want both the child and parent to have the same value of buf, namely the changed one, then you should change it before fork()ing.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    32
    i understand the question is a bit confusing... the thing is that i'm forking a lot of children if you remember me posting from a previous post... the problem is that one and only one will change the contents of buf... I got that sorted out too..

    However whoever called function1 in the first place now; that is from another process must have the value of the successfull return from function2.. is that about more clear?

    I'm thinking about opening a message queue between the child and the parent... however i'm thinking if there is a more easier way out (the reason for this post) which i don't know yet :S

    Thanks

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you need the parent or another child to know the value of a variable that has been changed by another child, then yes, you'll need some way to communicate or share memory.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    32
    yeah ok thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with function's pointer!
    By Tirania in forum C Programming
    Replies: 5
    Last Post: 11-28-2008, 04:50 AM
  2. Problem compiling files that store functions
    By tiachopvutru in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2008, 05:42 PM
  3. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  4. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM