How to create a new process in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
int main(void) {
 
    pid_t pid = fork();
    if (pid == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }
 
    if (pid == 0) {
        printf("child process - %d\n", getpid());
    } else {
        printf("parent process - %d\n", getpid());
    }
 
    exit(EXIT_SUCCESS);
}
 
 
 
  
  
  
/*
run
  
parent process - 19
child process - 20
  
*/

 

 



answered May 1, 2021 by avibootz
edited Jun 12, 2023 by avibootz

Related questions

2 answers 246 views
1 answer 146 views
146 views asked Jun 12, 2023 by avibootz
1 answer 187 views
1 answer 262 views
1 answer 248 views
248 views asked Jun 30, 2019 by avibootz
...