Question: Problem 3 : ( 8 pts ) SIGNALS and THREADS Read the C - based pseudo - code that executes on an OUTDATED Linux version.

Problem 3: (8 pts) SIGNALS and THREADS
Read the C-based pseudo-code that executes on an OUTDATED Linux version. Assume that each thread inherits the signal handlers set by the main process just before the call to pthread_create, and that that the particular thread is impervious to the changes of signal handlers from other threads or the main process once pthread_create has been invoked (this is not true for most of the modern OSs but here we work on a fictitious problem). For the sake of this problem only, assume that terminating signals that are sent to one thread alone only terminate the thread that handles these.
Also, assume that for signals that need to be handled only once, the algorithm that is used by the OS to select which thread will handle the signal is the following: find the first thread that is not blocking or ignoring the signal selected randomly, but first start from the thread that is now on the processor. Use these assumptions above to answer the questions below the pseudo-code presented. Remember, for some of the questions you may need to explore numerous scenarios.
#include
#include
#include
#include
#include
atomiclock=0;
typedef struct data {
char name[10];
} data;
void sig_func(int sig){
write(1,Caught signal no =%d
, sig);
signal(sig, sig_func);
}
void sig_func2(int sig){
write(1,Caught signal no =%d
, sig);
}
void func(data *p){
int x;
snprintf(p->name,10,%d,(int)pthread_self()); // could also do itoa();
x =(int)pthead_self()/((getpid()*(-1+ lock++));
sleep(10); // sleep to catch the signals
}
int main(){/* A0*/
pthread_t tid1, tid2, tid3;
data *ptr;
int pid;
signal (SIGINT, SIG_IGN);
pthread_create(&tid1,0,(void*)func, ptr);
signal(SIGSEGV, sig_func);
signal(SIGSTOP, sig_func);
pthread_create(&tid2,0,(void*)func, ptr);
signal(SIGFPE, sig_func);
signal(SIGINT, sig_func2);
pthread_create(&tid3,0,(void*)func, ptr);
/* A */
pid = getpid();
sleep(10); //Leave time for initializations and executing func for all threads /* B */
pthread_kill(tid1, SIGSEGV); // Line A
pthread_kill(tid2, SIGSTOP); // Line B
pthread_kill(tid2, SIGSEGV); // Line C
pthread_kill(tid1, SIGINT); // Line D
pthread_kill(tid3, SIGINT); // Line E
sleep(20);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);
}
Part 1: (2 pts)
Q1: Which of the three threads is the first to execute func? Why?
Q2: Assume that thread tid3 executes func first (ignore lines after /*A*/). If there is action, who triggers it and who is the recipient? How is it handled?
Part 2: (3 pts)
Once all threads are created, move on to part /* B */. For every line in question, ignore all the rest of the lines and describe what happens in main (e.g., when asked about line C, ignore lines: A, B, D, E): what signal if any, how and by who it gets handled. Are there more than one possibilities?
Q1: Line B: Execute 3 times in a row:
Q2: Line D: Execute 3 times in a row:
Part 3: (3 pts)
Ignore lines after /* B */. Assume that another user opened a terminal to send to process pid the following signals 3 times each. How do you IMAGINE (do not run this because the above program is pseudo-code) the delivered signal is handled by pid every time? Is there more than one possibility? If so which and why? If not, why not? Q1: kill (pid,2);
Q2: kill (pid,11);
Q3: kill (pid,3);
Solution:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!