user12140617
user12140617

Reputation:

Difference between a process and a regular function call?

I was just introduced to the idea of a process.

The book defines a process as "an instance of the running program".

  1. I am still a little confused as to what this means. It seems to me that a process is a particular instruction that a program is running? Or not?

  2. What is the difference between a function call and a process? For instance let us say we have a function called main, and within it we are calling the printf function. Does printf count as a separate process? Why/why not?

  3. What makes something a child vs parent process? I know that one way to create child processes is by calling fork(). And then based on the integer value that fork returns, we can either be in the child vs in the parent process. But other than is there something that makes something a parent vs a child process?

  4. Also based on the answer on question 2, would the printf count as a child process?

Upvotes: 1

Views: 1098

Answers (1)

naivecoder
naivecoder

Reputation: 116

Talking strictly in terms of linux processes are "instances" of the programs as the book mentions. That means that they contain the information that your program needs to "execute".

  1. The process doesn't mean the instruction that the program is running, it means the entire running program. The program you are referring to is I am assuming the code that you write, but that is just one aspect of the process. There are various other attributes like the stack memory space, heap memory space and process ID etc. and all these details are stored in a datastructure called process control block(PCB). Suppose you have a compiled version of your code "Fibonacci.c" called fibonacci, if you run it from two different terminals it would spawn "two processes" of the same program.

  2. Function calls are something that happen inside a process. printf would happen in the same function. It doesn't count as a separate process as it is executing inside the same entity.

  3. fork can create child processes. As a rule of thumb I would say that any process that is created inside our current process would be a child process. Though this might not be a strict definition. What fork does is duplicate the current process, that means that it creates a new entry by creating a new PCB. It has the same code segment as the process that calls the fork but it will have its own memory space, process ID etc. I will not go deeper into how memory is handled when a fork occurs but you can read more about it in the man pages.

  4. printf also is not a child process. It resides in the current process itself.

Upvotes: 1

Related Questions