$ gcc ex1-sum-stdin.c
$ ./a.out
I have 1000
You have 200
I give you 50
Now You have 250. <---- ctrl-D pressed
1250 <---- only 1000, 200, 50 are summed
$ ./a.out
100 + 23 = 123
$100 - 23 = 77. <---- ctrl-D pressed
269 <---- 100, 23, 123, 23 are summed
$
$ ./a.out klim is not milk
argc is 5
argv[0]: ./a.out
argv[1]: klim
argv[2]: is
argv[3]: not
argv[4]: milk
environ[0]: CPLUS_INCLUDE_PATH=/usr/lib64/qt/include
environ[1]: NNTPSERVER=news.ncnu.edu.tw
.......
environ[33]: INFOPATH=/usr/info:/usr/share/texmf/info
environ[34]: G_BROKEN_FILENAMES=1
environ[35]: _=./a.out
environ[36]: OLDPWD=/home/klim/course/usp-1092
argc is resided at 0x7ffdddad11fc
argv is resided at 0x7ffdddad11f0
environ is resided at 0x601040
argv[] is resided at 0x7ffdddad12f8
environ[] is resided at 0x7ffdddad1328
value of argv[ 0] is 0x7ffdddad24c3
value of argv[ 1] is 0x7ffdddad24cb
value of argv[ 2] is 0x7ffdddad24d0
value of argv[ 3] is 0x7ffdddad24d3
value of argv[ 4] is 0x7ffdddad24d7
value of argv[ 5] is (nil)
value of env [ 0] is 0x7ffdddad24dc
value of env [ 1] is 0x7ffdddad2505
value of env [ 2] is 0x7ffdddad2521
value of env [ 3] is 0x7ffdddad2575
..........
value of env [33] is 0x7ffdddad2f86
value of env [34] is 0x7ffdddad2faf
value of env [35] is 0x7ffdddad2fc4
value of env [36] is 0x7ffdddad2fce
$
$ env -i A=1 hello=ohio ./a.out see you again
argc is 4
argv[0]: ./a.out
argv[1]: see
argv[2]: you
argv[3]: again
environ[0]: A=1
environ[1]: hello=ohio
argc is resided at 0x7fff4b9083dc
argv is resided at 0x7fff4b9083d0
environ is resided at 0x601040
argv[] is resided at 0x7fff4b9084d8
environ[] is resided at 0x7fff4b908500
value of argv[ 0] is 0x7fff4b908fcb
value of argv[ 1] is 0x7fff4b908fd3
value of argv[ 2] is 0x7fff4b908fd7
value of argv[ 3] is 0x7fff4b908fdb
value of argv[ 4] is (nil)
value of env [ 0] is 0x7fff4b908fe1
value of env [ 1] is 0x7fff4b908fe5
$
$ cat IN-3
this is ok.
10/31 2019::::10/31
dec/12 koala
$ ./a.out < IN-3
number of line is 3
number of token is 7
number of field is 11
$
$ echo $$
5009 # pid of the shell
$ ./a.out #
a, my pid = 6001, my ppid = 5009 # only one process
$ ./a.out 3 #
b, my pid = 6009, my ppid = 6008 # a
c, my pid = 6010, my ppid = 6008 # / | \ <----- 3
d, my pid = 6011, my ppid = 6008 # b c d
a, my pid = 6008, my ppid = 5009 #
$ ./a.out 1 1 # a
c, my pid = 6021, my ppid = 6020 # | <------ 1
b, my pid = 6020, my ppid = 6019 # b
a, my pid = 6019, my ppid = 5009 # | <------ 1
$ ./a.out 3 2 # c
b, my pid = 6037, my ppid = 6036 # a
c, my pid = 6038, my ppid = 6036 # / | \ <------ 3
e, my pid = 6040, my ppid = 6039 # b c d
f, my pid = 6041, my ppid = 6039 # / \ <------ 2
d, my pid = 6039, my ppid = 6036 # e f
a, my pid = 6036, my ppid = 5009 #
$ #
If the process is a leaf,
while(1) pause();
else (the process is an internal node)
while there are children alive do
waiting..
exit(...);
$ gcc -o wait ex5-wait.c # the executable is named as ``wait''.
$ gcc ex5-exec.c
$ ./a.out ddudduduuduu abcdef #
I'am a, mypid is 21790, and my ppid is 18796 # a
I'am f, mypid is 21793, and my ppid is 21790 # / | \
I'am c, mypid is 21792, and my ppid is 21790 # b c f
I'am b, mypid is 21791, and my ppid is 21790 # / \
I'am e, mypid is 21795, and my ppid is 21792 # d e
I'am d, mypid is 21794, and my ppid is 21792 #
child 21791 signaled with signal 1 # $ kill -1 21791 kill b
child 21794 signaled with signal 2 # $ kill -2 21794 kill d
child 21795 signaled with signal 3 # $ kill -3 21795 kill e
child 21792 exited with value 5 # c exit
child 21793 signaled with signal 2 # $ kill -2 21793 kill f
$ echo $? # a exit
8 #
$
#
# Since 'A' is a binary file, you can not just use `cat' to show its content.
# Check the argument of 'od'.
# File 'A' contains 13 integer. First is the count,
# others are values of the array.
#
$ od -A none --endian=big -t d4 A
12 9 101 3
4 78 119 4
3 119 78 78
9
$
#
# Run and check the result.
#
$ ./a.out A B
$
$ od -A none --endian=big -t d4 B
6 3 4 9
78 101 119
$
$ cat A | ./a.out - B
$
$ od -A none --endian=big -t d4 B
6 3 4 9
78 101 119
$
$ cat A | ./a.out - - | od -A none --endian=big -t d4
6 3 4 9
78 101 119
$
$ backup ex7-pre-post.c
$ ./a.out ddududduduuduu abcdefg
p .__a___.
I'm a, my pid=26986, and my ppid=26014 / / \ \
I'm b, my pid=26987, and my ppid=26986 b c d g
I'm c, my pid=26988, and my ppid=26986 / \
I'm d, my pid=26989, and my ppid=26986 e f
I'm e, my pid=26991, and my ppid=26989
I'm f, my pid=26992, and my ppid=26989
I'm g, my pid=26990, and my ppid=26986
P
I'm b, my pid=26987, and my ppid=26986
I'm c, my pid=26988, and my ppid=26986
I'm e, my pid=26991, and my ppid=26989
I'm f, my pid=26992, and my ppid=26989
I'm d, my pid=26989, and my ppid=26986
I'm g, my pid=26990, and my ppid=26986
I'm a, my pid=26986, and my ppid=26014
p
I'm a, my pid=26986, and my ppid=26014
I'm b, my pid=26987, and my ppid=26986
I'm c, my pid=26988, and my ppid=26986
I'm d, my pid=26989, and my ppid=26986
I'm e, my pid=26991, and my ppid=26989
I'm f, my pid=26992, and my ppid=26989
I'm g, my pid=26990, and my ppid=26986
q
$
loop
read from stdin
if got 'f' then
got a number, n
send 0 1 n to the clockwise next process
loop
read three numbers, a,b,n from clockwise previous process
if n is zero, then break
write b, a+b,n n-1 to the clockwise next process
end loop
write first number, a, out
if got 'p' then
write a token to the anti-clockwise next process
read a token from anti-clockwise previous process
show pid
if got 'q' then
write a token to the anti-clockwise next process
read a token from anti-clockwise previous process
exit
end loop
Each child does something like:
loop
monitor two inputs simultaneously
(one clockwise and another anti-clockwise)
if clockwise input is ready then
read three numbers, a,b,n, from clockwise previous process
show them
if n > 0 then write b (a+b) n-1 to clockwise next process
else write a b 0 to clockwise next process
if anti-clockwise input is ready then
read a token from anti-clockwise previous process
write a token to anti-clockwise previous process
if this token means 'p' then show pid
if this token means 'q' then exit
end loop
$ gcc ex8-poll-fib.c
$ ./a.out 6
f 10
I am 8815, I got 0 1 10
I am 8816, I got 1 1 9
I am 8817, I got 1 2 8
I am 8818, I got 2 3 7
I am 8819, I got 3 5 6
I am root, I got 5 8 5
I am 8815, I got 8 13 4
I am 8816, I got 13 21 3
I am 8817, I got 21 34 2
I am 8818, I got 34 55 1
I am 8819, I got 55 89 0
I am root, I got 55 89 0
I am root. Fib(10) = 55
f 2
I am 8815, I got 0 1 2
I am 8816, I got 1 1 1
I am 8817, I got 1 2 0
I am 8818, I got 1 2 0
I am 8819, I got 1 2 0
I am root, I got 1 2 0
I am root. Fib(2) = 1
p
I am 8819
I am 8818
I am 8817
I am 8816
I am 8815
I am 8814, the root
q
$
initialize signal handling
loop
read a number from stdin
send it to two children through the pipes
waiting signals generated by children
show the waited result
end loop
Each child does something like:
initialize random seed
loop
read a number, N, from the pipe
generate a random number, R
use R to determine the read number N if good or not
if good then send SIGNAL-1 to parent
send SIGNAL-2 to parent
end loop
$ ./a.out
190
child 0 does not approve 190
child 1 does not approve 190
190
child 0 does approve 190
child 1 does approve 190
190
child 0 does approve 190
child 1 does not approve 190
121
child 0 does not approve 121
child 1 does not approve 121
121
child 0 does not approve 121
child 1 does not approve 121
1223232323
child 0 does approve 1223232323
child 1 does not approve 1223232323
1223232323
child 0 does not approve 1223232323
child 1 does not approve 1223232323
$
$ ./a.out 6
I am 22756
I am 22757
I am 22758
I am 22759
I am 22760 another window
I am 22761
+----------------------
I am 22761, source:22760 | $ kill -USR2 22760
I am 22756, source:22760 |
I am 22757, source:22760 |
I am 22758, source:22760 |
I am 22759, source:22760 |
I am 22760, source:22760 |
| # two operations simultaneou
sly
I am 22759, source:22758 | $ kill -USR2 22760 22758
I am 22761, source:22760 |
I am 22756, source:22760 |
I am 22757, source:22760 |
I am 22758, source:22760 |
I am 22759, source:22760 |
I am 22760, source:22758 |
I am 22760, source:22760 |
I am 22761, source:22758 |
I am 22756, source:22758 |
I am 22757, source:22758 |
I am 22758, source:22758 |
| # generating a random fib
I am 22760, I got 0 1 9 from 22759 | $ kill -USR1 22759
I am 22761, I got 1 1 8 from 22759 |
...... |
I am 22757, I got 34 55 0 from 22759 |
I am 22758, I got 34 55 0 from 22759 |
I am 22759, Fib(9) = 34 |
I am 22760, I got 0 1 14 from 22759 | $ kill -USR1 22759
I am 22761, I got 1 1 13 from 22759 |
....... |
I am 22756, I got 377 610 0 from 22759 |
I am 22757, I got 377 610 0 from 22759 |
I am 22758, I got 377 610 0 from 22759 |
I am 22759, Fib(14) = 377 |
I am 22756, I got 0 1 11 from 22761 | $ kill -USR1 22759 22761
I am 22760, I got 0 1 20 from 22759 |
I am 22757, I got 1 1 10 from 22761 |
..... |
I am 22760, I got 55 89 1 from 22761 |
I am 22760, I got 144 233 8 from 22759 |
I am 22761, Fib(11) = 89 |
I am 22761, I got 233 377 7 from 22759 |
I am 22756, I got 377 610 6 from 22759 |
..... |
I am 22758, I got 6765 10946 0 from 22759|
I am 22759, Fib(20) = 6765 | $ kill -INT 22759
$
$ gcc ex11-thread-ball.c -lpthread
$ ./a.out
The ball's initial setting: 0 1 3 2 4 5
The permuations for balls:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
------------------------------------------------------
13 4 15 10 3 7 1 11 12 6 8 5 9 0 16 14 2 17
16 0 15 1 5 11 12 7 4 10 3 6 14 13 17 8 9 2
7 6 15 5 14 13 11 17 8 10 4 1 3 0 9 12 16 2
0 9 14 12 17 7 4 11 8 13 10 2 16 1 5 6 15 3
16 6 8 1 11 14 10 7 2 17 0 15 9 3 12 4 5 13
5 12 8 3 17 16 14 1 2 6 0 7 10 9 13 11 15 4
------------------------------------------------------
After permutations applied, balls are: 15 14 1 8 7 12
So the answer is: 16 15 2 9 8 13
$
$ gcc ex12-thread-mutex.c -lpthread
$ ./a.out
l
the balls are: 6 28 33 13 29 32
l
the balls are: 24 17 40 49 26 41
l
the balls are: 37 5 23 45 17 10
l
the balls are: 34 42 29 35 19 46
q
$