Skip to content
Snippets Groups Projects
Commit 40b77fe2 authored by Mactavish's avatar Mactavish
Browse files

update

parent 94f2c5ce
Branches
No related tags found
No related merge requests found
...@@ -4,4 +4,8 @@ This branch contains all materials for the 3rd tutorial session. ...@@ -4,4 +4,8 @@ This branch contains all materials for the 3rd tutorial session.
## Agenda ## Agenda
TBD - Assignment's solution presentation (if any)
- Recap & Discussion: Concurrency, Threads
- Pthreads API
- Make & Makefile
- Q&A
---
title: Discussion
---
# Discussion
Discuss (research if necessary) the following topics with your group members:
### Programs and Processes
- What are the differences between programs and processes?
- What information does a process possess?
- How does a process communicate with another process?
- How to create processes? What are the differences between different approaches?
<br/>
<v-click>
### Exercise
Implement your own [system() function](https://devdocs.io/c/program/system) using child process, so that you can have a naive version of shell.
You can find the source file in [exercises/process](https://git.imp.fu-berlin.de/mactavish96/alp4-tutorials/-/tree/tutorial-2/exercises/process)
</v-click>
---
title: More on Pointers I
---
# More on Pointers
## Multiple indirection
Consider the following code:
```c
int a = 3;
int *b = &a;
int **c = &b;
int ***d = &c;
```
Here are how the values of these pointers equate to each other:
```c
*d == c; Dereferencing an (int ***) once gets you an (int **)
**d == *c == b; Dereferencing an (int ***) twice, or an (int **) once, gets you an (int *)
***d == **c == *b == a == 3; Dereferencing an (int ***) thrice, or an (int **) twice, or an (int *) once
```
---
title: More on Pointers II
---
## Pointers and `const`
The `const` keyword is used a bit differently when pointers are involved. These two declarations are equivalent:
```c
// They are both a pointer to a const integer
// you can not change *ptr_a, but you can change ptr_a
const int *ptr_a;
int const *ptr_a;
```
These two, however, are not equivalent:
```c
// ptr_a is a pointer to a const integer
int const *ptr_a;
// ptr_b is a const pointer to a integer
// changing *ptr_b is valid, but changing ptr_b is invalid
int *const ptr_b;
```
---
title: More on Pointers III
---
## Function Pointers
It's possible to take the address of a function, too.
```c
// Pointer to strcpy-like function
char *(*strcpy_ptr)(char *dst, const char *src);
// Just like in a regular function declaration, the parameter names are optional
char *(*strcpy_ptr_noparams)(char *, const char *) = strcpy_ptr;
// You can also take the address of a function
strcpy_ptr = &strcpy;
strcpy_ptr = strcpy; // this also works
```
The type of the pointer to `strcpy` is `char *(*)(char *, const char *)`.
We can even have a function that returns a pointer to functions:
```c
char *(*get_strcpy_ptr(void))(char *dst, const char *src);
# for better readability, we need to use typedef
typedef char *(*strcpy_funcptr)(char *, const char *);
strcpy_funcptr strcpy_ptr = strcpy;
strcpy_funcptr get_strcpy_ptr(void);
```
---
title: More on Pointers IV
layout: two-cols
---
## How to Read C Declarations
**The golden rule**:
Start at the variable name (or innermost construct if no identifier
is present). Look right without jumping over a right parenthesis; say
what you see. Look left again without jumping over a parenthesis; say
what you see. Jump out a level of parentheses if any. Look right;
say what you see. Look left; say what you see. Continue in this
manner until you say the variable type or return type.
**examples:**
```c
int i; // an int
int *a[3]; // an array of size 3 pointers to int
int (*a)[3]; // an pointer of array of size 3 ints
int (*Object_vtable[])(); // an array of pointers to function returning int
```
::right::
### Exercise I
Discuss with your teammates and read these declarations out loud:
- `char ****q[ 30 ];`
- `char **(**q)[ 30 ];`
- `extern int (x)[];`
- `long (*a[])( char, char );`
- `int *(*(*(*b)())[10])();`
- `char *strprt( char (*)( int ), unsigned char );`
- `int (*const ab[])( unsigned int );`
### Exercise II
Implementing higher order functions in C, see [exercises/higher-order-func](https://git.imp.fu-berlin.de/mactavish96/alp4-tutorials/-/tree/tutorial-2/exercises/higher-oder-func)
...@@ -6,15 +6,9 @@ title: Q&A ...@@ -6,15 +6,9 @@ title: Q&A
Questions about: Questions about:
- First Assignment Sheet
- Second Assignment Sheet - Second Assignment Sheet
- Third Assignment Sheet
- Programming exercises - Programming exercises
- Makefile
- Organisation - Organisation
<br/> <br/>
### Materials
- [Reading C Declarations: A Guide for the Mystified](http://www.ericgiguere.com/articles/reading-c-declarations.html)
- [Higher Order Functions in C](https://foxypanda.me/higher-order-functions-in-c/)
---
title: Recap I
---
# Recap I
### Concurrency
- What is concurrency?
- What are the essentials?
- What are the models for concurrency programming?
<v-click>
<br />
### Concurrency with Threads
</v-click>
<v-clicks>
- What are threads?
- What information does a thread hold? What are shared among threads, what not?
- What kind of issues will we face when we use threads? What is the root of these issues?
- How to design your program using threads?
</v-clicks>
<br />
### Reading
See: [MIT 6.005 - Concurrency](http://web.mit.edu/6.005/www/fa16/classes/19-concurrency/)
---
title: Recap II
---
# Recap II
### Pthreads
TBD
---
title: Recap
---
# Recap
### Deterministic Algorithm vs Determined Algorithm
<br />
<div v-click>
<strong>Deterministic:</strong> With the same input, you always get the same output, and changes to the states of the system and their order are always the same
<strong>Determined:</strong> With the same input, you always get the same output. But there is no guarantee, that the changes to the states of the system are also the same.
</div>
<v-click>
### Determined Algorithm
</v-click>
<br />
<v-clicks>
- Can you give an example of determined algorithm?
- How can we guarantee the correctness of a determined algorithm?
</v-clicks>
...@@ -16,7 +16,7 @@ transition: fade-out ...@@ -16,7 +16,7 @@ transition: fade-out
css: unocss css: unocss
--- ---
# ALP4 Tutorial 2 # ALP4 Tutorial 3
## Chao Zhan ## Chao Zhan
...@@ -31,32 +31,12 @@ css: unocss ...@@ -31,32 +31,12 @@ css: unocss
--- ---
transition: slide-left transition: slide-left
src: ./pages/recap.md src: ./pages/recap-1.md
--- ---
--- ---
transition: slide-left transition: slide-left
src: ./pages/discussion.md src: ./pages/recap-2.md
---
---
transition: slide-left
src: ./pages/pointer-1.md
---
---
transition: slide-left
src: ./pages/pointer-2.md
---
---
transition: slide-left
src: ./pages/pointer-3.md
---
---
transition: slide-left
src: ./pages/pointer-4.md
--- ---
--- ---
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment