Fixes in lab1
This commit is contained in:
parent
f8286f50b0
commit
25b4284d09
@ -1,15 +1,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <strings.h>
|
||||
|
||||
void wait_clocks(clock_t clocks_to_wait) {
|
||||
const clock_t end = clock() + clocks_to_wait;
|
||||
while (clock() < end);
|
||||
}
|
||||
|
||||
struct ping_pong_data_t {
|
||||
char const *name;
|
||||
@ -23,12 +15,11 @@ void do_ping_pong(int signo) {
|
||||
if (signo != SIGUSR1)
|
||||
return;
|
||||
|
||||
const clock_t delay_clocks = (clock_t) (CLOCKS_PER_SEC * ping_pong_data.delay_seconds);
|
||||
|
||||
printf("[%s] Got signal\n", ping_pong_data.name);
|
||||
printf("[%s] PID=%d\n", ping_pong_data.name, ping_pong_data.pid_to_print);
|
||||
printf("[%s] Another side PID=%d\n", ping_pong_data.name, ping_pong_data.pid_to_print);
|
||||
printf("[%s] Sleeping %lf seconds before sending signal\n", ping_pong_data.name, ping_pong_data.delay_seconds);
|
||||
wait_clocks(delay_clocks);
|
||||
usleep((useconds_t) (1000 * 1000 * ping_pong_data.delay_seconds));
|
||||
printf("[%s] Sending signal\n", ping_pong_data.name);
|
||||
kill(ping_pong_data.pid_to_send, SIGUSR1);
|
||||
}
|
||||
@ -55,20 +46,24 @@ int main(void) {
|
||||
case 0:
|
||||
child_pid = getpid();
|
||||
|
||||
ping_pong_data.name = "c";
|
||||
ping_pong_data.delay_seconds = 2.5;
|
||||
ping_pong_data.name = "\033[36mchild\033[0m";
|
||||
ping_pong_data.delay_seconds = 1.5;
|
||||
ping_pong_data.pid_to_print = parent_pid;
|
||||
ping_pong_data.pid_to_send = parent_pid;
|
||||
|
||||
printf("[%s] My PID=%d\n", ping_pong_data.name, getpid());
|
||||
|
||||
signal(SIGUSR1, do_ping_pong);
|
||||
|
||||
break;
|
||||
default:
|
||||
ping_pong_data.name = "c";
|
||||
ping_pong_data.delay_seconds = 2.5;
|
||||
ping_pong_data.name = "\033[31mparent\033[0m";
|
||||
ping_pong_data.delay_seconds = 1.5;
|
||||
ping_pong_data.pid_to_print = child_pid;
|
||||
ping_pong_data.pid_to_send = child_pid;
|
||||
|
||||
printf("[%s] My PID=%d\n", ping_pong_data.name, getpid());
|
||||
|
||||
signal(SIGUSR1, do_ping_pong);
|
||||
|
||||
kill(parent_pid, SIGUSR1);
|
||||
@ -79,5 +74,7 @@ int main(void) {
|
||||
|
||||
while (is_running)
|
||||
pause();
|
||||
|
||||
printf("[%s] Interrupted\n", ping_pong_data.name);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue
Block a user