[LINUX] Container-like # 1 made with C

Introduction

This article is the 22nd article of FuraIT Advent Calendar. Sorry for being late. I thought yesterday was the 22nd.

Also, it will be similar to the content announced at here. Maybe it's more interesting when you announce it, so if you are interested, please join us.

It has nothing to do with FuraIT, but I will continue without worrying about it.

environment

I will make

With "# 1", this time I would like to do the part that handles fork, exec, and wait in C language. I don't know if I will continue.

fork First you need to fork the process. There are also Linux-specific libraries, so if you are using Windows, please do something about it yourself.

#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>

int main(){
  pid_t pid;

  if ((pid = fork()) < 0){
    perror("fork");
    return 1;
  } 
  //Processing on the child process side
  else if (pid == 0) {
    printf("child process:%d\n",(int)getpid());
    _exit(0);
  }
  printf("parent proces:%d\n",(int)getpid());
  return 0;
}

Execution result

parent proces:28540
child process:28541

It was like this. ~~ The processing on the parent process side is executed first. ~~

If you look at Man page of FORK, you can see it. If the return value of fork is a positive value, it is the child process ID. If 0, it is returned to the child process as a successful fork. Therefore, by specifying pid == 0, you can perform processing unique to the child process.


exec Now that we've successfully called the child process, it's time to actually run the external program inside the child process. To execute an external program, it is necessary to call an exec system call. This time I decided to use execl.

#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>

int main(){
  pid_t pid;

  if ((pid = fork()) < 0){
    perror("fork");
    return 1;
  } 
  //child process
  else if (pid == 0) {
    printf("child process:%d\n",(int)getpid());
    execl("/bin/bash","",NULL);
    _exit(0);
  }
  printf("parrent proces:%d\n",(int)getpid());
  return 0;
}

Man page of EXEC  The explanation of the exec system will be long, so I will omit it.

~~ I ran / bin / bash from a child process but nothing happens. This is because the child process dies when the parent process terminates. Therefore, the parent process must wait for the child process to finish. ~~


wait You can wait for the child process to terminate using a wait-type system call. This time I used waitpid. Don't forget to include sys / wait.h.

#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdio.h>

int main(){
  pid_t pid;
  int status;

  if ((pid = fork()) < 0) {
    perror("fork");
    return 1;
  } 
  //child process
  else if (pid == 0) {
    printf("child process:%d\n",(int)getpid());
    execl("/bin/bash","",NULL);
    perror("bash");
    _exit(1);
  }
  
  printf("parrent proces:%d\n",(int)getpid());
  if ((pid = waitpid(pid,&status,0)) < 0) {
    perror("wait");
    return 1;
  }
  if (WIFEXITED(status)) {
    printf("pid:%d status:%d\n",(int)getpid(),WEXITSTATUS(status));
  }
  return 0;
}

You can see everything by looking at the Man page of WAIT. waitpid is the process ID that waits for the end, the variable that puts the exit status, and the third option is option (it doesn't matter now). WIFEXITED () is true when the child process exits, and WEXITSTATUS () can retrieve its exit status. As for the execution result, use the ps command to confirm that the ID of the parent process and the ID of the child process exist. At this point, you can check the operation using the chroot command, but it should be introduced in'# 2', so please wait.

the end!

'# 1'ends here. I would like to write'# 2'when I feel like it. Please feel free to contact me if there are any mistakes or confusing points.

Where I referred

http://chikuwait.hatenablog.com/entry/2017/10/05/001126

Recommended Posts

Container-like # 1 made with C
Container-like # 2 made with C
Debugging C / C ++ with gdb
Pomodoro timer made with Errbot
Heapsort made in C language
ABC163 C problem with python3
Try Google Mock with C
I made blackjack with python!
SNS Flask (Ajax) made with Flask
Format C source with pycparser
I made COVID19_simulator with JupyterLab
I made Word2Vec with Pytorch
I made blackjack with Python.
ABC188 C problem with python3
Othello made with python (GUI-like)
I made wordcloud with Python.
ABC187 C problem with python
SNS Flask (Model) edition made with Flask
I made a C ++ learning site
Solve ABC163 A ~ C with Python
SNS Python basics made with Flask
Create Awaitable with Python / C API
Writing C language with Sympy (metaprogramming)
Numer0n with items made in Python
I made a fortune with Python.
Twitter posting application made with Django
Solve ABC168 A ~ C with Python
Othello game development made with Python
SNS made with Flask Flask (Blueprint, bcrypt)
Solved AtCoder ABC 114 C-755 with Python3
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Twitter search client made with bottle
Segfault with 16 characters in C language
I made a daemon with Python
Speed up C / C ++ compilation with ccache
Your own Twitter client made with Django
A simple RSS reader made with Django
Simple Slack API client made with Python
HTTP split download guy made with Python
I made a character counter with Python
X86 assembler on Linux (linkage with C)
Easy C / C ++ multilingual binding with CMake + SWIG
[C] [python] Read with AquesTalk on Linux
Improved motion sensor made with Raspberry Pi
I made CORS custom middleware with Django
I made a Hex map with Python
I made a life game with Numpy
I made a stamp generator with GAN
Use C ++ functions from python with pybind11
Serverless face recognition API made with Python
I made a roguelike game with Python
I made a simple blackjack with Python
RaspberryPi L Chika with Python and C #
[C, C ++, Python, JavaScript] L Chika with Edison
I made a configuration file with Python
I made a WEB application with Django
I made a neuron simulator with Python
Communicate with I2C devices in Linux C
Othello app (iOS app) made with Python (Kivy)