-1

"In what logical segments of the processes do the variables f and c exist?"

int c = 5;
    void main(int argc, char **argv)
    {
        int f = fork();
        if(f == 0)
        {
            c += 5;
        }
        else
        {
            f = fork();
            c += 10;
            if(f)
            {
                c += 5;
            }
        }
    }

Hi guys I'm kind of stuck on this question, I guess that c is a global variable and is in the data region and that f is in the stack (as main is a procedural call), but I am not clear on what is meant by logical segments. I would greatly appreciate an expert eye to look over this question and tell me what I am missing. Thanks in advance.

mohelt
  • 55
  • 2
  • 6
  • I don't think this question necesarilly deserves two downvotes, but it would help if the code was formatted to be readable. I'd assume that what is meant by logical segments would be explained in whatever course you're taking, since what's exactly meant can vary. However, the most common selection of options would probably be text/code, data, (maybe bss,) stack, and heap. – Thomas Jager Mar 13 '21 at 15:00
  • You have it solved here https://www.chegg.com/homework-help/questions-and-answers/anyone-explain-give-depth-explanation-thoery-asked-question-q45433959 – Risinek Mar 13 '21 at 15:18
  • The answer is not specific to the C language, it's specific to the destination architecture. Maybe klingon (or quantum) computers have POSIX compatability :-) – pmg Mar 13 '21 at 15:42

1 Answers1

0

Initial c variable lives only in the main process (in the f == 0 branch). Everytime you fork, it will be cloned, but every process has it's own copy of c variable.

See this answer https://stackoverflow.com/a/4299626/3035795

And global variables are stored in data segment like you said, so the answer is c exists in data segment, f exists in stack segment.

See https://stackoverflow.com/a/14588866/3035795

Risinek
  • 390
  • 2
  • 16