processexecvfork

Multiple Processes with vfork()


I'm trying to run multiple processes on a SmartFusion2 SOM running uClinux, but I'm only able to use vfork() and not fork() on it. I've been trying to run the following code to test out running multiple processes, but I'm not getting the results I want. The code is supposed to run two different programs simultaneously, but I'm getting a SEGV fault.

Here's the code:

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

int main(){
  pid_t pid;

  pid = vfork();
  if(pid > 0){
    printf("I am the parent of pid = %d\n", pid);
    execve("/home/path/to/executable2", NULL, NULL);
  }
  else if (!pid){
    printf("I am the baby\n");
    execve("/home/path/to/executable1", NULL, NULL);
  }
  else if (pid == -1){
    perror("fork");
  }
  return 0;
}

It compiles jut fine, but my output looks like this:

I am the baby
I am the parent of pid = 140
SEGV

Can someone help me see what I'm doing wrong?


Solution

  • Turns out my code was right, but the file path I was using was the directory from root on my computer, and not the same directory when running off the SmartFusion2. But since I didn't post the file path in the above code, it should work just fine for whomever.