Skip to content

Commit

Permalink
Add TP1 Signals code
Browse files Browse the repository at this point in the history
  • Loading branch information
guim4dev committed Jul 27, 2021
1 parent b5450a3 commit 4568ac7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
Binary file added TP1/TP1.pdf
Binary file not shown.
33 changes: 33 additions & 0 deletions TP1/signals/signal_receiver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <sys/signalfd.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

bool keep_running = true;

void sig_handler(int signo) {
cout << "Received signal " << signo << endl;
if (signo == SIGABRT) {
keep_running = false;
}
}

int main(int argc, char* argv[]) {
int type;
cout << "Enter 1 for busy wait and anything else for blocking wait:" << endl;
cin >> type;
signal(SIGUSR1, sig_handler);
signal(SIGUSR2, sig_handler);
signal(SIGABRT, sig_handler);
cout << "My pid is: " << getpid() << endl;
if(type == 1) {
while (keep_running);
} else {
while(keep_running)
pause();
}
return 0;
}
24 changes: 24 additions & 0 deletions TP1/signals/signal_sender.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <sys/signalfd.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include <errno.h>

using namespace std;

int main(int argc, char* argv[]){
int pid, signal;
cout << "Enter pid and signal" << endl;
cin >> pid >> signal;
if(kill(pid,signal) == -1){
if(errno==ESRCH){
cout << "PID inexistente" << endl;
}
else {
cout << "Ocorreu um erro" << endl;
}
return 1;
}
return 0;
}

0 comments on commit 4568ac7

Please sign in to comment.