This commit is contained in:
chenyf 2024-02-27 20:28:51 +08:00
parent 1f11d41e9f
commit 05046725b7
13 changed files with 273 additions and 0 deletions

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"files.associations": {
"wait.h": "c",
"stat.h": "c"
}
}

View File

@ -0,0 +1,50 @@
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
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;
}

3
child_porcess_clean/run.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
gcc -o test.bin clear.c
./test.bin

BIN
child_porcess_clean/test.bin Executable file

Binary file not shown.

158
deamon/daemon.c Normal file
View File

@ -0,0 +1,158 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#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;
}

0
deamon/log.txt Normal file
View File

7
deamon/run.sh Executable file
View File

@ -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

BIN
deamon/test.bin Executable file

Binary file not shown.

BIN
exec/app.bin Executable file

Binary file not shown.

6
exec/app.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
int main(int argc ,char *argv[]){
printf("app run\n");
return 0;
}

39
exec/exec.c Normal file
View File

@ -0,0 +1,39 @@
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <signal.h>
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;
// }

4
exec/run.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
gcc -o app.bin ./app.c
gcc -o test.bin ./exec.c
./test.bin ./app.bin

BIN
exec/test.bin Executable file

Binary file not shown.