1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <fnctl.h> #include <unistd.h> #include <linux/fs.h> int main( void ) { pid_t pid; int i; /* create new process */ pid = fork(); if (pid == -1) return -1; else if (pid != 0) exit (EXIT_SUCCESS); /* create new session and process group */ if (setsid() == -1) return -1; /* set the working directory to the root directory */ if (chdir( "/" ) == -1) return -1; /* close all open files --NR_OPEN is overkill, but works */ if (i = 0; i < NR_OPEN; ++i) close(i); /* redirect fd's 0,1,2 to /dev/null */ open( "/dev/null" , O_RDWR); /* stdin */ dup(0); /* stdout */ dup(0); /* stderr */ /* do its daemon thing ... */ return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /* showppid.c */ #include <unistd.h> #include <stdio.h> int main( void ) { FILE *fp; char path[80]; //sleep(30); snprintf(path, sizeof (path) - 1, "/tmp/showppid.%d.txt" , getpid()); fp = fopen (path, "w+" ); if (fp == 0) return 0; fprintf (fp, "getppid = %d\n" , getppid()); fclose (fp); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /* showsid.c */ #include <unistd.h> #include <stdio.h> int main( void ) { FILE *fp; char path[80]; sleep(30); snprintf(path, sizeof (path) - 1, "/tmp/showsid.%d.txt" , getpid()); fp = fopen (path, "w+" ); if (fp == 0) return 0; fprintf (fp, "getppid = %d, getsid = %d\n" , getppid(), getsid(0)); fclose (fp); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /* showsid.c */ #include <unistd.h> #include <stdio.h> int main( void ) { FILE *fp; char path[80]; setpgid(0, 0); //create new process group and becomes leader sleep(30); snprintf(path, sizeof (path) - 1, "/tmp/showsid.%d.txt" , getpid()); fp = fopen (path, "w+" ); if (fp == 0) return 0; fprintf (fp, "getppid = %d, getsid = %d\n" , getppid(), getsid(0)); fclose (fp); return 0; } |
联系客服