Creating child process in C linux

European Academy

Create child process by using fork() function.

The parent process runs change content of the process by execl() function which run cat f1.c command.

child process runs a traceroute www.google.com command.

TheEngineer

Before asking questions here, please try it on your own and post what you have tried so far so we can guide you in the right direction. Also, it would be nice if you put more effort in asking better question. But to give you some guidance:

you can create child process by using fork. It returns an integer. If it is zero, that means you are in the child process. so you can do something like:

    int pid;
        if((pid=fork())==0){
          // you are in child process
          //use execl(constant char *path, constant char *commands); to run your commands
    }
    else {
          //whatever you need to do in the parent process
}

You can find about execl() here :https://www.systutorials.com/docs/linux/man/3-execl/ It is basically a way to run a command. The first argument is a constant char pointer which points to the shell that you want to run the command in ("/bin/sh" etc.). The next arguments are the command it self ("cd", "mydir" etc.) terminated with null.

execl("/bin/sh","cd","mydir",NULL); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

creating child process of a child process in unix

From Dev

Can a child process wait for the parent process to terminate in Linux programming in C?

From Dev

There was an error creating the child process for this terminal

From Dev

How to get all descendent child process id of pid in c in linux

From Dev

How to detect file activities of exec'ed child process on Linux in C?

From Dev

C - Creating child processes

From Dev

Restrict child process 's cpu and ram usage by parent process using C/C++ in Linux

From Dev

creating child process with MPI_Comm_spawn

From Dev

creating child process with MPI_Comm_spawn

From Dev

creating pipe between father and child process

From Dev

Linux pass value between parent process and child process using pipe in c?

From Dev

Linux pass value between parent process and child process using pipe in c?

From Dev

Process.Start without creating a child process (port handle inheritance)?

From Dev

Process.Start without creating a child process (port handle inheritance)?

From Dev

Is it OK to wait on a child process from a thread different to the one creating the process?

From Dev

When creating a child process is it necessary to copy data from the parent process to the child process?

From Dev

Creating a background process for shell in c

From Dev

Waiting for child process to terminate, or not - C

From Dev

C Programing : Parent & Child Process

From Dev

Reading file in child process in C

From Dev

Reading file in child process in C

From Dev

Memory usage of child process in C

From Dev

Creating a process in Linux with a different mount namespace

From Dev

Linux, waitpid, WNOHANG, child process, zombie

From Dev

Linux: fork & execv, wait for child process hangs

From Dev

Set various limits on child process programmatically (Linux)

From Dev

Windows/Linux child process STDIN differences

From Dev

Malloc kills child process in Linux environment

From Dev

How to IPC with the parent process when creating child processes in a loop (Ruby)

Related Related

  1. 1

    creating child process of a child process in unix

  2. 2

    Can a child process wait for the parent process to terminate in Linux programming in C?

  3. 3

    There was an error creating the child process for this terminal

  4. 4

    How to get all descendent child process id of pid in c in linux

  5. 5

    How to detect file activities of exec'ed child process on Linux in C?

  6. 6

    C - Creating child processes

  7. 7

    Restrict child process 's cpu and ram usage by parent process using C/C++ in Linux

  8. 8

    creating child process with MPI_Comm_spawn

  9. 9

    creating child process with MPI_Comm_spawn

  10. 10

    creating pipe between father and child process

  11. 11

    Linux pass value between parent process and child process using pipe in c?

  12. 12

    Linux pass value between parent process and child process using pipe in c?

  13. 13

    Process.Start without creating a child process (port handle inheritance)?

  14. 14

    Process.Start without creating a child process (port handle inheritance)?

  15. 15

    Is it OK to wait on a child process from a thread different to the one creating the process?

  16. 16

    When creating a child process is it necessary to copy data from the parent process to the child process?

  17. 17

    Creating a background process for shell in c

  18. 18

    Waiting for child process to terminate, or not - C

  19. 19

    C Programing : Parent & Child Process

  20. 20

    Reading file in child process in C

  21. 21

    Reading file in child process in C

  22. 22

    Memory usage of child process in C

  23. 23

    Creating a process in Linux with a different mount namespace

  24. 24

    Linux, waitpid, WNOHANG, child process, zombie

  25. 25

    Linux: fork & execv, wait for child process hangs

  26. 26

    Set various limits on child process programmatically (Linux)

  27. 27

    Windows/Linux child process STDIN differences

  28. 28

    Malloc kills child process in Linux environment

  29. 29

    How to IPC with the parent process when creating child processes in a loop (Ruby)

HotTag

Archive