- #include <signal.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <stdio.h>
- #include <string.h>
- #define ALARM_WIDTH 32
- #define ALARM_HEIGHT 12
- void ding(int sig)
- {
- int row, col;
- int ch = '*';
- char alarm_string[16] = "ALARM!";
- int i = 0;
- fprintf(stdout, "\\n");
- for(row = 0; row < ALARM_HEIGHT; row++) {
- if((row == 0) || (row == (ALARM_HEIGHT - 1)))
- {
- for(col = 0;col < ALARM_WIDTH; col++)
- putchar(ch);
- putchar('\\n');
- }
- else if(row != (ALARM_HEIGHT/2)) {
- for(col = 0;col < ALARM_WIDTH; col++) {
- if(col == 0 || col == (ALARM_WIDTH -1))
- putchar(ch);
- else
- putchar(' ');
- }
- putchar('\\n');
- }
- else {
- int len = strlen(alarm_string);
- for(col = 0;col < ALARM_WIDTH; col++) {
- if(col == 0 || col == (ALARM_WIDTH -1))
- putchar(ch);
- else if((col >= (ALARM_WIDTH - len)/2) && col < (ALARM_WIDTH + len)/2)
- {
- for(; i < strlen(alarm_string); i++)
- putchar(alarm_string[i]);
- }
- else
- putchar(' ');
- }
- }
- putchar('\\n');
- }
- }
- int main()
- {
- pid_t pid;
- puts("Alarm application starting\\n");
- pid = fork();
- switch(pid) {
- case -1:
- /* Failure */
- perror("Fork failed");
- exit(EXIT_FAILURE);
- case 0:
- /* Child process */
- sleep(3);
- kill(getppid(), SIGALRM);
- exit(EXIT_SUCCESS);
- }
- /* This is the parent process */
- puts("Waiting for alarm to turn off\\n");
- (void) signal(SIGALRM, ding);
- pause();
- printf("Done\\n");
- exit(EXIT_SUCCESS);
- }
- //该片段来自于http://www.codesnippet.cn/detail/310320149193.html
来源: http://www.codesnippet.cn/detail/310320149193.html