diff --git a/Topic-6/task_6_1 b/Topic-6/task_6_1 new file mode 100755 index 0000000..3161e19 Binary files /dev/null and b/Topic-6/task_6_1 differ diff --git a/Topic-6/task_6_1.c b/Topic-6/task_6_1.c new file mode 100644 index 0000000..43f6898 --- /dev/null +++ b/Topic-6/task_6_1.c @@ -0,0 +1,32 @@ +// +// Written for Computer Networks and Systems lab classes +// AUTHOR : Sergiusz Warga + +#include +#include +#include +#include + +void child_task() { + execl("/usr/bin/cal", "cal", "2021", NULL); +} + +int main() { + pid_t pid; + int status; + + pid = fork(); + + if (pid == -1) { // If something went wrong. + perror("Fork: "); + exit(EXIT_FAILURE); + } + + if (pid == 0) { // If this is a child process. + child_task(); + exit(EXIT_SUCCESS); + } + + wait(&status); + return 0; +} diff --git a/Topic-6/task_6_2 b/Topic-6/task_6_2 new file mode 100755 index 0000000..ddeb2b8 Binary files /dev/null and b/Topic-6/task_6_2 differ diff --git a/Topic-6/task_6_2.c b/Topic-6/task_6_2.c new file mode 100644 index 0000000..1870ef8 --- /dev/null +++ b/Topic-6/task_6_2.c @@ -0,0 +1,17 @@ +// +// Written for Computer Networks and Systems lab classes +// AUTHOR : Sergiusz Warga + +#include +#include + +int main() { + char msg_write[] = "write() to dup(1)\n"; + int soutfd = dup(STDOUT_FILENO); + write(soutfd, msg_write, sizeof(msg_write)); + + FILE *fakeout = fdopen(soutfd, "w"); + fprintf(fakeout, "using fprintf() on fakeout\n"); + + return 0; +} \ No newline at end of file diff --git a/Topic-6/task_6_3 b/Topic-6/task_6_3 new file mode 100755 index 0000000..ca80d4f Binary files /dev/null and b/Topic-6/task_6_3 differ diff --git a/Topic-6/task_6_3.c b/Topic-6/task_6_3.c new file mode 100644 index 0000000..1458184 --- /dev/null +++ b/Topic-6/task_6_3.c @@ -0,0 +1,44 @@ +// +// Written for Computer Networks and Systems lab classes +// AUTHOR : Sergiusz Warga + +#include +#include +#include +#include +#include + + +int main() { + int pipefd[2]; + if (pipe(pipefd) < 0) { + fprintf(stderr, "Something went wrong!\n"); + return -1; + } + + pid_t pid; + pid = fork(); + + if (pid == -1) { // If something went wrong. + perror("Fork: "); + exit(EXIT_FAILURE); + } + + if (pid == 0) { // If this is a child process. + dup2(pipefd[1], STDOUT_FILENO); + printf("printf() from a child\n"); + close(pipefd[1]); + exit(EXIT_SUCCESS); + } + + char buff[100] = {}; + + while (read(pipefd[0], &buff, sizeof(buff)) > 0) { + printf("PARENT: %s", buff); + } + + int status; + wait(&status); + + return 0; +} \ No newline at end of file diff --git a/Topic-6/task_6_4 b/Topic-6/task_6_4 new file mode 100755 index 0000000..538f6d2 Binary files /dev/null and b/Topic-6/task_6_4 differ diff --git a/Topic-6/task_6_4.c b/Topic-6/task_6_4.c new file mode 100644 index 0000000..f66a970 --- /dev/null +++ b/Topic-6/task_6_4.c @@ -0,0 +1,44 @@ +// +// Written for Computer Networks and Systems lab classes +// AUTHOR : Sergiusz Warga + +#include +#include +#include +#include +#include + + +int main() { + int pipefd[2]; + if (pipe(pipefd) < 0) { + fprintf(stderr, "Something went wrong!\n"); + return -1; + } + + pid_t pid; + pid = fork(); + + if (pid == -1) { // If something went wrong. + perror("Fork: "); + exit(EXIT_FAILURE); + } + + if (pid == 0) { // If this is a child process. + dup2(pipefd[1], STDOUT_FILENO); + execl("/usr/bin/cal", "cal", NULL); + close(pipefd[1]); + exit(EXIT_SUCCESS); + } + + char buff[200] = {}; + + while (read(pipefd[0], &buff, sizeof(buff)) > 0) { + printf("%s", buff); + } + + int status; + wait(&status); + + return 0; +} \ No newline at end of file