GithubHelp home page GithubHelp logo

zhadavla / ft_pipex Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 1.36 MB

Project aimed at studying how to work with multiple processes in C using the pipe and fork functions. Check subject for details.

C 99.05% Makefile 0.95%

ft_pipex's Introduction

ft_pipex with the bonus part

Project aimed at studying how to work with multiple processes in C using the pipe and fork functions.

Just run make to compile it.

Program will be executed as follows: ./pipex file1 cmd1 cmd2 cmd3 ... file2 by cmd1 cmd2 cmd3 meaning any number of shell commands. It must take at least 4 arguments:

  • file1 and file2 are file names.
  • cmd1 and cmd2 are shell commands with their parameters.

It must behave exactly the same as the shell command below: $> < file1 cmd1 | cmd2 > file2

Bonus part

You will get extra points if you:

  1. Handle multiple pipes. This: ./pipex file1 cmd1 cmd2 cmd3 ... cmdn file2 Should behave like: < file1 cmd1 | cmd2 | cmd3 ... | cmdn > file2
  2. Support « and » when the first parameter is "here_doc". This: ./pipex here_doc LIMITER cmd cmd1 file Should behave like: cmd << LIMITER | cmd1 >> file

Approach

  1. Create and initialize t_pipex struct that will store following information:
typedef struct s_pipex
{
	int		fd_pipes_count; // amount of FDs for all pipes (cmd_count - 1) * 2
	int		cmd_count; // number of commands
	int		infile_fd;
	int		outfile_fd;
	int		*fd_pipes; // array of pipes' FDs
	int		fd_ind; // index of a pipe
	int		is_heredoc;
	char	*infile_name;
	char	*outfile_name;
	char	*limiter;
}			t_pipex;
  1. Create pipes. (using fd_pipes_count):
int	create_pipes(t_pipex *pipex)
{
	int	i;

	i = 0;
	while (i < pipex->fd_pipes_count)
	{
		if (pipe(pipex->fd_pipes + i) == -1)
		{
			free(pipex->fd_pipes);
			return (-1);
		}
		i += 2;
	}
	return (1);
}
  1. Create list of commands together with parameters;
  2. Execute command:
    • we do fork() for every command --> while (--cmd_count) and increment fd_ind by 2 --> fd_ind+=2;
    • here we have separate do_fork functions for first command, middle commands, and last command;
  3. Forking:
    • we fork and go into the child process (fork() == 0).
    • inside child process we redirect the input and output using ft_dup2(input_file_fd, output_file_fd) function:
    int	ft_dup2(int fd1, int fd2)
     {
       if (dup2(fd1, STDIN_FILENO) == -1)
         return (-1);
       if (dup2(fd2, STDOUT_FILENO) == -1)
         return (-1);
       return (1);
     }
  • after the redirection, we close all the open FDs inside the child process:
void	close_fd(t_pipex *pipex)
{
	int	i;

	i = 0;
	while (i < pipex->fd_pipes_count)
	{
		close(pipex->fd_pipes[i]);
		i++;
	}
}
  • and then we execute the command itself with execve still inside the child process.
  1. After the execution of all the commands (while (--cmd_count) --> cmd_count == 0), we are back to the parent process:
  • close all pipes in parent process:
    void	ft_close(t_pipex *pipex)
    {
    	close_fd(pipex);
    	if (pipex->infile_name)
    		close(pipex->infile_fd);
    	close(pipex->outfile_fd);
    }
  • wait(&status) - waiting for the child processes to end.
  • free memory;

ft_pipex's People

Contributors

zhadavla avatar

Stargazers

Dmytro Starov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.