Thread: Basic C question ---- URGENT

  1. #1
    Registered User
    Join Date
    Mar 2006

    Basic C question ---- URGENT

    Can someone please help me with this question, I know its pretty basic but i just cant seem to get my head around it???

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    int varA = 40;
    int main(int argc, int **argv)
        int varB = 29;
        pid_t retFork=2;
        fprintf(stderr, "PID: %4ld  retFork:%2d  varA: %2d  varB: %2d\n", getpid(), retFork, varA, varB);
        retFork = fork();
        varA = 80;
        fprintf(stderr, "PID: %4ld  retFork:%2d  varA: %2d  varB: %2d\n", getpid(), retFork, varA, varB);
        switch (retFork){
        case -1:  
            fprintf(stderr, "The fork system call failed\n" );
            fprintf(stderr, "PID: %4ld  retFork: %2d\n", getpid(), retFork);
        case 0:
            varA += 10;
            varB += 10;
            fprintf(stderr, "PID: %4ld  retFork: %2d\n", getpid(), retFork);
            fprintf(stderr, "PID: %4ld  retFork: %2d\n", getpid(), retFork);
            varA += 25;
            varB += 25;
    sleep (2.0);
        fprintf(stderr, "PID: %4ld  varA: %2d  varB: %2d\n", getpid(), varA, varB);
        return 0;

    PID: 32613 retFork: 2 varA: 40 varB: 29
    PID: 32614 retFork: 0 varA: 80 varB: 29
    PID: 32614 retFork: 0
    PID: 32614 varA: 90 varB: 39
    PID: 32613 retFork:32614 varA: 80 varB: 29
    PID: 32613 retFork: 32614

    ii. Observe that the values of the three variables retFork, varA and varB change. Explain the reason for the change to the values of variables retFork, varA and varB.

    plzzzz can someone help??


  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Sydney, Australia

  3. #3
    Registered User
    Join Date
    Mar 2006
    Bangalore, INDIA
    retFork = fork();
    This statement created a child process and hence the function fork() returns 0 as the child process is going to shoot out first (not always, but in ur case this is true).
    varA = 80;
    Here u have changed the value of the variable so now the value of varA=80 in the child process.

    switch (retFork){
    The value of the retFork is 0 so now the case o is going to execute and the hence varA=90 (varA+=10 is same as varA=varA+10) and similarly varB=39. The PID it prints is the PID of the child process and retFork=0 as already said.

    You have printf statement after sleep(), even at this point the child process was running so it printed the value of varA as 90 and varB as 39.

    Now the child process ended and so again parent process starts continuing and so the retFork=for() returned the PID of the child process which is 32614. so retFork value changed to 32614. The value of varA and varB were 40 and 29 at this point because the previously changed value was in child process and not in parent. the next statement is varA=80 so the value of varA changed to 80.

    so the switch(retFork) runs the case "default". so the value of retFork = 32614 and getpid()=32613 is printed.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Basic C question ---- URGENT?
    Nothing urgent about it, and nothing basic about it. This is advanced IMO
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  2. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  3. Visual Basic Question?
    By ob1cnobe in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2002, 09:31 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM