Creating a child process and pipe C

Gagan GGagan G
2 min read

Child Process

A child process is a duplicate of the parent process(it it has the same program and data as the parent). The child process can have a new program loaded onto it. A parent process can continue to execute parallelly as the child process or it can wait using the wait function. pid=fork(); here pid is the process id. execlp(“ls”,”ls”,”-l”,NULL); it executes the program and 1st ls is the function it has to perform, or it can have the address of the new seperate program which can be assigned to the child process. 2nd ls has the name of the child process, -l is the argv[0] for the child process. If child process executes it returns NULL. wait(NULL) waits until the child process executes.

Here is a program illustrating the creation of a child process which is used to execute another program.

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include <sys/wait.h>
int main(){
        pid_t pid;//store pid
        pid=fork();//create child
        if(pid<0){
                fprintf(stderr,"fork failed");
                return 1;
        }
        else if(pid==0){
                execlp("gcc","gcc","program.c","-o","program",NULL);
                perror("exec failed");
        }
        else{
        wait(NULL);
        execl("./program","./program",NULL);
         perror("exec failed");
        }
        return 0;
}

here is the program that this program executes.

#include<stdio.h>
int main(){
        printf("Hello World");
        return 0;
}

Pipe’s

A pipe’s act as a conduct which allows 2 process to communicate. Ordinary pipes allow 2 process to communicate in standard producer-consumer fashion. The producer writes(write end) to one end of the pipe and the consumer reads form the other end(read end). pipe(int fd[]) allows to create a pipe. This is a type of interprocess communication. The other 2 types allow 2 different process to communicate. 1. shared memory- Memory space shared between 2 different process. 2.Messege passing- Directly sending messages to other processes. I will edit this blog or upload a new blog showing these 2 types.

Here is a program illustrating how pipes work by creating a child process.

#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc,const char *argv[]){
        int fd[2];//0 for read end,1 for write end
        pid_t pid;
        char message[100];
        int n=1;
        while(n!=argc){
                strcat(message,argv[n]);
                n++;
        }
        char buffer[100];
        if(pipe(fd)==-1){
                perror("pipe");
                return 1;
        }
        pid=fork();
        if(pid<0){
                perror("fork");
                return 1;
        }
        if(pid==0){
                close(fd[1]);
                read(fd[0],buffer,sizeof(buffer));
                printf("recieved:%s\n",buffer);
                }

        else{
                close(fd[0]);
                write(fd[1],message,strlen(message)+1);
        }
        return 0;
}

Hope you learnt something new today.

Happy Coding…

0
Subscribe to my newsletter

Read articles from Gagan G directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gagan G
Gagan G

I'm passionate about coding and enjoy working with embedded systems, app development, and compiler design.