0

I am trying to accepts two integers (say low and high) as command line argument and in my main program is trying to call two other programs. program-1 should calculate the summation of all integers between (low, high) as sum_res and program-2 should evaluate whether sum_res is prime or not.

So I was trying to create two processes and I want to share a common variable between two processes but after execution I checked that only my main program is giving me segmentation fault.

I am new to this concept of execl please help:

My main program:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h> 
#include <string.h>

int sum_res=0;

int main(int argc, char *argv[])
{
    int low = atoi(argv[1]), high = atoi(argv[2]);
    pid_t pid;
    if((pid=vfork())==0)
    {
        execl("pro1","pro1", low, high, &sum_res, (char *)NULL);
        exit(0);
    }
    else if(pid > 0)
    {
        wait(NULL);
        execl("pro2","pro2", sum_res, (char *)NULL);
        exit(0);
    }
    
    return 0;
}

My program-1 is: (Named prog1.c and compiled as gcc -g prog1.c -o prog1)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    int n1 = atoi(argv[1]), n2 = atoi(argv[2]), i, sum_res = (int *)(argv[3]);
    for(i=n1; i<=n2; i++)
    {
        (*sum_res)+=i;
    }
    printf("Sum is : %d\n", *sum_res);
    
    return 0;
}

My program-2 is: (Named prog2.c and compiled as gcc -g prog2.c -o prog2 -lm)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int *main(int argc, char *argv[])
{
    int sum_res = atoi(argv[1]), i, c=0;
    for(i=2; i<=sqrt(sum_res); i++)
    {
        if(sum_res % i == 0)
        {
            c++;
            break;
        }
    }
    if(c==0)
    {
        printf("Prime \n");
    }
    else printf("Not Prime \n");
    
    return 0;
}

Note: All 3 programs and their respective executables are present in the same current working directory.

If this is not possible then how will i get the summation result from program-1 into program-2 ?

  • 2
    `execl` takes a number of pointers to strings. None of `&sum_res`, `sum_res`, `high` or `low` are suitable for that purpose. As command line interface is dealing with strings, what makes you think you could pass numerical values directly or pointers? Any address from one process does not have any meaning in another process. If you want to use command line arguments, pass them as text and read output as text. – Gerhardh Jun 11 '22 at 10:29
  • Compile with [GCC](https://gcc.gnu.org/) invoked as `gcc -Wall -Wextra -g` – Basile Starynkevitch Jun 11 '22 at 10:44

1 Answers1

1

It makes sense that you receive a segmentation fault. A pointer is a memory address inside a single process memory space. Each process memory space is completely independent and separated. This prevents one programs from accidentally breaking another program. When you try to read or write outside your process address space (the memory "segment" for your process) you receive a segmentation fault.

If you want to share memory space between two processes you need to use an IPC (Inter-process Communication) libraries to enable sharing memory space. One way is the shm_open function: https://www.geeksforgeeks.org/posix-shared-memory-api/

juz
  • 23
  • 5