$ ./a.out
0
$ ./a.out 1 2 3
6
$ ./a.out 1 2 3 4
10
$ ./a.out 1 2 3 klim
6
$ ./a.out 1 2 3 klim 3 -1
8
$ ./a.out 1 2 3 klim 3 -1 9klim
8
$
$ ./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
$
$ gcc ex3-atexit.c
$
$ cat k
this is a test
Oh! It works.
1 + 2=3
$
$ wc k
3 10 43 k
$
$ ./a.out < k
this is a test
Oh! It works.
1 + 2=3
3 10 43
$
$ cat k-no-newline
this is a test
Oh! It works.
1 + 2=3
without the trailing newline$
$
$ wc k-no-newline
3 14 71 k-no-newline
$
$ ./a.out < k-no-newline
this is a test
Oh! It works.
1 + 2=3
without the trailing newline3 14 71
$
$ ./a.out < k-no-newline > output
3 14 71
$ cat output
this is a test
Oh! It works.
1 + 2=3
without the trailing newline$
$
$ ./a.out 1
I am external 12312. My parent is 12311.
I am external 12313. My parent is 12311.
I am internal 12311. My left and right are 12312 and 12313.
$ ./a.out 2
I am external 12320. My parent is 12319.
I am external 12321. My parent is 12319.
I am internal 12319. My left and right are 12320 and 12321.
I am external 12322. My parent is 12318.
I am internal 12318. My left and right are 12319 and 12322.
$ ./a.out 3
I am external 12325. My parent is 12324.
I am external 12327. My parent is 12326.
I am external 12328. My parent is 12326.
I am internal 12326. My left and right are 12327 and 12328.
I am internal 12324. My left and right are 12325 and 12326.
I am external 12329. My parent is 12323.
I am internal 12323. My left and right are 12324 and 12329.
$
print a message to show itself label, pid, ...
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, `cat' can not be used to show its content.
# Instead, 'od' is used. Check the argument of 'od' carefully.
# 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
$
$ gcc 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
read from stdin 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-1 to the clockwise next process
end loop
write to the stdout the first number, a
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 next 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
$
while get a line do
if first char is 'q' then
break
endif
if first char is 'l' then
send a signal to child
read an integer from child (through the pipe)
print the integer
endif
done
send a signal to terminate the child
setup signal handler
initialize random generator
while true; do
wait a signal from parent
generate a number in the range 1..49
write the number to parent (through the pipe)
done
$ ./a.out
hello
do it
lot of fun
got 33
lottery
got 26
l
got 29
quit
$
$ gcc ex10-pselect.c
$ ./a.out 4
I am 28539
I am 28540
I am 28541 another window
I am 28542 +----------------------
I am 28541, I got 0 1 5 from 28540 | $ kill -USR1 28540
I am 28542, I got 1 1 4 from 28540 |
I am 28539, I got 1 2 3 from 28540 |
I am 28540, I got 2 3 2 from 28540 |
I am 28541, I got 3 5 1 from 28540 |
I am 28542, I got 5 8 0 from 28540 |
I am 28539, I got 5 8 0 from 28540 |
I am 28540, Fib(5) = 5 |
I am 28542, I got 0 1 15 from 28541 | $ kill -USR1 28541
I am 28539, I got 1 1 14 from 28541 |
. . . .
I am 28541, I got 89 144 4 from 28541 |
I am 28542, I got 144 233 3 from 28541 |
I am 28539, I got 233 377 2 from 28541 |
I am 28540, I got 377 610 1 from 28541 |
I am 28541, Fib(15) = 610 |
I am 28542, I got 0 1 15 from 28541 | $ kill -USR1 28541 28539
I am 28540, I got 0 1 1 from 28539 |
I am 28539, I got 1 1 14 from 28541 |
I am 28540, I got 1 2 13 from 28541 |
I am 28541, I got 1 1 0 from 28539 |
I am 28541, I got 2 3 12 from 28541 |
I am 28542, I got 1 1 0 from 28539 |
I am 28542, I got 3 5 11 from 28541 |
I am 28539, Fib(1) = 1 |
I am 28539, I got 5 8 10 from 28541 |
I am 28540, I got 8 13 9 from 28541 |
I am 28541, I got 13 21 8 from 28541 |
I am 28542, I got 21 34 7 from 28541 |
I am 28539, I got 34 55 6 from 28541 |
I am 28540, I got 55 89 5 from 28541 |
I am 28541, I got 89 144 4 from 28541 |
I am 28542, I got 144 233 3 from 28541 |
I am 28539, I got 233 377 2 from 28541 |
I am 28540, I got 377 610 1 from 28541 |
I am 28541, Fib(15) = 610 |
$ | $ $ kill -INT 28541
$ 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
$