Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <signal.h>
- #include <ncurses.h>
- #include <stdlib.h>
- #include <termios.h>
- #include <sys/ioctl.h>
- #include <string.h>
- int _kbhit() {
- static bool inited = false;
- int left;
- if (!inited) {
- struct termios t;
- tcgetattr(0, &t);
- t.c_lflag &= ~ICANON;
- tcsetattr(0, TCSANOW, &t);
- setbuf(stdin, NULL);
- inited = true;
- }
- ioctl(0, FIONREAD, &left);
- return left;
- }
- void handle_sigusr1(int sig)
- {
- fflush(stdout);
- system("date");
- FILE *f = fopen("text.txt", "a");
- if(f == NULL)
- {
- printf("Error while opening file\n");
- return;
- }
- char *str = (char *)malloc(sizeof(char) * 256);
- strcpy(str, "aaa");
- for(int i = 0; i<3; i++)
- {
- str[i] = 'a' + rand()%26;
- }
- fprintf(f,"%s\n", str);
- fclose(f);
- }
- int main()
- {
- int pid = fork();
- switch (pid)
- {
- case -1:
- printf("Error launchong mew proces\n");
- break;
- case 0:
- {
- while (true)
- {
- sleep(2);
- kill(getppid(), SIGUSR1);
- }
- }
- default:
- {
- struct sigaction sa;
- //gsa.sa_flags = SA_RESTART;
- sa.sa_handler = &handle_sigusr1;
- sigaction(SIGUSR1, &sa, NULL);
- while (true)
- {
- if(_kbhit() != 0)
- {
- kill(pid, SIGKILL);
- return 0;
- }
- }
- }
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement