#include #include #include #include #include #include #include #include #include #include char *getTime() { time_t now = time(NULL); char *result = ctime(&now); result[strlen(result) - 1] = '\0'; return result; } int main(int argc, char **argv) { // fork a new process pid_t cpid = fork(); if (cpid < 0) { printf("ERROR: fork() not successful\n"); exit(EXIT_FAILURE); } // exit the parent process if (cpid > 0) exit(0); // unmask the file mode umask(0); // create a new session for the daemon process pid_t sid = setsid(); if (sid < 0) { printf("ERROR: setsid() not successful\n"); exit(EXIT_FAILURE); } // change directory to root chdir("/"); // close all file handles close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); // create log file FILE *fp = fopen("/tmp/cuda_lock_driver.log", "w"); fprintf(fp, "%s - INFO: cuda_lock_driver started\n", getTime()); fclose(fp); // start CUDA device locking int deviceCountOld = -1; while (true) { int deviceCount = 0; cudaError_t error_id = cudaGetDeviceCount(&deviceCount); if (error_id != cudaSuccess) { FILE *fp = fopen("/tmp/cuda_lock_driver.log", "a"); fprintf(fp, "%s - ERROR: cudaGetDeviceCount() returned %d\n-> %s\n", getTime(), (int)error_id, cudaGetErrorString(error_id)); fclose(fp); exit(EXIT_FAILURE); } if (deviceCountOld == -1) deviceCountOld = deviceCount; FILE *fp = fopen("/tmp/cuda_lock_driver.log", "a"); fprintf(fp, "%s - INFO: cudaGetDeviceCount() returned %d\n", getTime(), deviceCount); fclose(fp); if (deviceCountOld != deviceCount) { FILE *fp = fopen("/tmp/cuda_lock_driver.log", "a"); printf("%s - ERROR: cudaGetDeviceCount() result has changed from %d to %d\n", getTime(), deviceCountOld, deviceCount); fclose(fp); exit(EXIT_FAILURE); } sleep(15 * 60); } }