Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <pwd.h>
- #define SCREENLOCK "/tmp/YOUR SCREENLOCK HERE"
- int lock(char *f);
- void unlock(char *f);
- main()
- { int pid;
- pid = getpid();
- if(pid=fork()!=0)
- {//Parent
- while(1)
- {if(lock(SCREENLOCK) == -1)
- { printf(".");
- sleep(1);
- }
- else
- {
- printf("P\n");
- sleep(5);
- unlock(SCREENLOCK);
- sleep(2);
- }
- }
- }
- else
- {//Child
- while(1)
- {if(lock(SCREENLOCK) == -1)
- { printf(".");
- sleep(1);
- }
- else
- {
- printf("C\n");
- sleep(5);
- unlock(SCREENLOCK);
- sleep(2);
- }
- }
- }
- }
- int lock(char *f)
- {
- int fd;
- struct stat statbuf;
- if((fd = open(f,O_WRONLY | O_CREAT | O_EXCL, 0777)) == -1)
- {
- stat(f, &statbuf);
- return -1;
- }
- else
- {
- close(fd);
- return 0;
- }
- }
- void unlock(char *f)
- {
- unlink(f);
- }
Add Comment
Please, Sign In to add comment