child process trying to use Cunit!

This is a discussion on child process trying to use Cunit! within the C Programming forums, part of the General Programming Boards category; hi guys, i'm working with processes and i find an obstacle that i can't handle, and i can't even find ...

  1. #1
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59

    child process trying to use Cunit!

    hi guys, i'm working with processes and i find an obstacle that i can't handle, and i can't even find anything on the web regarding it.the application is a basic producer\consumer application with the use of a buffer. the logic part is gone, and i'm testing it with Cunit.one of the test should be something like:fork() to a child process, that tries to read a message in the buffer. obviously there's no way a child process can return the message (except storing it in shared memory, but i'd like to leave it as last choice), so i should made, from inside the child process, the assert regarding the message:
    Code:
     /*The consumer has taken the right message from the buffer*/
    Code:
    CU_ASSERT_STRING_EQUAL(content_1, (char*)(*rec_msg).content);
    Code:
      /*The buffer still contains a copy of the string*/
    Code:
    CU_ASSERT_STRING_EQUAL(content_1, buffer->msgbuffer[buffer->D].content);
    if i try to strcmp or print the strings everything works, but it's not the way to do it; i have to use Cunit, but he seems to simply ignore my asserts, when called from child process. if i have 100 asserts in the parent process and 10 in the child process, the total amount of asserts verificated is 100.how can i solve this issue?
    Last edited by DeliriumCordia; 12-19-2013 at 04:19 AM.

  2. #2
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    sorry for the bad text formattation of my post, but it seems that posting deletes all kind of spacing, that's also the reason i had to make 4 different box of code!

  3. #3
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    seems no-one knows how to do it! without opening a new thread, does someone instead know how to wait for ALL children processes to exit? without status too, i need to do something like:as main process, when ALL the children process are terminated, do something.wait(NULL) seems to have a different behaviour, and at the state of the art, all i can do is a list of waitpid(pid, NULL, 0) for all the children processes.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Never messed with CUnit, and haven't had time to grab a copy. It would help us immensely if you could provide the code you're testing with the asserts, or a small, compileable example that shows the problem. It's not clear to me exactly what you're testing. It's not even clear why you must fork to a child process. I think you may actually be stepping outside the scope of unit testing, so CUnit or other unit testing frameworks may not actually be able to help you here.

    That being said, if you want to wait for all the children to finish, then wait/waitpid are your best bet. You can read the man page for all the details. waiting for a pid of -1 may be the easiest. Just do that N number of times, where N is how many child processes you have. Make sure you check the status returned by wait(), to ensure that the signal was not from a stopped/resumed child process.

  5. #5
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    i need to fork() pretty often to create producers\consumers in a concurrent program.

    by the way, it's getting pretty strange: in any part of the code if i try to make asserts from within the child processes nothing happens, it just goes over them.
    Code:
    #include "CUnit/Basic.h"
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <sys/sem.h>
    #include <sys/stat.h>
    #include <sys/ipc.h>
    
    int init_suite1(void)
    {
      return 0;
    }
    
    
    int clean_suite1(void)
    {
       return 0;
    }
    
    
    void test1() {
    
       pid_t tester_process;
       
       CU_ASSERT(1==1);
    
       if((tester_process = fork()) == 0) { //child process
       
       CU_ASSERT(1==1); //not execute
       CU_ASSERT(2==2);
       exit(0);
       }
       waitpid(tester_process, NULL, 0);
    }
    
    int main()
    {
       CU_pSuite pSuite = NULL;
    
       /* initialize the CUnit test registry */
       if (CUE_SUCCESS != CU_initialize_registry())
          return CU_get_error();
    
       /* add a suite to the registry */
       pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
       if (NULL == pSuite) {
          CU_cleanup_registry();
          return CU_get_error();
       }
    
       /* add the tests to the suite */
          if ((NULL == CU_add_test(pSuite, "test for forks", test1)))
       {
          CU_cleanup_registry();
          return CU_get_error();
       }
    
       /* Run all tests using the CUnit Basic interface */
       CU_basic_set_mode(CU_BRM_VERBOSE);
       CU_basic_run_tests();
       CU_cleanup_registry();
       return CU_get_error();
    }
    remember to compile adding -lcunit at the end to import cunit library.
    Last edited by DeliriumCordia; 12-20-2013 at 12:01 PM.

  6. #6
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    obviously just check inside the test1, the remaining is just test setup.

    by the way i found a strange side effect that i hardly understand. i really didn't expect the following behaviour.

    if i delete the condition exit(0) from inside the child process, i don't get anymore:

    --Run Summary: Type Total Ran Passed Failed
    suites 1 1 n/a 0
    tests 1 1 1 0
    asserts 1 1 1 0



    but i get instead:

    Suite: Suite_1
    Test: test for forks ... passed

    --Run Summary: Type Total Ran Passed Failed
    suites 1 1 n/a 0
    tests 1 1 1 0
    asserts 3 3 3 0
    passed

    --Run Summary: Type Total Ran Passed Failed
    suites 1 1 n/a 0
    tests 1 1 1 0
    asserts 1 1 1 0



    that's really out of logic:

    its like if 2 different suites get executed, and the one with all the tests gets suppressed when the child process dies. but if the child process could raise a separate suite it would have 2 asserts instead of 3... seeing it like that it's as if the parent process continues a separate test suite, and the child process sets up a new one, inheriting old suites from his parent...

    like that i can't really do it, i need the child processes to exit, and i should find a way to pass these tests to the parent process -> that's really inefficient and ugly to see, i need to find a way to be able to make these asserts from the parent process, so i will have to deal with shared memory to get access the results of the operations and make my asserts, i suppose.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Quote Originally Posted by DeliriumCordia View Post
    Code:
       if((tester_process = fork()) == 0) { //child process
       
       CU_ASSERT(1==1); //not execute
       CU_ASSERT(2==2);
       exit(0);
       }
       waitpid(tester_process, NULL, 0);
    Note that when you remove the exit(0) you end up calling waitpid for both child and parent processes here.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by DeliriumCordia View Post
    i found a strange side effect that i hardly understand. i really didn't expect the following behaviour. if i delete the condition exit(0) from inside the child process, i don't get anymore:
    When you fork the program, you end up with two copies of the assert counter, one in the parent and one in the child. When the child calls CU_ASSERT it updates its own assert counter, but not the one in the parent. When the child exits, its copy is destroyed.


    But when you remove the exit(0) and let the child fall through, then both the child and parent print out stats, the child printing 3 asserts and the parent printing 1.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    yes, what surprised me a little was to find 3 asserts on the child instead of 2, like if the child process inherited old asserts from the parent.by the way finding a workaround of this kind doesn't seems to be the way to do it, i will have to find a way to make asserts always from the parent process.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 01:14 PM
  2. CreateRemoteThread in a child process
    By electrohippi in forum Windows Programming
    Replies: 7
    Last Post: 11-24-2008, 09:15 AM
  3. Child process I/O
    By madmardigan53 in forum Windows Programming
    Replies: 6
    Last Post: 08-23-2004, 10:40 PM
  4. Child Process & Parent Process Data :: Win32
    By kuphryn in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 12:19 PM
  5. child process
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-31-2002, 03:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21