$ ls
Makefile main.c mylib.c mylib.h
$ make
gcc -c -o main.o main.c
gcc -c -o mylib.o mylib.c
gcc -o sum main.o mylib.o
$ touch mylib.h
$ make
gcc -c -o main.o main.c
gcc -c -o mylib.o mylib.c
gcc -o sum main.o mylib.o
$ touch mylib.c
$ make
gcc -c -o mylib.o mylib.c
gcc -o sum main.o mylib.o
$ touch main.c
$ make
gcc -c -o main.o main.c
gcc -o sum main.o mylib.o
$ rm sum
$ make
gcc -o sum main.o mylib.o
$
$ ./sum 1 2 3
The sum of arguments is 6
$ ./sum 1 2 3 "1 2 3"
The sum of arguments is 12
$ ./sum 1 2 3 "1 2 3" "1000 1"
The sum of arguments is 1013
$
$ env -i klim=milk milk=coffee ./usp-ex2 one apple a day
argc is 5
argv[0]: ./usp-ex2
argv[1]: one
argv[2]: apple
argv[3]: a
argv[4]: day
environ[0]: klim=milk
environ[1]: milk=coffee
argc is at 0x7fff9ead9dfc
argv is at 0x7fff9ead9df0
environ is at 0x600d98
argv[] is at 0x7fff9ead9ef8
environ[] is at 0x7fff9ead9f28
value of argv[ 0] is 0x7fff9eadafbe
value of argv[ 1] is 0x7fff9eadafc8
value of argv[ 2] is 0x7fff9eadafcc
value of argv[ 3] is 0x7fff9eadafd2
value of argv[ 4] is 0x7fff9eadafd4
value of env [ 0] is 0x7fff9eadafd8
value of env [ 1] is 0x7fff9eadafe2
$
$ ./a.out 3
I am internal 31512. My left and right are 31513 and 31514.
I am external 31514. My parent is 31512.
I am internal 31513. My left and right are 31515 and 31516.
I am external 31516. My parent is 31513.
I am internal 31515. My left and right are 31517 and 31518.
I am external 31518. My parent is 31515.
I am external 31517. My parent is 31515.
^C
$
while(1) pause();
$ ./a.out 3
I am internal 2368. My left and right are 2369 and 2370.
I am external 2370. My parent is 2368.
I am internal 2369. My left and right are 2371 and 2372.
I am external 2372. My parent is 2369.
I am internal 2371. My left and right are 2373 and 2374.
I am external 2374. My parent is 2371.
I am external 2373. My parent is 2371.
child 2373 signaled with signal 2 < ---- kill -2 2373
child 2374 signaled with signal 3 < ---- kill -3 2374
child 2371 exited with value 5
child 2372 signaled with signal 1 < ---- kill -1 2372
child 2369 exited with value 6
child 2370 signaled with signal 5 < ---- kill -5 2370
$ echo $?
11
$
$ ./myrev file1 file2 file3 .. # reading from files, files1..
$ ./myrev # reading from stdin.
$ ./a.out ddduduuduu abcde
I'm a, my pid=27828, and my ppid=27682
I'm b, my pid=27829, and my ppid=27828
I'm c, my pid=27830, and my ppid=27829
I'm d, my pid=27832, and my ppid=27829
I'm e, my pid=27831, and my ppid=27828
I'm c, my pid=27830, and my ppid=27829
I'm d, my pid=27832, and my ppid=27829
I'm b, my pid=27829, and my ppid=27828
I'm e, my pid=27831, and my ppid=27828
I'm a, my pid=27828, and my ppid=27682
$
loop
read from stdin
if got 'l' then
generate 12 random numbers between 1 to 49
send them to the clockwise next process
read 12 numbers from clockwise previous process
write the first six out
if got 'p' then
write a token to the anti-clockwise next process
read a token from anti-clockwise previous process
end loop
Each child does
loop
monitor two inputs simultaneously
(one clockwise and another anti-clockwise)
if clockwise input is ready then
read 12 numbers from clockwise previous process
show them
randomly generate a, b
swap a-th and b-th numbers
write 12 numbers to clockwise next process
if anti-clockwise input is ready then
read a token from anti-clockwise previous process
show the a, b (previously generated random numbers)
write a token to the anti-clockwise next process
end loop
$ ./a.out 6 <----- one root, five children
l
40 9 18 41 3 28 39 36 15 2 33 43
40 9 33 41 3 28 39 36 15 2 18 43
40 9 33 41 39 28 3 36 15 2 18 43
40 9 33 41 28 39 3 36 15 2 18 43
40 9 33 3 28 39 41 36 15 2 18 43
The six balls: 40 9 33 3 18 39
l
35 14 21 7 30 27 24 38 31 26 40 2
35 14 21 7 30 27 2 38 31 26 40 24
35 14 21 38 30 27 2 7 31 26 40 24
38 14 21 35 30 27 2 7 31 26 40 24
38 2 21 35 30 27 14 7 31 26 40 24
The six balls: 14 2 21 35 30 27
p
It is time to list the random numbers.
I'am 26069 and my random numbers are 6 and 0
I'am 26068 and my random numbers are 6 and 1
I'am 26067 and my random numbers are 0 and 3
I'am 26066 and my random numbers are 3 and 7
I'am 26065 and my random numbers are 6 and 11
q
$
$ ./a.out ddduduudduuu abdecf
I'm a, my pid=26895, and my ppid=25582
I'm c, my pid=26897, and my ppid=26895
I'm b, my pid=26896, and my ppid=26895
I'm f, my pid=26898, and my ppid=26897
I'm e, my pid=26900, and my ppid=26896
I'm d, my pid=26899, and my ppid=26896
I'm a, my pid=26895, and my ppid=25582 <---- kill -1 26895
I'm b, my pid=26896, and my ppid=26895
I'm d, my pid=26899, and my ppid=26896
I'm e, my pid=26900, and my ppid=26896
I'm c, my pid=26897, and my ppid=26895
I'm f, my pid=26898, and my ppid=26897
I'm b, my pid=26896, and my ppid=26895 <---- kill -1 26896
I'm d, my pid=26899, and my ppid=26896
I'm e, my pid=26900, and my ppid=26896
I'm c, my pid=26897, and my ppid=26895 <---- kill -1 26897
I'm f, my pid=26898, and my ppid=26897
I'm c, my pid=26897, and my ppid=26895 <---- kill -3 26897; kill -1 26897
I'm b, my pid=26896, and my ppid=26895 <---- kill -3 26896; kill -1 26896
I'm a, my pid=26895, and my ppid=25582 <---- kill -1 26895
I'm b, my pid=26896, and my ppid=26895
I'm c, my pid=26897, and my ppid=26895
I'm a, my pid=26895, and my ppid=25582 <---- kill -3 26895; kill -1 26895
Killed <---- kill -9 26895
$
$ ./a.out infile outfile <----- copy infile to outfile
hello i'm klim with async read and async write
hello i'm klim <---- read stdin and echo it to stdout ...
...
...
[Copy completed] <------ when the copy is completed, show something
.. to screen
..
^D
$
1st shell:
$ ./periodicchars 12 < FIFO
....... <---- the contents of infile is shown here
in the rate of 12 chars per second
2nd shell:
$ ./a.out infile FIFO <----- the contents of infile is sent
hello klim to FIFO
hello klim
.......
$ gcc ex-12.c -lpthread
$ ./a.out
The balls are 29 42 35 37 5 14
$ ./a.out
The balls are 12 10 15 22 9 34
$ ./a.out
The balls are 6 20 28 35 2 11
$ ./a.out
The balls are 39 4 22 41 17 14
$ ./a.out
The balls are 39 1 20 26 42 13
$ ./a.out
The balls are 11 42 28 2 26 9
$