init commit
This commit is contained in:
Executable
+35
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
|
||||
volatile int cnt = 0;
|
||||
|
||||
void
|
||||
f(int n)
|
||||
{
|
||||
if (cnt == 5) {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
printf("%d\n", cnt++);
|
||||
fflush(stdout);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
sigaction(SIGHUP, &(struct sigaction){.sa_handler = f, .sa_flags = SA_RESTART}, NULL);
|
||||
|
||||
printf("%d\n", getpid());
|
||||
fflush(stdout);
|
||||
|
||||
for (;;) {
|
||||
pause();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+51
@@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
|
||||
volatile int mode = 0; // 0 - + 1 - *
|
||||
|
||||
void
|
||||
mul(int n)
|
||||
{
|
||||
mode = 1;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
sum(int n)
|
||||
{
|
||||
mode = 0;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
sigaction(SIGINT, &(struct sigaction){.sa_handler = sum, .sa_flags = SA_RESTART}, NULL);
|
||||
sigaction(SIGQUIT, &(struct sigaction){.sa_handler = mul, .sa_flags = SA_RESTART}, NULL);
|
||||
|
||||
printf("%d\n", getpid());
|
||||
fflush(stdout);
|
||||
|
||||
int res = 0, tmp;
|
||||
|
||||
while (scanf("%d", &tmp) == 1) {
|
||||
if (mode == 0) {
|
||||
__builtin_add_overflow(res, tmp, &res);
|
||||
} else {
|
||||
__builtin_mul_overflow(res, tmp, &res);
|
||||
}
|
||||
printf("%d\n", res);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
// for (;;) {
|
||||
// pause();
|
||||
// }
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+68
@@ -0,0 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <wait.h>
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int n = (int) strtoll(argv[1], NULL, 10);
|
||||
|
||||
int fd[2];
|
||||
pipe(fd);
|
||||
|
||||
int tmp = 1;
|
||||
write(fd2[1], &tmp, sizeof(tmp));
|
||||
|
||||
if (!fork()) {
|
||||
int i = 0;
|
||||
close(fd[0]);
|
||||
|
||||
while (i < n) {
|
||||
if (read(fd2[0], &i, sizeof(i)) == -1) {
|
||||
return 0;
|
||||
}
|
||||
if (i >= n) {
|
||||
write(fd1[1], &i, sizeof(i));
|
||||
break;
|
||||
}
|
||||
printf("%d %d\n", 1, i);
|
||||
fflush(stdout);
|
||||
i++;
|
||||
write(fd1[1], &i, sizeof(i));
|
||||
}
|
||||
|
||||
return 0;
|
||||
} else if (!fork()) {
|
||||
int i = 0;
|
||||
close(fd2[0]);
|
||||
close(fd1[1]);
|
||||
|
||||
while (i < n) {
|
||||
if (read(fd1[0], &i, sizeof(i)) == -1) {
|
||||
return 0;
|
||||
}
|
||||
if (i >= n) {
|
||||
write(fd1[1], &i, sizeof(i));
|
||||
break;
|
||||
}
|
||||
printf("%d %d\n", 2, i);
|
||||
fflush(stdout);
|
||||
i++;
|
||||
write(fd2[1], &i, sizeof(i));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
|
||||
while (wait(NULL) != -1)
|
||||
;
|
||||
|
||||
printf("Done\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+75
@@ -0,0 +1,75 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
volatile sig_atomic_t sigusr1_received = 0;
|
||||
|
||||
void
|
||||
sigusr1_handler(int signum)
|
||||
{
|
||||
sigusr1_received = 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int n = (int) strtol(argv[1], NULL, 10);
|
||||
|
||||
int fd[2];
|
||||
pipe(fd);
|
||||
|
||||
signal(SIGUSR1, sigusr1_handler);
|
||||
|
||||
if (!fork()) {
|
||||
close(fd[0]);
|
||||
FILE *out = fdopen(fd[1], "w");
|
||||
|
||||
int i = 1;
|
||||
while (i <= n) {
|
||||
printf("1 %d\n", i);
|
||||
fflush(stdout);
|
||||
fprintf(out, "%d\n", i);
|
||||
fflush(out);
|
||||
kill(getppid(), SIGUSR1);
|
||||
pause();
|
||||
if (i >= n) {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
fclose(out);
|
||||
return 0;
|
||||
} else if (!fork()) {
|
||||
close(fd[1]);
|
||||
FILE *in = fdopen(fd[0], "r");
|
||||
|
||||
int i;
|
||||
while (1) {
|
||||
if (i >= n) {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
printf("2 %d\n", i);
|
||||
fflush(stdout);
|
||||
kill(getppid(), SIGUSR1);
|
||||
pause();
|
||||
}
|
||||
|
||||
fclose(in);
|
||||
return 0;
|
||||
}
|
||||
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
|
||||
while (wait(NULL) != -1)
|
||||
;
|
||||
|
||||
printf("Done\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+72
@@ -0,0 +1,72 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
|
||||
volatile int last_prime = 0;
|
||||
volatile int cnt = 0;
|
||||
|
||||
void
|
||||
f(int n)
|
||||
{
|
||||
if (++cnt == 4) {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
printf("%d\n", last_prime);
|
||||
fflush(stdout);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
exit_term(int n)
|
||||
{
|
||||
exit(0);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int
|
||||
is_prime(int n)
|
||||
{
|
||||
if (n < 2) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (int i = 2; i * i <= n; ++i) {
|
||||
if (n % i == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
sigaction(SIGINT, &(struct sigaction){.sa_handler = f, .sa_flags = SA_RESTART}, NULL);
|
||||
sigaction(SIGTERM, &(struct sigaction){.sa_handler = exit_term, .sa_flags = SA_RESTART}, NULL);
|
||||
|
||||
printf("%d\n", getpid());
|
||||
fflush(stdout);
|
||||
|
||||
int a, b;
|
||||
scanf("%d", &a);
|
||||
scanf("%d", &b);
|
||||
|
||||
a = (a < 2) ? 2 : a;
|
||||
|
||||
for (int i = a; i < b; ++i) {
|
||||
if (is_prime(i)) {
|
||||
last_prime = i;
|
||||
}
|
||||
}
|
||||
|
||||
printf("-1\n");
|
||||
fflush(stdout);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+34
@@ -0,0 +1,34 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
volatile int count = 0;
|
||||
|
||||
void
|
||||
func(int sign)
|
||||
{
|
||||
if (count < 5) {
|
||||
printf("%d\n", count);
|
||||
fflush(stdout);
|
||||
count++;
|
||||
} else {
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
printf("%d\n", getpid());
|
||||
fflush(stdout);
|
||||
|
||||
if (signal(SIGHUP, func) == SIG_ERR) {
|
||||
exit(1);
|
||||
}
|
||||
for (;;) {
|
||||
pause();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user