Creating child process in C linux [closed]

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); 

Leave a Comment