PDA

View Full Version : Understanding the Difference Between a Process and a Program in OS with a Code Exampl



mark01
10-16-2023, 05:48 AM
I'm studying operating systems, and I'm trying to grasp the concept of processes and programs. I understand that a program is an executable file, while a process is the execution of that program. However, I'd like a more detailed explanation with a code example to illustrate the difference.


Could someone provide a code example in C that demonstrates the distinction between a program and a process? Specifically, I'd like to see how a single program can result in multiple processes when executed concurrently, and how these processes share or don't share memory and resources.


Here's a simple C code snippet that calculates the factorial of a number:


#include <stdio.h>


int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}


int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}



Could someone modify this code to create multiple processes that calculate the factorial of different numbers concurrently? Additionally, please explain how these processes differ from the original program, especially in terms of memory and resource allocation. Since this idea is crucial to operating systems, I'm seeking for a practical knowledge of it. I've also been to a few websites like Scaler (https://www.scaler.com/topics/difference-between-process-and-program/). We would value any new information on how operating systems control programs and processes.

June7
10-16-2023, 11:26 AM
A process is just programming. There is no 'concurrently' - programs do one thing at a time. I don't know C well enough to provide specific code but the logic would be:

in a loop that reads number inputs (by prompting a user or pulling from dataset or hard-coded) call the function and output the result somewhere

I use VBA and this loop with hard-coded inputs could look like:

Sub Main()
For x = 1 to 3
Debug.Print Factorial(x)
Next
End Sub

So where you have int num = 5, the 5 would be a variable input

Do you know how to code loop in C (C++ or C#) https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements? Where do you want the inputs to come from?

gregbowers
06-02-2024, 01:57 AM
Hello

A program is a static set of instructions in an executable file. A process is an active instance of a program that's running and consuming resources (like memory and CPU).
When you run a program, the OS creates a process. You can create multiple processes from the same program using fork(), like in the modified C code below:


#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}

int main() {
int nums[] = {3, 4, 5, 6};
int num_processes = sizeof(nums) / sizeof(nums[0]);

for (int i = 0; i < num_processes; i++) {
pid_t pid = fork();

if (pid == 0) {
printf("Factorial of %d is %d (process %d)\n", nums[i], factorial(nums[i]), getpid());
return 0;
} else if (pid < 0) {
perror("Fork failed");
return 1;
}
}

for (int i = 0; i < num_processes; i++) {
wait(NULL);
}

return 0;
}

Here, fork() creates new processes. Each child process calculates the factorial of a different number. Processes run concurrently and have their own memory space, so they work independently. The parent process waits for all child processes to finish. This demonstrates how a single program can spawn multiple processes, each with separate resources.

Aussiebear
06-02-2024, 02:17 AM
Thank you gregbowers, however we are digressing as this forum is based on VBA and not C.