Guest User

Untitled

a guest
May 24th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <pwd.h>
  7.  
  8. #define SCREENLOCK "/tmp/YOUR SCREENLOCK HERE"
  9.  
  10. int lock(char *f);
  11. void unlock(char *f);
  12.  
  13. main()
  14. { int pid;
  15. pid = getpid();
  16.  
  17.  
  18.  
  19. if(pid=fork()!=0)
  20. {//Parent
  21.  
  22. while(1)
  23. {if(lock(SCREENLOCK) == -1)
  24. { printf(".");
  25. sleep(1);
  26. }
  27. else
  28. {
  29. printf("P\n");
  30. sleep(5);
  31. unlock(SCREENLOCK);
  32. sleep(2);
  33. }
  34. }
  35. }
  36. else
  37. {//Child
  38.  
  39. while(1)
  40. {if(lock(SCREENLOCK) == -1)
  41. { printf(".");
  42. sleep(1);
  43. }
  44. else
  45. {
  46. printf("C\n");
  47. sleep(5);
  48. unlock(SCREENLOCK);
  49. sleep(2);
  50. }
  51. }
  52. }
  53.  
  54.  
  55.  
  56. }
  57.  
  58. int lock(char *f)
  59. {
  60. int fd;
  61. struct stat statbuf;
  62.  
  63. if((fd = open(f,O_WRONLY | O_CREAT | O_EXCL, 0777)) == -1)
  64.  
  65. {
  66. stat(f, &statbuf);
  67. return -1;
  68. }
  69. else
  70. {
  71. close(fd);
  72. return 0;
  73. }
  74. }
  75.  
  76. void unlock(char *f)
  77. {
  78. unlink(f);
  79. }
Add Comment
Please, Sign In to add comment