ostep: add hw5

This commit is contained in:
Kyle Isom 2018-09-05 18:32:57 -07:00
parent 72f3484804
commit 22f71e1c99
7 changed files with 264 additions and 1 deletions

View File

@ -1,4 +1,10 @@
TARGETS := hw5_1
TARGETS := hw5_1 \
hw5_2 \
hw5_3 \
hw5_5 \
hw5_6 \
hw5_7 \
hw5_8
all: $(TARGETS)

49
ostep/hw5/hw5_2.c Normal file
View File

@ -0,0 +1,49 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
const char default_path[] = "hello.txt";
int
main(int argc, char *argv[])
{
char buf[2] = {0x41, 0x0a};
char *pathname = (char *)default_path;
int fd = -1;
if (argc > 1) {
pathname = argv[1];
}
fd = open(pathname, O_WRONLY|O_CREAT, S_IWUSR|S_IRUSR);
if (fd == -1) {
perror("open");
exit(1);
}
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
else if (pid > 0) {
for (int i = 0; i < 5; i++) {
write(fd, buf, 2);
sleep(1);
}
}
else {
buf[0] = 0x42;
for (int i = 0; i < 5; i++) {
write(fd, buf, 2);
sleep(1);
}
}
close(fd);
exit(0);
}

34
ostep/hw5/hw5_3.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(void)
{
int pipefd[2];
if (pipe(pipefd) == -1) {
perror("pipe");
exit(1);
}
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
else if (pid > 0) {
close(pipefd[1]);
char buf[1];
read(pipefd[0], buf, 1);
printf("goodbye\n");
}
else {
sleep(1);
close(pipefd[0]);
printf("hello\n");
close(pipefd[1]);
}
}

33
ostep/hw5/hw5_5.c Normal file
View File

@ -0,0 +1,33 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(void)
{
int status = 0;
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
else if (pid > 0) {
wait(&status);
if (!WIFEXITED(status)) {
fprintf(stderr, "child exited due to an error\n");
}
printf("goodbye: %d\n", status);
}
else {
wait(&status);
if (!WIFEXITED(status)) {
fprintf(stderr, "child exited due to an error\n");
}
printf("hello: %d\n", status);
}
exit(EXIT_SUCCESS);
}

29
ostep/hw5/hw5_6.c Normal file
View File

@ -0,0 +1,29 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(void)
{
int status = 0;
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
else if (pid > 0) {
waitpid(pid, &status, 0);
if (!WIFEXITED(status)) {
fprintf(stderr, "child exited due to an error\n");
}
printf("goodbye: %d\n", status);
}
else {
printf("hello: %d\n", status);
}
exit(EXIT_SUCCESS);
}

27
ostep/hw5/hw5_7.c Normal file
View File

@ -0,0 +1,27 @@
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(void)
{
pid_t pid;
pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
// child
else if (pid > 0) {
close(STDOUT_FILENO);
printf("screaming into the void...\n");
}
else {
wait(NULL);
printf("the darkness\n");
}
exit(EXIT_SUCCESS);
}

85
ostep/hw5/hw5_8.c Normal file
View File

@ -0,0 +1,85 @@
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int pipefd[2];
int
main(void)
{
if (pipe(pipefd) != 0) {
perror("pipe");
return EXIT_FAILURE;
}
int pwrite = pipefd[1];
int pread = pipefd[0];
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return EXIT_FAILURE;
}
/* the first child */
else if (pid == 0) {
/*
* Close standard output and reassign it to the pipe.
* The close is done automagically by dup2.
*/
dup2(pwrite, STDOUT_FILENO);
/* Close unnecessary pipe read file descriptor. */
close(pread);
/* Now we do the thing. */
printf("Hello, world.\n");
/* ¡Adios! */
return EXIT_SUCCESS;
}
/*
* We are now in the parent. We must fork again, and do the
* child dance.
*/
if ((pid = fork()) < 0) {
perror("fork");
return EXIT_FAILURE;
}
/* the second of the children */
else if (pid == 0) {
/* Close standard input and reassign it to the pipe. */
dup2(pread, STDIN_FILENO);
/* Close unnecessary pipe write file descriptor. */
close(pwrite);
/* Read the thing. */
char data[16];
ssize_t rl;
memset(data, 0, 16);
rl = read(STDIN_FILENO, data, 15);
if (rl < 1) {
perror("read");
return EXIT_FAILURE;
}
printf("%s", data);
return EXIT_SUCCESS;
}
/* wait for the children to die so we can reap them */
/* there are two, they should drop dead on their own. */
int status1, status2;
waitpid(-1, &status1, 0);
waitpid(-1, &status2, 0);
if (WIFEXITED(status1) && WIFEXITED(status2)) {
printf("Goodbye.\n");
return EXIT_SUCCESS;
}
fprintf(stderr, "At least one child died abnormally.\n");
return EXIT_FAILURE;
}