init commit
This commit is contained in:
Executable
+30
@@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <wait.h>
|
||||
|
||||
// cmd < file1 >> file2
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
char *file1 = argv[2];
|
||||
char *file2 = argv[3];
|
||||
char *cmd = argv[1];
|
||||
|
||||
if (!fork()) {
|
||||
int fin = open(file1, O_RDONLY);
|
||||
int fout = open(file2, O_WRONLY | O_APPEND | O_CREAT, 0666);
|
||||
|
||||
dup2(fin, 0);
|
||||
dup2(fout, 1);
|
||||
|
||||
execlp(cmd, cmd, NULL);
|
||||
_exit(1);
|
||||
} else {
|
||||
wait(NULL);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+39
@@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <wait.h>
|
||||
|
||||
// cmd1 | cmd2
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
char *cmd1 = argv[1];
|
||||
char *cmd2 = argv[2];
|
||||
|
||||
int fd[2];
|
||||
pipe(fd);
|
||||
|
||||
if (!fork()) {
|
||||
dup2(fd[1], 1);
|
||||
close(fd[0]);
|
||||
|
||||
execlp(cmd1, cmd1, NULL);
|
||||
_exit(1);
|
||||
} else if (!fork()) {
|
||||
dup2(fd[0], 0);
|
||||
close(fd[1]);
|
||||
|
||||
execlp(cmd2, cmd2, NULL);
|
||||
_exit(1);
|
||||
} else {
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
}
|
||||
|
||||
while (wait(NULL) != -1)
|
||||
;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+63
@@ -0,0 +1,63 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <wait.h>
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
int n = strtoll(argv[1], NULL, 10);
|
||||
|
||||
int fd1[2];
|
||||
int fd2[2];
|
||||
pipe(fd1);
|
||||
pipe(fd2);
|
||||
|
||||
int tmp = 1;
|
||||
write(fd2[1], &tmp, sizeof(tmp));
|
||||
|
||||
if (!fork()) {
|
||||
int i = 0;
|
||||
close(fd1[0]);
|
||||
close(fd2[1]);
|
||||
|
||||
while (i < n) {
|
||||
read(fd2[0], &i, sizeof(i));
|
||||
if (i > n) {
|
||||
break;
|
||||
}
|
||||
printf("Channel %d: %d\n", 1, i);
|
||||
i++;
|
||||
write(fd1[1], &i, sizeof(i));
|
||||
}
|
||||
|
||||
return 0;
|
||||
} else if (!fork()) {
|
||||
int i = 0;
|
||||
close(fd2[0]);
|
||||
close(fd1[1]);
|
||||
|
||||
while (i < n) {
|
||||
read(fd1[0], &i, sizeof(i));
|
||||
if (i > n) {
|
||||
break;
|
||||
}
|
||||
printf("Channel %d: %d\n", 2, i);
|
||||
i++;
|
||||
write(fd2[1], &i, sizeof(i));
|
||||
}
|
||||
|
||||
return 0;
|
||||
} else {
|
||||
close(fd1[0]);
|
||||
close(fd1[1]);
|
||||
close(fd2[0]);
|
||||
close(fd2[1]);
|
||||
}
|
||||
|
||||
while (wait(NULL) != -1)
|
||||
;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user