Compare commits

...

2 commits

2 changed files with 88 additions and 1 deletions

View file

@ -13,6 +13,7 @@ page_for: "sp1-lesson"
- `mkrepo` zum Aufgaben-Repo erstellen
- `deadline` zum Abgabetermine nachschauen
- Vorgegebene Compiler-Flags: `-std=c11` `-pedantic` `-D_XOPEN_SOURCE=700` `-Wall` `-Werror` `-fsanitize=undefined` `-fno-sanitize-recover` `-g`
- Debugger Valgrind kann so ausgeführt werden: `valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./--track-fds=yes ./lilo`
## Wichtige Links
- [Aufgaben](https://sys.cs.fau.de/lehre/ss25/sp1/uebung#aufgaben)
@ -40,4 +41,4 @@ page_for: "sp1-lesson"
- [Lukas (T03)](https://wwwcip.cs.fau.de/~uk16isyq/)
- [Julian (T08)](https://jzbor.de/tut/sp1/)
- [Luca (T11)](https://wwwcip.cs.fau.de/~am99ihig/sp)
- [Philip (ehemalig)](https://wwwcip.cs.fau.de/~oj14ozun/sp1/)
- [Philip (ehemalig)](https://wwwcip.cs.fau.de/~oj14ozun/sp1/) (Hier ist das sp-quiz!)

86
src/sp1/ub3.md Normal file
View 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, " ");
}
}
```