Thread: Switch process

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    134

    Switch process

    I have this code
    Code:
    #include  <stdio.h>
    #include  <sys/types.h>
    
    #define   MAX_COUNT 200
    
    void  ChildProcess();
    void  ParentProcess();
    
    int  main()
    {
        pid_t  pid;
        pid = fork();
        if (pid == 0)
             ChildProcess();
        else
             ParentProcess();
        return 0;
    }
    
    void  ChildProcess() {
        int   i;
        for (i = 1; i <= MAX_COUNT; i++)
             printf("   This line is from child, value = %d\n", i);
        printf("   *** Child Process is done ***\n");
    }
    
    void  ParentProcess() {
        int   i;
        //wait();
        for (i = 1; i <= MAX_COUNT; i++)
             printf("This line is from parent, value = %d\n", i);
        printf("*** Parent Process is done ***\n");
    }
    I have three queries:

    1. why child process starts first?
    2. Why shometime it prints upto 70(i's value) from child then 200 from parent and then switch again to parent, is it due to the service time from the processor handeled by OS?
    3. How I can print alternatively from both process:
    like first 1 from child, then 1 from parent then two from child and then 2 from parents and so on?
    Is there any way to switch between the processes? Child and Parents.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    23
    1. why child process starts first?
    when you use fork, the child process is always started first...

    2. Why shometime it prints upto 70(i's value) from child then 200 from parent and then switch again to parent, is it due to the service time from the processor handeled by OS?
    after the fork() call the child process becomes an independent process and thus, is scheduled interdependently with other processes for CPU time. the exact time alloted to each process and their relative scheduling is platform dependent. you cannot directly control that...

    3. How I can print alternatively from both process:
    use semaphores or mutexes...

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    This is not a C programming question - it is a unix programming question. Suggest moving to a more appropriate forum than "C programming".

    That said ....

    Quote Originally Posted by kapil1089thekin View Post
    1. why child process starts first?
    It doesn't. The parent process starts the child process. The visible output seen will depend on how the operating system allocates processor time to each process.

    Quote Originally Posted by kapil1089thekin View Post
    2. Why shometime it prints upto 70(i's value) from child then 200 from parent and then switch again to parent, is it due to the service time from the processor handeled by OS?
    Look up "operating system scheduler". Essentially, the scheduler allocates slices of processor time to every active process (or thread). The duration of the slices, and the order in which any process receives a time slice, depend on the scheduling algorithm and characteristics of each process (eg assigned priority, how much demand it makes for processor time).
    Quote Originally Posted by kapil1089thekin View Post
    3. How I can print alternatively from both process:
    like first 1 from child, then 1 from parent then two from child and then 2 from parents and so on?
    Look up synchronization (mutexes, semaphores, etc). They provide a global means by which programs can explicitly align their operations - for example, one process waits for a mutex to be released before doing something. The mutex can therefore be used - cooperatively - by both processes as a means of signalling the other can do things.

    Quote Originally Posted by kapil1089thekin View Post
    Is there any way to switch between the processes? Child and Parents.
    Not in the way you mean, I suspect. It is possible to synchronise actions by the processes, and it is possible for one process to send a signal that the other responds to.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    23
    Originally Posted by grumpy
    It doesn't. The parent process starts the child process. The visible output seen will depend on how the operating system allocates processor time to each process.
    yes, the parent process starts the child, but just after the call to fork() the child executes first. the reason is to enable safe copy-on-write.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That actually depends on how the operating system implements the fork() function.

    Different flavours of unix do it in different ways though. There is plenty of room for interpretation in (say) the posix spec.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Capturing stderr from an execv()ed process
    By Angus in forum Linux Programming
    Replies: 1
    Last Post: 11-10-2009, 01:43 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. restarting process
    By Bleech in forum Windows Programming
    Replies: 3
    Last Post: 11-29-2007, 02:07 AM
  4. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  5. Help needed in creating a process
    By sac_garg in forum C Programming
    Replies: 3
    Last Post: 10-01-2006, 01:40 AM