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.
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.
'# 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.
http://chikuwait.hatenablog.com/entry/2017/10/05/001126
Recommended Posts