-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask1.c
98 lines (65 loc) · 3.32 KB
/
task1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
I pledge my honor that I have abided by the Stevens Honor System - Aparajita Rana
* This program implements a signal-handling program:
* Functionality Description:
This program will register a signal handler for sigint (SIGINT), using the sigaction interface (with the sa_sigaction field in the second argument to sigaction). When a sigint signal is received (by pressing ctrl + c), the handler will increase a "global variable" called "sigcount" by 1
After the program starts, it will run an infinite loop, where each iteration will print a message and then sleep for 1 second.
You can press ctrl + c to trigger the sigint signal. After you do it for 3 times, the the loop will be broken and the program will terminate.
* Testing points:
You are required to find out 10 errors in the following piece of code. You need to find at least 8 errors to get full score.
HINT:
Note that all the errors are marked with hints
Please find syntax and grammar errors that prevent the building
Pay attention to compiler warnings (if all syntax and grammar errors are fixed, you will not see any compiler warnings)
Please find the errors that mismatch the above functionality descriptions
Please find the missing of error checking
Please find other errors based on the hints (you may need to change the locations of some code)
To test this case, run "make -s task1"
After fixing all the errors, the program will run an infinite loop and after you press ctrl + c for 3 times, the program will terminate.
In case you do not find all the errors and you cannot kill your program by ctrl + c, try ctrl + d and then run "pkill task1"
*
*/
// SIGNAL.H Hint 1: something missing?
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
int sigcount; //DONE - global Hint 3: is this variable at the correct location? Check the description to understand its scope
//DONE Hint 2: does this handler have a correct prototype? You can google the requirement of the sigaction interface
static void hdl (int sig, siginfo_t *siginfo, void *context){
sigcount++;
printf ("Received Signal, PID: %ld, UID: %ld\n", (long)siginfo->si_pid, (long)siginfo->si_uid);
}
int main (int argc, char **argv){
struct sigaction act;
//DONE Hint 4: Missing any initialization?
sigcount=0;
memset(&act, '\0',sizeof(act));
//Some code here?
/* Use the sa_sigaction field */
act.sa_sigaction=hdl;
/* Setup the flag to tell sigaction() to use the sa_sigaction field */
//DONE Hint 5: is it setting the correct flag?
act.sa_flags = SA_SIGINFO;
//DONE added & Hint 6+7: Are all arguments correctly passed to the following function?
//There are two errors related to the first two arguments.
//Please check both the above functionality description and the prototype of the "sigaction" interface
int sigret = sigaction(SIGINT, &act, NULL);
//DONE Hint 8: missing something after a system level interface?
if(sigret<0){
perror("Error: sigaction");
return 1;
}
while(1){
//DONE Hint 9: will this condition check work correctly?
if(sigcount == 3){
//DONE fix the %d Hint 10: any problem in the following format string? Think about types
printf("You have hit ctrl+c for %d times. Program terminated\n", sigcount);
exit(0);
}
printf("Hello, please presse ctrl+c\n");
sleep(1);
}
return 0;
}