add content for sp tutorium lesson 03
This commit is contained in:
parent
1c58c42897
commit
9e3fec2539
1 changed files with 86 additions and 0 deletions
86
src/sp1/ub3.md
Normal file
86
src/sp1/ub3.md
Normal file
|
|
@ -0,0 +1,86 @@
|
||||||
|
---
|
||||||
|
title: "Übung 03 - Prozesse"
|
||||||
|
layout: "blog-post.njk"
|
||||||
|
tags: "sp1-lesson"
|
||||||
|
date: 2025-05-19
|
||||||
|
---
|
||||||
|
### waitpid()
|
||||||
|
Mit `waitpid()` können Informationen über den Exitstatus eines Kindprozesses abgefragt werden.
|
||||||
|
By default wartet waitpid, bis das angegebene Kind terminiert. Falls das nicht gewünscht ist, kann options=WNOHANG als Argument von waitpid() angegeben werden.
|
||||||
|
|
||||||
|
|
||||||
|
## Beispiel Programme aus der Präsenzübung
|
||||||
|
### `listRun.c`: Übergebenes Programm mehrfach mit verschiedenen Argumenten aufrufen
|
||||||
|
```c
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
if (argc < 3) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *command = argv[1];
|
||||||
|
|
||||||
|
for (int i = 2; i < argc; i++) {
|
||||||
|
pid_t pid = fork();
|
||||||
|
if (pid == -1) {
|
||||||
|
// Fehler
|
||||||
|
perror("fork");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
} else if (pid == 0) {
|
||||||
|
// Child
|
||||||
|
if (execlp(command, command, argv[i], NULL) == -1) {
|
||||||
|
perror("execlp");
|
||||||
|
}
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
} else {
|
||||||
|
// Parent (pid == child's pid)
|
||||||
|
int status;
|
||||||
|
waitpid(pid, &status, 0);
|
||||||
|
// ... waitpid Fehlerbehandlung
|
||||||
|
if (WIFEXITED(status)) {
|
||||||
|
fprintf(stderr, "Exitstatus: %d\n", WEXITSTATUS(status));
|
||||||
|
// ... fprintf Fehlerbehandlung
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Makefile für `listRun.c`
|
||||||
|
```Makefile
|
||||||
|
CC=gcc
|
||||||
|
CFLAGS=-std=c11 -pedantic -Wall -Werror -fsanitize=undefined -fno-sanitize-recover -g -D_XOPEN_SOURCE=700
|
||||||
|
|
||||||
|
all: listRun
|
||||||
|
|
||||||
|
listRun: listRun.o
|
||||||
|
$(CC) $(CFLAGS) -o listRun listRun.o
|
||||||
|
|
||||||
|
listRun.o: listRun.c
|
||||||
|
$(CC) $(CFLAGS) -o listRun.o -c listRun.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf listRun listRun.o
|
||||||
|
|
||||||
|
.PHONY: all clean
|
||||||
|
```
|
||||||
|
|
||||||
|
### strtok-test
|
||||||
|
```c
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
char str[] = "hello world"; // auf dem stack, damit schreibbar
|
||||||
|
char *token;
|
||||||
|
token = strtok(str, " ");
|
||||||
|
while (token != NULL) {
|
||||||
|
printf("%s\n", token);
|
||||||
|
token = strtok(NULL, " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
Loading…
Add table
Reference in a new issue