diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..226bb33 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "wait.h": "c", + "stat.h": "c" + } +} \ No newline at end of file diff --git a/child_porcess_clean/clear.c b/child_porcess_clean/clear.c new file mode 100644 index 0000000..95e9324 --- /dev/null +++ b/child_porcess_clean/clear.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void process_clean() +{ + while (wait(NULL) != -1) + { + printf("child clean\n"); + } + + return 0; +} +int main(int argc, char *argv[]) +{ + signal(SIGCHLD, process_clean); + for (int i = 0; i < 10; i++) + { + pid_t pid = fork(); + if (pid == 0) + { + sleep(1); + exit(0); + } + else if (pid > 0) + { + int status; + // wait(&status); + // printf("Child %d exited with status %d\n", pid, status); + } + else + { + perror("fork"); + } + } + int k = 5; + while (k--) + { + sleep(1); + } + + return 0; +} \ No newline at end of file diff --git a/child_porcess_clean/run.sh b/child_porcess_clean/run.sh new file mode 100755 index 0000000..3160891 --- /dev/null +++ b/child_porcess_clean/run.sh @@ -0,0 +1,3 @@ +#!/bin/bash +gcc -o test.bin clear.c +./test.bin \ No newline at end of file diff --git a/child_porcess_clean/test.bin b/child_porcess_clean/test.bin new file mode 100755 index 0000000..c22791a Binary files /dev/null and b/child_porcess_clean/test.bin differ diff --git a/deamon/daemon.c b/deamon/daemon.c new file mode 100644 index 0000000..7598268 --- /dev/null +++ b/deamon/daemon.c @@ -0,0 +1,158 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define CREAT_PID_FILE_PATH "/tmp/mydaemon.pid" +#define LOG_FILE_NAME "log.txt" +#define SUCCESS() fprintf(stdout, "success\n") +void daemonize(const char *cmd) +{ + pid_t pid, sid; + + // Fork off the parent process + pid = fork(); + // An error occurred + if (pid < 0) + { + + fprintf(stderr, "can not creat process\n"); + exit(EXIT_FAILURE); + } + // Success: Let the parent terminate + if (pid > 0) + { + exit(EXIT_SUCCESS); + } + + // On success: The child process becomes session leader + if ((sid = setsid()) < 0) + { + fprintf(stderr, "can not new secion\n"); + exit(EXIT_FAILURE); + } + + // Catch, ignore and handle signals + signal(SIGCHLD, SIG_IGN); + signal(SIGHUP, SIG_IGN); + // Fork off for the second time + pid = fork(); + + // An error occurred + if (pid < 0) + { + fprintf(stderr, "can not creat process\n"); + exit(EXIT_FAILURE); + } + + // Success: Let the parent terminate + if (pid > 0) + { + exit(EXIT_SUCCESS); + } + + // Set new file permissions + umask(0); + + // Change the working directory to the root directory + // or another appropriated directory + if ((chdir("/")) < 0) + { + exit(EXIT_FAILURE); + } + + // Open the /dev/null file + // open("/dev/null", O_RDWR); + // open("/dev/null", O_RDWR); + + // Write the PID to the pidfile + FILE *fp; + fp = fopen(CREAT_PID_FILE_PATH, "w"); + if (fp != NULL) + { + fprintf(fp, "%d\n", getpid()); + fclose(fp); + } + else + { + fprintf(stdout, "create file error\n"); + exit(EXIT_FAILURE); + } + fprintf(stdout,"server start...\n"); + // Close all open file descriptors + int x; + for (x = sysconf(_SC_OPEN_MAX); x >= 0; x--) + { + close(x); + } + // Open the /dev/null file + open("/dev/null", O_RDWR); + open("/dev/null", O_RDWR); + // Now we can do the tasks of the daemon + while (1) + { + // Your daemon code here + sleep(1); + fprintf(stdout, "Daemon is running...\n"); + } +} + +int main(int argc, char *argv[]) +{ + const char *cmd = "mydaemon"; + // Check for a valid command line argument + if (argc < 2) + { + fprintf(stderr, "Usage: %s start|stop\n", cmd); + exit(EXIT_FAILURE); + } + + // Check the command line argument + if (strcmp(argv[1], "start") == 0) + { + // 测试下是否已经有个进程了 + int exit = access(CREAT_PID_FILE_PATH, F_OK); + if (exit == 0) + { + fprintf(stderr, "the server is exited, so safe exit\n"); + return 0; + } + else + { + fprintf(stdout, " try to start server\n"); + } + daemonize(cmd); + } + else if (strcmp(argv[1], "stop") == 0) + { + // Code to stop the daemon + FILE *fp; + pid_t pid; + + fp = fopen(CREAT_PID_FILE_PATH, "r"); + if (fp != NULL) + { + + fscanf(fp, "%d", &pid); + fclose(fp); + kill(pid, SIGTERM); + remove(CREAT_PID_FILE_PATH); + fprintf(stdout, "server stop...\n"); + } + else + { + printf(" no find the process\n"); + } + } + else + { + fprintf(stderr, "Usage: %s start|stop\n", cmd); + exit(EXIT_FAILURE); + } + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/deamon/log.txt b/deamon/log.txt new file mode 100644 index 0000000..e69de29 diff --git a/deamon/run.sh b/deamon/run.sh new file mode 100755 index 0000000..c9f563d --- /dev/null +++ b/deamon/run.sh @@ -0,0 +1,7 @@ +#!/bin/bash +if [ "$1" = "ps" ];then + ps -aux | grep test +else + gcc -o test.bin daemon.c + ./test.bin $1 +fi \ No newline at end of file diff --git a/deamon/test.bin b/deamon/test.bin new file mode 100755 index 0000000..fbceec6 Binary files /dev/null and b/deamon/test.bin differ diff --git a/exec/app.bin b/exec/app.bin new file mode 100755 index 0000000..2430bde Binary files /dev/null and b/exec/app.bin differ diff --git a/exec/app.c b/exec/app.c new file mode 100644 index 0000000..8f62e50 --- /dev/null +++ b/exec/app.c @@ -0,0 +1,6 @@ +#include + +int main(int argc ,char *argv[]){ + printf("app run\n"); + return 0; +} \ No newline at end of file diff --git a/exec/exec.c b/exec/exec.c new file mode 100644 index 0000000..acd03a4 --- /dev/null +++ b/exec/exec.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +extern char **environ; + +int main(int argc ,char *argv[]) +{ + char pwd[1024]; + getcwd(pwd,1024); + printf("master \n"); + printf("path %s\n",pwd); + char path[1024]; + snprintf(path,sizeof(path),"%s/app.bin",pwd); + + char *exec_arg[]={path,NULL}; + char *envp[] = {NULL}; + int err = execve(path,exec_arg,envp); + printf("error happen %d\n",err); + return 0; +} + + +// int main() { + +// char *args[] = {"/bin/ls", "-l", NULL}; +// char *envp[] = {"PATH=/bin:/usr/bin", NULL}; +// execve("/bin/ls", args, envp); +// // 如果 execve 失败,会执行到这里 +// perror("execve failed"); + +// return 1; + +// } \ No newline at end of file diff --git a/exec/run.sh b/exec/run.sh new file mode 100755 index 0000000..21cff57 --- /dev/null +++ b/exec/run.sh @@ -0,0 +1,4 @@ +#!/bin/bash +gcc -o app.bin ./app.c +gcc -o test.bin ./exec.c +./test.bin ./app.bin \ No newline at end of file diff --git a/exec/test.bin b/exec/test.bin new file mode 100755 index 0000000..6f45c88 Binary files /dev/null and b/exec/test.bin differ