init commit
This commit is contained in:
Executable
+22
@@ -0,0 +1,22 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <wait.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
proc(void)
|
||||
{
|
||||
int pid = fork();
|
||||
if (!pid) {
|
||||
write(1, "1\n", 2);
|
||||
}
|
||||
return pid;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
proc(), proc(), proc();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
proc(), proc(), proc()
|
||||
Executable
+35
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <wait.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
pid_t pid;
|
||||
pid = fork();
|
||||
if (!pid) {
|
||||
pid = fork();
|
||||
if (!pid) {
|
||||
printf("3 ");
|
||||
|
||||
return 0;
|
||||
} else if (pid == -1) {
|
||||
return 1;
|
||||
} else {
|
||||
wait(NULL);
|
||||
printf("2 ");
|
||||
}
|
||||
return 0;
|
||||
} else if (pid == -1) {
|
||||
return 1;
|
||||
} else {
|
||||
wait(NULL);
|
||||
printf("1");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+39
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
enum
|
||||
{
|
||||
MAX_LEN = 8
|
||||
};
|
||||
|
||||
void
|
||||
process_line(int line_number)
|
||||
{
|
||||
char buffer[MAX_LEN + 1] = {0};
|
||||
read(0, buffer, sizeof(buffer) - 1);
|
||||
long long number = strtoll(buffer, NULL, 10);
|
||||
long long square = number * number;
|
||||
printf("%d %lld\n", line_number, square);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
pid_t pid;
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if ((pid = fork()) == 0) {
|
||||
process_line(i + 1);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
while (wait(NULL) != -1)
|
||||
;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
3
|
||||
Executable
+38
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
pid_t pid;
|
||||
|
||||
int n;
|
||||
if (scanf("%d", &n) != 1) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
printf("%d", i);
|
||||
fflush(stdout);
|
||||
|
||||
if (i == n) {
|
||||
printf("\n");
|
||||
return 0;
|
||||
} else {
|
||||
printf(" ");
|
||||
fflush(stdout);
|
||||
}
|
||||
pid = fork();
|
||||
|
||||
if (!pid) {
|
||||
continue;
|
||||
} else {
|
||||
wait(NULL);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+1
@@ -0,0 +1 @@
|
||||
1 2 3
|
||||
Executable
+43
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <wait.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
|
||||
pid_t pid;
|
||||
int n;
|
||||
|
||||
pid = fork();
|
||||
if (!pid) {
|
||||
for (;;) {
|
||||
if (scanf("%d", &n) != 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
pid = fork();
|
||||
if (!pid) {
|
||||
continue;
|
||||
}
|
||||
int status;
|
||||
wait(&status);
|
||||
if (pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("%d\n", n);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
int status;
|
||||
wait(&status);
|
||||
if (pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status)) {
|
||||
printf("-1\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user