summaryrefslogtreecommitdiffstats
path: root/shell/beispiele
diff options
context:
space:
mode:
authorStefan Zabka <zabkaste@hu-berlin.de>2020-05-24 12:19:52 +0200
committerStefan Zabka <zabkaste@hu-berlin.de>2020-05-24 12:19:52 +0200
commit04576dc2a3f761eb041b808b56f13a58052e7655 (patch)
treec6bed6db34e29f0dec0c844fbc931b3dd4fab8db /shell/beispiele
parent65966ded0cc15c5966c6568cf0ff2f2bbe1fc29a (diff)
downloadbetriebssysteme-04576dc2a3f761eb041b808b56f13a58052e7655.tar.gz
betriebssysteme-04576dc2a3f761eb041b808b56f13a58052e7655.zip
Moved back to 02_exercise
Diffstat (limited to 'shell/beispiele')
-rw-r--r--shell/beispiele/dup/Makefile18
-rw-r--r--shell/beispiele/dup/dup.c51
-rw-r--r--shell/beispiele/fork_example/Makefile18
-rw-r--r--shell/beispiele/fork_example/fork.c36
-rw-r--r--shell/beispiele/fork_signal/Makefile19
-rw-r--r--shell/beispiele/fork_signal/fork.c39
-rw-r--r--shell/beispiele/fork_signal/signal.c21
-rw-r--r--shell/beispiele/pipe_bidirect/Makefile19
-rw-r--r--shell/beispiele/pipe_bidirect/log.txt19
-rw-r--r--shell/beispiele/pipe_bidirect/pingpong.c125
-rw-r--r--shell/beispiele/pipe_example/Makefile18
-rw-r--r--shell/beispiele/pipe_example/pipe.c60
12 files changed, 0 insertions, 443 deletions
diff --git a/shell/beispiele/dup/Makefile b/shell/beispiele/dup/Makefile
deleted file mode 100644
index 0f73ac4..0000000
--- a/shell/beispiele/dup/Makefile
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/usr/bin/make
-.SUFFIXES:
-
-CFLAGS = -c -Os -Wall -Werror
-
-%.o: %.c
- $(CC) $(CFLAGS) $^ -o $@
-
-%: %.o
- $(CC) -o $@ $^
-
-all: dup
-
-run: all
- ./dup
-
-clean:
- $(RM) $(RMFILES) dup
diff --git a/shell/beispiele/dup/dup.c b/shell/beispiele/dup/dup.c
deleted file mode 100644
index ea3c5a6..0000000
--- a/shell/beispiele/dup/dup.c
+++ /dev/null
@@ -1,51 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-/* https://www.youtube.com/watch?v=gaXigSu72A4 */
-static inline void die(const char* msg) {
- perror(msg);
- exit(EXIT_FAILURE);
-}
-
-int main(int argc, char **argv) {
- int pdes[2];
- pid_t child;
-
- if (pipe(pdes) < 0)
- die("pipe()");
-
- if ((child = fork()) < 0)
- die("fork()");
-
- if (child == 0) {
- /* child process */
- close(pdes[0]);
- close(1); /* close stdout */
-
- if (dup(pdes[1]) < 0)
- die("dup()");
-
- /* now stdout and pdes[1] are equivalent (dup returns lowest free descriptor) */
- if (execlp("cat", "cat", "/etc/passwd", NULL) < 0)
- die("execlp()");
-
- exit(EXIT_SUCCESS);
- }
- else {
- /* parent process */
- close(0); /* close stdin */
- close(pdes[1]);
-
- if (dup(pdes[0]) < 0)
- die("dup()");
-
- /* now stdin and pdes[0] are equivalent (dup returns lowest free descriptor) */
- if (execlp("wc", "wc", "-l", NULL) < 0)
- die("execlp()");
-
- exit(EXIT_SUCCESS);
- }
-
- return 0;
-}
diff --git a/shell/beispiele/fork_example/Makefile b/shell/beispiele/fork_example/Makefile
deleted file mode 100644
index 8f69ed9..0000000
--- a/shell/beispiele/fork_example/Makefile
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/usr/bin/make
-.SUFFIXES:
-
-CFLAGS = -c -Os -Wall -Werror
-
-%.o: %.c
- $(CC) $(CFLAGS) $^ -o $@
-
-%: %.o
- $(CC) -o $@ $^
-
-all: fork
-
-run: all
- ./fork
-
-clean:
- $(RM) $(RMFILES) fork
diff --git a/shell/beispiele/fork_example/fork.c b/shell/beispiele/fork_example/fork.c
deleted file mode 100644
index 7377b0f..0000000
--- a/shell/beispiele/fork_example/fork.c
+++ /dev/null
@@ -1,36 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/wait.h>
-
-int main(void) {
- pid_t proc_id;
- int status = 0;
-
- proc_id = fork();
-
- if (proc_id < 0) {
- fprintf(stderr, "fork error\n");
- fflush(stderr);
- return EXIT_FAILURE;
- }
-
- if (proc_id == 0) {
- /* child process */
- printf("[child] process id: %d\n", (int) getpid());
-
- char* args[] = {"sleep", "1", NULL};
- execvp(args[0], args);
- exit(-1);
- }
- else {
- /* parent */
- printf("[parent] process id: %d\n", (int) getpid());
- pid_t child_id = wait(&status);
-
- printf("[parent] child %d returned: %d\n",
- child_id, WEXITSTATUS(status));
- }
-
- return EXIT_SUCCESS;
-}
diff --git a/shell/beispiele/fork_signal/Makefile b/shell/beispiele/fork_signal/Makefile
deleted file mode 100644
index 499b8cb..0000000
--- a/shell/beispiele/fork_signal/Makefile
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/usr/bin/make
-.SUFFIXES:
-
-CFLAGS = -c -O0
-
-%.o: %.c
- $(CC) $(CFLAGS) $^ -o $@
-
-%: %.o
- $(CC) -o $@ $^
-
-all: fork signal
-
-run: all
- ./fork 10
- ./signal
-
-clean:
- $(RM) $(RMFILES) fork signal
diff --git a/shell/beispiele/fork_signal/fork.c b/shell/beispiele/fork_signal/fork.c
deleted file mode 100644
index b144577..0000000
--- a/shell/beispiele/fork_signal/fork.c
+++ /dev/null
@@ -1,39 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-
-int main(int argc, const char* argv[]) {
- pid_t pid;
- int h;
- int count;
-
- if (argc < 2) {
- printf("Usage: ./fork process#\n");
- return 0;
- }
-
- count = atoi(argv[1]) - 1;
- printf("Process#: %d\n", count + 1);
- printf("Parent started\n");
-
- pid = 1;
- for (h = 0; h < count; ++h) {
- if ((pid = fork()) < 0)
- printf("fork error\n");
- else if (pid == 0) {
- printf("Child %d started\n", h);
- sleep(1);
- printf("Child %d finished\n", h);
- exit(0);
- }
- }
-
- printf("Parent waiting\n");
- for (h = 0; h < count; ++h) {
- wait(NULL);
- }
- printf("Parent finished\n");
- return EXIT_SUCCESS;
-}
diff --git a/shell/beispiele/fork_signal/signal.c b/shell/beispiele/fork_signal/signal.c
deleted file mode 100644
index 500d2e1..0000000
--- a/shell/beispiele/fork_signal/signal.c
+++ /dev/null
@@ -1,21 +0,0 @@
-#include <unistd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <signal.h>
-
-void sig_handler(int signo) {
- const char msg[] = "Speicherzugriffsfehler!\n";
- write(2, msg, sizeof(msg) - 1);
- exit(-1);
-}
-
-int main(void) {
- if (signal(SIGSEGV, sig_handler) == SIG_ERR) {
- perror("signal");
- exit(-1);
- }
-
- int array[1] = {0};
- array[1000000] = 3;
- return 0;
-}
diff --git a/shell/beispiele/pipe_bidirect/Makefile b/shell/beispiele/pipe_bidirect/Makefile
deleted file mode 100644
index 361ac97..0000000
--- a/shell/beispiele/pipe_bidirect/Makefile
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/usr/bin/make
-.SUFFIXES:
-
-CFLAGS = -c -Os -Wall -Werror
-
-%.o: %.c
- $(CC) $(CFLAGS) $^ -o $@
-
-%: %.o
- $(CC) -o $@ $^
-
-all: pingpong
-
-run: all
- @echo "writing into log.txt; stop execution with CTRL+C"
- ./pingpong
-
-clean:
- $(RM) $(RMFILES) pingpong
diff --git a/shell/beispiele/pipe_bidirect/log.txt b/shell/beispiele/pipe_bidirect/log.txt
deleted file mode 100644
index c59b0bb..0000000
--- a/shell/beispiele/pipe_bidirect/log.txt
+++ /dev/null
@@ -1,19 +0,0 @@
-[3225581]: ping
-[3225580]: pong
-[3225581]: ping
-[3225580]: pong
-[3225581]: ping
-[3225580]: pong
-[3225581]: ping
-[3225580]: pong
-[3225581]: ping
-[3225580]: pong
-[3225581]: ping
-[3225580]: pong
-[3225581]: ping
-[3225580]: pong
-[3225581]: ping
-[3225580]: pong
-[3225581]: ping
-[3225580]: pong
-Programmabbruch durch Nutzer
diff --git a/shell/beispiele/pipe_bidirect/pingpong.c b/shell/beispiele/pipe_bidirect/pingpong.c
deleted file mode 100644
index f38394f..0000000
--- a/shell/beispiele/pipe_bidirect/pingpong.c
+++ /dev/null
@@ -1,125 +0,0 @@
-#include <fcntl.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-static int split(int fd[2]);
-static int redir(int fd, const char *file);
-static void sig_handler(int sig);
-
-static int parent;
-
-int main(void) {
- ssize_t bytes;
- char buf[256];
- int fd[2];
-
- // disable buffering for stdout
- setvbuf(stdout, NULL, _IONBF, 0);
-
- // redirect stdout to write into a file
- if (redir(1, "log.txt") < 0) {
- perror("redir()");
- exit(-1);
- }
-
- // install the signal handler for Ctrl+C
- if (signal(SIGINT, sig_handler) == SIG_ERR) {
- perror("signal()");
- exit(-1);
- }
-
- // split processes and assign file descriptors for communication
- switch (split(fd)) {
- case -1: // error
- exit(-1);
-
- case 0: // child
- while ((bytes = read(fd[0], buf, sizeof(buf))) > 0) {
- printf("[%u]: %s\n", getpid(), buf);
- sleep(1);
- write(fd[1], "pong", 5);
- }
- break;
-
- case 1: // parent
- parent = 1;
- write(fd[1], "ping", 5);
- while ((bytes = read(fd[0], buf, sizeof(buf))) > 0) {
- printf("[%u]: %s\n", getpid(), buf);
- sleep(1);
- write(fd[1], "ping", 5);
- }
- break;
- }
-
- // close the file descriptors
- close(fd[0]);
- close(fd[1]);
-
- return 0;
-}
-
-int split(int fd[2]) {
- int pfd[2][2];
-
- if (pipe(pfd[0]) < 0) {
- perror("pipe()");
- goto err_pipe1;
- }
-
- if (pipe(pfd[1]) < 0) {
- perror("pipe()");
- goto err_pipe2;
- }
-
- switch (fork()) {
- case -1:
- perror("fork()");
- goto err_fork;
-
- case 0: // child process
- close(pfd[0][1]); // close write for child
- close(pfd[1][0]); // close read for parent
- fd[0] = pfd[0][0]; // keep read for child
- fd[1] = pfd[1][1]; // keep write for parent
- return 0;
-
- default: // parent process
- close(pfd[1][1]); // close write for parent
- close(pfd[0][0]); // close read for child
- fd[0] = pfd[1][0]; // keep read for parent
- fd[1] = pfd[0][1]; // keep write for child
- return 1;
- }
-
-err_fork:
- close(pfd[1][0]);
- close(pfd[1][1]);
-err_pipe2:
- close(pfd[0][0]);
- close(pfd[0][1]);
-err_pipe1:
- return -1;
-}
-
-int redir(int fd, const char *file) {
- int fd2 = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
-
- if (fd2 < 0)
- return -1;
-
- dup2(fd2, fd);
- close(fd2);
-
- return 0;
-}
-
-void sig_handler(int sig) {
- if (parent) {
- const char msg[] = "Programmabbruch durch Nutzer\n";
- write(1, msg, sizeof(msg) - 1);
- }
- exit(0);
-}
diff --git a/shell/beispiele/pipe_example/Makefile b/shell/beispiele/pipe_example/Makefile
deleted file mode 100644
index d69473e..0000000
--- a/shell/beispiele/pipe_example/Makefile
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/usr/bin/make
-.SUFFIXES:
-
-CFLAGS = -c -Os -Wall -Werror
-
-%.o: %.c
- $(CC) $(CFLAGS) $^ -o $@
-
-%: %.o
- $(CC) -o $@ $^
-
-all: pipe
-
-run: all
- ./pipe
-
-clean:
- $(RM) $(RMFILES) pipe
diff --git a/shell/beispiele/pipe_example/pipe.c b/shell/beispiele/pipe_example/pipe.c
deleted file mode 100644
index 422e4d5..0000000
--- a/shell/beispiele/pipe_example/pipe.c
+++ /dev/null
@@ -1,60 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#define BUF_SIZE 32
-
-int pipe_fds[2];
-
-void term(const char *msg) {
- if (msg) {
- fprintf(stderr, "Error: %s\n", msg);
- fflush(stderr);
- }
-
- int close_fail = 0;
- int ret = close(pipe_fds[0]);
-
- if (ret < 0) {
- fprintf(stderr, "Error: Couldn't close reading end of pipe\n");
- fflush(stderr);
- close_fail = 1;
- }
-
- ret = close(pipe_fds[1]);
-
- if (ret < 0) {
- fprintf(stderr, "Error: Couldn't close writing end of pipe\n");
- fflush(stderr);
- close_fail = 1;
- }
-
- exit((msg || close_fail) ? EXIT_FAILURE : EXIT_SUCCESS);
-}
-
-int main(void) {
- int ret = pipe(pipe_fds);
-
- if (ret < 0)
- term("Couldn't create pipe");
-
- char out_buf[] = "hello";
- ret = write(pipe_fds[1], out_buf, strlen(out_buf) + 1);
-
- if (ret < 0)
- term("Couldn't write to pipe");
-
- printf("send msg: %s\n", out_buf);
-
- char in_buf[BUF_SIZE];
- memset(in_buf, 0, BUF_SIZE);
-
- ret = read(pipe_fds[0], in_buf, BUF_SIZE - 1);
-
- if (ret < 0)
- term("Couldn't read from pipe");
-
- printf("recv msg: %s\n", in_buf);
- return 0;
-}