I want to run two different main processes

79 Views Asked by At

I want to kill the 1st process then I wan to run the 2nd process. how can I do that?

I want to do in embedded linux, How can I do that? I have no idea. Any sample example would be helpful. The 2nd process should run independently, there should not be any relation between 1st and 2nd process.

1

There are 1 best solutions below

2
Terraminator On

You can use fork to create a child process and then do stuff in both processes. You don't need multiple processes for everything: you might want to read about threading vs multiprocessing.

This is an example for your scenario using fork:

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

int main() {
    pid_t pid = fork();
    if(pid == -1) {
        perror("fork");
        return 1;
    }

    if(pid == 0) {
        puts("hello from child process!");
    }
    else {
        puts("hello from parent process!");
    }
}