Added Topic 6

This commit is contained in:
Sergiusz Warga 2021-04-03 20:08:15 +02:00
parent 5d52ecddc6
commit b2342ecade
8 changed files with 137 additions and 0 deletions

BIN
Topic-6/task_6_1 Executable file

Binary file not shown.

32
Topic-6/task_6_1.c Normal file
View File

@ -0,0 +1,32 @@
//
// Written for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
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;
}

BIN
Topic-6/task_6_2 Executable file

Binary file not shown.

17
Topic-6/task_6_2.c Normal file
View File

@ -0,0 +1,17 @@
//
// Written for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#include <stdio.h>
#include <unistd.h>
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;
}

BIN
Topic-6/task_6_3 Executable file

Binary file not shown.

44
Topic-6/task_6_3.c Normal file
View File

@ -0,0 +1,44 @@
//
// Written for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
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;
}

BIN
Topic-6/task_6_4 Executable file

Binary file not shown.

44
Topic-6/task_6_4.c Normal file
View File

@ -0,0 +1,44 @@
//
// Written for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
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;
}