pid文件
the pid files contains the process id (a number) of a given program. For example, Apache HTTPD may write it's main process number to a pid file - which is a regular text file, nothing more than that -, and later use the information there contained to stop itself. You can also use that information (just do a cat filename.pid
) to kill the process yourself, using kill <the number in the .pid file>
/* ============================================================================ Name : testc.c Author : Version : Copyright : Your copyright notice Description : pid文件的使用,一个终端运行该程序,另一个终端输入 kill -9 `cat testc.pid` 即可杀死该程序。注意路径和符号``. ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> int write_pid(void); int main(void) { printf("Hello World!\n"); if(write_pid() < 0) { printf("cannot write pid file!"); return -1; } while (1) { } return 0; } int write_pid(void) { FILE *f; if(!(f = fopen("testc.pid","w"))) { return -1; } fprintf(f,"%d",(int)getpid()); fclose(f); return 0; }