In UNIX / Linux In order to ensure that one program is running in the same time, it is necessary to prevent the occurrence of dual boot, in addition to using the lock file, in addition to the system, in the process The information in the table is controlled (the content seen by the PS command).
There are two ways: the first is: to prevent the control implementation of the Double Starts with the process of the process: (1) Defining the name of the application of the application Char EXE_NAME [255] = "App"; Can perform PS command with EXEC or POPEN, I use POPEN to use POPEN to introduce #include ; char ps_cmd [256]; char STR [256]; file fd; int brun = 0; STRCPY (PS_CMD, "PS-EF | GREP"); STRCAT (PS_CMD, EXE_NAME); if ((FD = POPEN (PS_CMD, "R")) == NULL) {Printf ("Call Popen FaileD / N"); Return;} else {while (FGETS (STR, 255, FD)! = null) {Printf ("% S / N", STR); brun = 1; Break;}}}} (brun == 1) {/ * There is already an instance running * / exit (0);} / * The function of executing the program * / this method of this method: (1) If the other program's main execution file name is the same, the program is the same, and is executing, then The program to be executed can be executed, but it cannot be performed; (2) If this program is using a full path, or the program name is relatively long, the output result may be shorted, which may be in GREP It can be matched to the execution file name, and the error determines that this program has not yet been running, but it has been running; the second is: to prevent two-start control (1) to place the program with PID PID of the PID CHAR PID_FILE [255] = "App.PID"; (2) If the app.pid file does not exist, the program is not run, created the file, and writes the PID of the current running program to the file PID_T PID = -1; pid = getPid (); / * Write PID to app.pid file * / / * execution Program function * / execution (4) If the app.pid file exists, read the PID value in the app.pid file and the PID that is running the process in the system process table, [/ * From app.pid Read the PID value in the file, put it in pid_value * / strcpy (ps_cmd, "ps -ef | awk '{print $ 2}' | grep"); strcat (PS_CMD, PID_VALUE); / * Find pid_value values in the process table * / If ((fd = popen (ps_cmd, "r")) == null) {Printf ("Call Popen Failed / N");