Script started on Wed Dec 4 12:30:56 2024 mweeks@air:unix_ch14$ echo 4321 4321 mweeks@air:unix_ch14$ ls *c Iterated_Prisoners_Dilemmma.c critical.c alarm.c handler.c chef.c itime.c controlC.c limit.c controlC2.c linked_list.c controlC3.c myfork.c controlC4.c pulse.c cook.c tasks.c mweeks@air:unix_ch14$ cat alarm.c // Linux for Programmers and Users, Glass/Ables, page 489 #include #include #include #include int main () { alarm (3); /* Schedule an alarm signal in three seconds */ printf ("Looping forever...\n"); while (1); printf ("This line should never be executed\n"); } mweeks@air:unix_ch14$ gcc alarm.c mweeks@air:unix_ch14$ ./a.out Looping forever... Alarm clock: 14 mweeks@air:unix_ch14$ more controlC.c /* This example is from James Wilson's book "Berkeley Unix: A Simple and Comprehensive Guide", John Wiley & Sons, Inc., New York, 1991, page 171. It sets up a function called "confirm()" that is executed whenever the user presses control-C, allowing this program to handle control-C interrupts. I had to make a few changes to get it to compile, such as changing "int confirm()" to "void confirm()". -MCW, Spring 2008 compile with: gcc controlC.c -o controlC /skiff/local/bin/arm-linux-gcc controlC.c -o controlC_iPAQ */ #include #include #include /* needed for "exit" call */ #include // for fork, sleep void confirm() { char yes_or_no; char carriage_return; printf("\nDo you really want to exit? (Y/N)"); scanf("%c%c", &yes_or_no, &carriage_return); printf("%c\n", yes_or_no); if ((yes_or_no == 'y') || (yes_or_no == 'Y')) { exit(0); } } int main() { int i; /* Set up "confirm" as the function to call on ctrl-C */ signal(SIGINT, confirm); for (i=1; i<=20; i++) { printf("Going through loop number %d\n", i); sleep(1); } } mweeks@air:unix_ch14$ ./a.out Looping forever... ^C mweeks@air:unix_ch14$ gcc controlC.c -o controlC mweeks@air:unix_ch14$ ./controlC Going through loop number 1 Going through loop number 2 Going through loop number 3 Going through loop number 4 Going through loop number 5 Going through loop number 6 Going through loop number 7 Going through loop number 8 ^C Do you really want to exit? (Y/N)y y mweeks@air:unix_ch14$ ./controlC Going through loop number 1 Going through loop number 2 ^C Do you really want to exit? (Y/N)n n Going through loop number 3 Going through loop number 4 Going through loop number 5 Going through loop number 6 Going through loop number 7 Going through loop number 8 Going through loop number 9 Going through loop number 10 Going through loop number 11 Going through loop number 12 Going through loop number 13 Going through loop number 14 Going through loop number 15 Going through loop number 16 Going through loop number 17 Going through loop number 18 Going through loop number 19 Going through loop number 20 mweeks@air:unix_ch14$ cat controlC2.c /* See James Wilson's book, p171. */ #include #include #include /* needed for "exit" call */ #include // for fork, sleep int myarray[2]; int s; void monitor() { // interrupt routine s = rand() / 1000; myarray[0] = s; myarray[1] = s; } int main() { int a, b, i; /* Set up "monitor" as the function to call on ctrl-C */ signal(SIGINT, monitor); myarray[0] = 0; myarray[1] = 0; for (i=1; i<=20; i++) { a = myarray[0]; printf("Going through loop number %d\n", i); sleep(1); b = myarray[1]; if (a != b) { printf("Error! Values do not match!\n"); exit(0); } } } mweeks@air:unix_ch14$ gcc controlC2.c mweeks@air:unix_ch14$ ./a.out Going through loop number 1 ^CError! Values do not match! mweeks@air:unix_ch14$ diff controlC2.c controlC3.c 2a3,18 > See also http://www.csl.mtu.edu/cs4411.choi/www/Resource/signal.pdf > > Signals: SIGALRM > SIGBUS > SIGFPE > SIGINT > SIGQUIT > SIGTERM > SIGUSR1 > SIGUSR2 > > // User-defined signal 1: > signal(SIGUSR1, SIGhandler); > if (count++ > 1000) > raise(SIGUSR1); > 17a34,50 > > // See cls.mtu.edu ppt slides > void ALARMhandler(int sig) { > //signal(SIGINT, SIG_IGN); > signal(SIGALRM, SIG_IGN); > printf("ALARM handler: alarm signal received\n"); > //printf("ALARM handler: i = %d and j = %d\n", i, j); > alarm(4); > //signal(SIGINT, INThandler); > // Yes, this must be done again: > signal(SIGALRM, ALARMhandler); > } > // This shell command seems to be equivalent to control-C, for process 1234: > // kill –INT 1234 > > > 22a56 > signal(SIGALRM, ALARMhandler); 25a60 > alarm(5); mweeks@air:unix_ch14$ gcc controlC3.c mweeks@air:unix_ch14$ ./a.out Going through loop number 1 Going through loop number 2 Going through loop number 3 Going through loop number 4 Going through loop number 5 ALARM handler: alarm signal received Going through loop number 6 Going through loop number 7 Going through loop number 8 Going through loop number 9 ALARM handler: alarm signal received Going through loop number 10 Going through loop number 11 Going through loop number 12 Going through loop number 13 ALARM handler: alarm signal received Going through loop number 14 Going through loop number 15 Going through loop number 16 Going through loop number 17 ALARM handler: alarm signal received Going through loop number 18 Going through loop number 19 Going through loop number 20 mweeks@air:unix_ch14$ ls Dec5_2024.log handler.c Iterated_Prisoners_Dilemmma.c itime.c a.out limit.c alarm.c linked_list.c chef myfork.c chef.c pipe_fork_signal_wait controlC proc.h controlC.c pulse.c controlC2.c recipe controlC3.c task_struct.h controlC4.c task_struct.txt cook tasks.c cook.c test.txt critical.c mweeks@air:unix_ch14$ more critical.c // Linux for Programmers and Users, Glass/Ables, pages 491-492 #include #include #include #include int main () { void (*oldHandler) (); /* To hold old handler value */ printf ("I can be Control-C'ed\n"); sleep (3); oldHandler = signal (SIGINT, SIG_IGN); /* Ignore Control-C */ printf ("I'm protected from Control-C now\n"); sleep (3); signal (SIGINT, oldHandler); /* Restore old handler */ printf ("I can be Control-C'ed again\n"); sleep (3); printf ("Bye!\n"); return 0; } mweeks@air:unix_ch14$ gcc critical.c mweeks@air:unix_ch14$ ./a.out I can be Control-C'ed ^C mweeks@air:unix_ch14$ ./a.out I can be Control-C'ed I'm protected from Control-C now ^CI can be Control-C'ed again Bye! mweeks@air:unix_ch14$ ./a.out I can be Control-C'ed I'm protected from Control-C now I can be Control-C'ed again ^C mweeks@air:unix_ch14$ ./a.out I can be Control-C'ed I'm protected from Control-C now I can be Control-C'ed again Bye! mweeks@air:unix_ch14$ ssh snowball.cs.gsu.edu mweeks@snowball.cs.gsu.edu's password: Last login: Wed Dec 4 09:35:33 2024 from c-24-99-154-26.hsd1.ga.comcast.net + | GSU Computer Science | Instructional Server | SNOWBALL.cs.gsu.edu + [mweeks@gsuad.gsu.edu@snowball ~]$ ps -e PID TTY TIME CMD 1 ? 00:37:09 systemd 2 ? 00:00:05 kthreadd 4 ? 00:00:00 kworker/0:0H 6 ? 00:00:15 ksoftirqd/0 [lines removed] [mweeks@gsuad.gsu.edu@snowball ~]$ ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 Sep13 ? 00:37:10 /usr/lib/systemd/systemd --switc root 2 0 0 Sep13 ? 00:00:05 [kthreadd] root 4 2 0 Sep13 ? 00:00:00 [kworker/0:0H] root 6 2 0 Sep13 ? 00:00:15 [ksoftirqd/0] [lines removed] hmohame+ 22793 1 0 Nov18 ? 00:00:00 nano /home/hmohamed16/Documents/ jgutlay+ 23073 1 0 Nov19 ? 00:00:00 ./prisoner_dilemna 5 jgutlay+ 23085 1 0 Nov19 ? 00:00:00 ./prisoner_dilemna 2 scheong+ 23153 1 0 Sep15 ? 00:00:00 vim hello.c agantai+ 24074 1 0 Nov11 ? 00:00:00 sh /home/agantait1/.vscode-serve agantai+ 24080 24074 0 Nov11 ? 00:00:00 /home/agantait1/.vscode-server/c goteri1+ 24605 1 2 Dec02 ? 01:06:56 ./lab13 goteri1+ 24607 1 2 Dec02 ? 01:06:25 ./lab13 goteri1+ 24608 1 2 Dec02 ? 01:09:17 ./lab13 goteri1+ 24609 1 2 Dec02 ? 01:06:34 ./lab13 root 24744 2 0 12:16 ? 00:00:00 [kworker/3:0] goteri1+ 24937 1 2 Dec02 ? 01:04:44 ./lab13 goteri1+ 24939 1 2 Dec02 ? 01:06:31 ./lab13 goteri1+ 24940 1 2 Dec02 ? 01:06:53 ./lab13 goteri1+ 24941 1 3 Dec02 ? 01:09:39 ./lab13 goteri1+ 29949 1 2 Dec02 ? 01:01:46 ./lab13 goteri1+ 29951 1 2 Dec02 ? 01:01:42 ./lab13 goteri1+ 29952 1 2 Dec02 ? 01:07:49 ./lab13 goteri1+ 29954 1 2 Dec02 ? 01:07:39 ./lab13 goteri1+ 31247 1 2 Dec02 ? 01:05:28 ./lab13 goteri1+ 31249 1 2 Dec02 ? 01:05:45 ./lab13 root 31956 2 0 13:03 ? 00:00:00 [kworker/1:2] [mweeks@gsuad.gsu.edu@snowball ~]$ ps PID TTY TIME CMD 2020 pts/19 00:00:00 bash 2506 pts/19 00:00:00 ps [mweeks@gsuad.gsu.edu@snowball ~]$ top top - 13:30:16 up 81 days, 22:57, 16 users, load average: 1.00, 1.01, 1.09 Tasks: 192 total, 16 running, 176 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.5 us, 0.1 sy, 10.0 ni, 89.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 8008768 total, 1187200 free, 642388 used, 6179180 buff/cache KiB Swap: 4194300 total, 4027180 free, 167120 used. 6641256 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 24605 goteri1+ 39 19 10880 204 12 R 5.0 0.0 67:06.10 lab13 24609 goteri1+ 39 19 10884 208 12 R 4.7 0.0 66:59.42 lab13 31249 goteri1+ 39 19 10884 208 12 R 3.3 0.0 65:53.16 lab13 24937 goteri1+ 39 19 10880 204 12 R 3.0 0.0 64:52.00 lab13 29949 goteri1+ 39 19 10880 204 12 R 2.7 0.0 61:59.16 lab13 29954 goteri1+ 39 19 10884 208 12 R 2.7 0.0 67:47.16 lab13 31247 goteri1+ 39 19 10880 204 12 R 2.7 0.0 65:37.38 lab13 24607 goteri1+ 39 19 10884 208 12 R 2.3 0.0 66:35.78 lab13 24608 goteri1+ 39 19 10884 208 12 R 2.3 0.0 69:27.80 lab13 24940 goteri1+ 39 19 10884 208 12 R 2.3 0.0 67:03.76 lab13 24941 goteri1+ 39 19 10884 208 12 R 2.3 0.0 69:47.88 lab13 29951 goteri1+ 39 19 10884 208 12 R 2.3 0.0 61:52.48 lab13 24939 goteri1+ 39 19 10884 208 12 R 2.0 0.0 66:42.32 lab13 29952 goteri1+ 39 19 10884 208 12 R 2.0 0.0 67:58.79 lab13 19623 root 20 0 2383212 173132 20940 S 1.7 2.2 17:14.25 cylancesvc 1187 root 20 0 3015524 93456 76232 S 0.3 1.2 239:00.63 fail2ban-s+ 2509 mweeks@+ 20 0 175120 2708 1892 R 0.3 0.0 0:00.04 top [mweeks@gsuad.gsu.edu@snowball ~]$ grep goteri1 /etc/passwd [mweeks@gsuad.gsu.edu@snowball ~]$ exit logout Connection to snowball.cs.gsu.edu closed. mweeks@air:unix_ch14$ exit exit Script done on Wed Dec 4 13:32:25 2024