[root@rhel74 clang]# cat spawn.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int
main(int argc, char *argv[])
{
pid_t pid;
if (argc != 3) {
fprintf(stderr, "Usage: %s <command> <arg>\n", argv[0]);
exit(1);
}
pid = fork();
if (pid < 0) {
fprintf(stderr, "fork(2) failed\n");
exit(1);
}
if (pid == 0) { /* 子プロセス */
execl(argv[1], argv[1], argv[2], NULL);
/* execl()は成功したら戻らないので、戻ったらすべて失敗 */
perror(argv[1]);
exit(99);
}
else { /* 親プロセス */
int status;
waitpid(pid, &status, 0);
printf("child (PID=%d) finished; ", pid);
if (WIFEXITED(status))
printf("exit, status=%d\n", WEXITSTATUS(status));
else if (WIFSIGNALED(status))
printf("signal, sig=%d\n", WTERMSIG(status));
else
printf("abnormal exit\n");
exit(0);
}
}
[root@rhel74 clang]# ./spawn /bin/sleep 3
child (PID=1396) finished; exit, status=0
[root@rhel74 clang]# cat /root/clang/rsleep
#!/bin/bash
/bin/sleep `/bin/expr $((RANDOM%+10)) + 1`
[root@rhel74 clang]# cat multi_spawn.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
int
main(int argc, char *argv[])
{
pid_t pid;
int i;
if (argc != 3) {
fprintf(stderr, "Usage: %s <command> <arg>\n", argv[0]);
exit(1);
}
for(i = 0; i < 10; i++){
pid = fork();
if (pid < 0) {
fprintf(stderr, "fork(2) failed\n");
exit(1);
}
if (pid == 0) { /* 子プロセス */
execl(argv[1], argv[1], argv[2], NULL);
/* execl()は成功したら戻らないので、戻ったらすべて失敗 */
perror(argv[1]);
exit(99);
}
}
printf("for x 10, finished\n");
/* 親プロセス */
for(;;){
pid_t pid;
int status = 0;
pid = waitpid(-1, &status, 0);
if(pid == -1){
if(errno == ECHILD) break;
if(errno == EINTR) continue;
err(1, "wait error");
}
printf("child (PID=%d) finished; ", pid);
if (WIFEXITED(status)) printf("exit, status=%d\n", WEXITSTATUS(status));
else if (WIFSIGNALED(status)) printf("signal, sig=%d\n", WTERMSIG(status));
else printf("abnormal exit\n");
}
exit(0);
}
[root@rhel74 clang]# ./multi_spawn /root/clang/rsleep ss
for x 10, finished
child (PID=1885) finished; exit, status=0
child (PID=1889) finished; exit, status=0
child (PID=1892) finished; exit, status=0
child (PID=1886) finished; exit, status=0
child (PID=1893) finished; exit, status=0
child (PID=1884) finished; exit, status=0
child (PID=1888) finished; exit, status=0
child (PID=1890) finished; exit, status=0
child (PID=1891) finished; exit, status=0
child (PID=1887) finished; exit, status=0
[root@rhel74 ~]# ps -elf | grep sleep
0 S root 1883 1336 0 80 0 - 1042 do_wai 00:59 pts/0 00:00:00 ./multi_spawn /root/clang/rsleep ss
0 S root 1884 1883 0 80 0 - 28282 do_wai 00:59 pts/0 00:00:00 /bin/bash /root/clang/rsleep ss
0 S root 1885 1883 0 80 0 - 28282 do_wai 00:59 pts/0 00:00:00 /bin/bash /root/clang/rsleep ss
0 S root 1886 1883 0 80 0 - 28282 do_wai 00:59 pts/0 00:00:00 /bin/bash /root/clang/rsleep ss
0 S root 1887 1883 0 80 0 - 28282 do_wai 00:59 pts/0 00:00:00 /bin/bash /root/clang/rsleep ss
0 S root 1888 1883 0 80 0 - 28282 do_wai 00:59 pts/0 00:00:00 /bin/bash /root/clang/rsleep ss
0 S root 1889 1883 0 80 0 - 28282 do_wai 00:59 pts/0 00:00:00 /bin/bash /root/clang/rsleep ss
0 S root 1890 1883 0 80 0 - 28282 do_wai 00:59 pts/0 00:00:00 /bin/bash /root/clang/rsleep ss
0 S root 1891 1883 0 80 0 - 28282 do_wai 00:59 pts/0 00:00:00 /bin/bash /root/clang/rsleep ss
0 S root 1892 1883 0 80 0 - 28282 do_wai 00:59 pts/0 00:00:00 /bin/bash /root/clang/rsleep ss
0 S root 1893 1883 0 80 0 - 28282 do_wai 00:59 pts/0 00:00:00 /bin/bash /root/clang/rsleep ss
0 S root 1904 1884 0 80 0 - 26976 hrtime 00:59 pts/0 00:00:00 /bin/sleep 8
0 S root 1905 1885 0 80 0 - 26976 hrtime 00:59 pts/0 00:00:00 /bin/sleep 1
0 S root 1906 1886 0 80 0 - 26976 hrtime 00:59 pts/0 00:00:00 /bin/sleep 7
0 S root 1907 1887 0 80 0 - 26976 hrtime 00:59 pts/0 00:00:00 /bin/sleep 9
0 S root 1908 1888 0 80 0 - 26976 hrtime 00:59 pts/0 00:00:00 /bin/sleep 8
0 S root 1909 1889 0 80 0 - 26976 hrtime 00:59 pts/0 00:00:00 /bin/sleep 1
0 S root 1910 1890 0 80 0 - 26976 hrtime 00:59 pts/0 00:00:00 /bin/sleep 8
0 S root 1911 1891 0 80 0 - 26976 hrtime 00:59 pts/0 00:00:00 /bin/sleep 8
0 S root 1912 1892 0 80 0 - 26976 hrtime 00:59 pts/0 00:00:00 /bin/sleep 3
0 S root 1913 1893 0 80 0 - 26976 hrtime 00:59 pts/0 00:00:00 /bin/sleep 7
0 R root 1915 1840 0 80 0 - 28169 - 00:59 pts/1 00:00:00 grep --color=auto sleep
[root@rhel74 clang]# cat multi_zonbie.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
int
main(int argc, char *argv[])
{
pid_t pid;
int i;
if (argc != 3) {
fprintf(stderr, "Usage: %s <command> <arg>\n", argv[0]);
exit(1);
}
for(i = 0; i < 10; i++){
pid = fork();
if (pid < 0) {
fprintf(stderr, "fork(2) failed\n");
exit(1);
}
if (pid == 0) { /* 子プロセス */
execl(argv[1], argv[1], argv[2], NULL);
/* execl()は成功したら戻らないので、戻ったらすべて失敗 */
perror(argv[1]);
exit(99);
}
}
printf("for x 10, finished\n");
sleep(20);
exit(0);
}
[root@rhel74 clang]# ./multi_zonbie /root/clang/rsleep ss
for x 10, finished
[root@rhel74 ~]# ps -elf | grep sleep
0 S root 2015 1336 0 80 0 - 1042 hrtime 01:07 pts/0 00:00:00 ./multi_zonbie /root/clang/rsleep ss
0 Z root 2016 2015 0 80 0 - 0 do_exi 01:07 pts/0 00:00:00 [rsleep] <defunct>
0 Z root 2017 2015 0 80 0 - 0 do_exi 01:07 pts/0 00:00:00 [rsleep] <defunct>
0 S root 2018 2015 0 80 0 - 28282 do_wai 01:07 pts/0 00:00:00 /bin/bash /root/clang/rsleep ss
0 Z root 2019 2015 0 80 0 - 0 do_exi 01:07 pts/0 00:00:00 [rsleep] <defunct>
0 Z root 2020 2015 0 80 0 - 0 do_exi 01:07 pts/0 00:00:00 [rsleep] <defunct>
0 Z root 2021 2015 0 80 0 - 0 do_exi 01:07 pts/0 00:00:00 [rsleep] <defunct>
0 S root 2022 2015 0 80 0 - 28282 do_wai 01:07 pts/0 00:00:00 /bin/bash /root/clang/rsleep ss
0 Z root 2023 2015 0 80 0 - 0 do_exi 01:07 pts/0 00:00:00 [rsleep] <defunct>
0 Z root 2024 2015 0 80 0 - 0 do_exi 01:07 pts/0 00:00:00 [rsleep] <defunct>
0 Z root 2025 2015 0 80 0 - 0 do_exi 01:07 pts/0 00:00:00 [rsleep] <defunct>
0 S root 2038 2018 0 80 0 - 26976 hrtime 01:07 pts/0 00:00:00 /bin/sleep 10
0 S root 2042 2022 0 80 0 - 26976 hrtime 01:07 pts/0 00:00:00 /bin/sleep 10
[root@rhel74 clang]# cat pipe.c
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define READ (0)
#define WRITE (1)
int main(int argc, char *argv[]) {
int fd_r = fileno(stdin);
if (argc < 1) {
printf("Usage: %s\n", argv[0]);
return 1;
}
// 子から親への通信用パイプ
int pipe_child2parent[2];
// プロセスID
int pid;
// パイプを生成
if (pipe(pipe_child2parent) < 0) {
// パイプ生成失敗
perror("pipe");
return 1;
}
// fork
if ((pid = fork()) < 0) {
// fork失敗
perror("fork");
// 開いたパイプを閉じる
close(pipe_child2parent[READ]);
close(pipe_child2parent[WRITE]);
return 1;
}
// 子プロセスか?
if (pid == 0) {
close(pipe_child2parent[READ]);
// 子→親への入力を標準出力に割り当て
dup2(pipe_child2parent[WRITE], 1);
// 割り当てたファイルディスクリプタは閉じる
close(pipe_child2parent[WRITE]);
// 子プロセスはここで該当プログラムを起動しリターンしない
sleep(5);
if (execl("/bin/echo", "/bin/echo", "message child to parent", NULL) < 0) {
perror("execl");
close(pipe_child2parent[WRITE]);
return 1;
}
}else{
// 親プロセス側の処理
close(pipe_child2parent[WRITE]);
fd_r = pipe_child2parent[READ];
char buf[255];
printf("waiting messages from child\n");
int size = read(fd_r, buf, 255);
if (size == -1) {
perror("error");
return 1;
}
fwrite(buf, 1, size, stdout);
printf("\n");
}
return 0;
}
[root@rhel74 clang]# ./pipe
waiting messages from child
message child to parent
[root@rhel74 clang]# cat sigint.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void print_exit();
int
main(int argc, char *argv[])
{
struct sigaction act, old;
act.sa_handler = print_exit;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART;
if (sigaction(SIGINT, &act, &old) < 0){
perror("sigaction");
exit(1);
}
printf("please press ^C\n");
sleep(10);
printf("timed out\n");
if(pause() == -1){
perror("sigaction");
exit(1);
}
exit(0);
}
void
print_exit(int sig)
{
printf("Got signal %d\n", sig);
exit(0);
}
[root@rhel74 clang]# ./sigint
please press ^C
^CGot signal 2
[root@rhel74 clang]# ./sigint
please press ^C
timed out
^CGot signal 2