Открыть Электронные книги
Категории
Открыть Аудиокниги
Категории
Открыть Журналы
Категории
Открыть Документы
Категории
..............................................................................................................................................3
................................................................4
1 .............6
1.1 ....................................................................................................................6
1.2 ................................................................................................................... 7
1.3 ................................................................................................................... 8
1-30 1...10
2 ...... 12
2.1 ..................................................................................................................12
2.2 ..................................................................................................................12
2.3 ..................................................................................................................13
2.4 ..................................................................................................................13
2.5 ..................................................................................................................13
2.6 ..................................................................................................................14
2.7 ..................................................................................................................14
2.8 ..................................................................................................................15
2.9 ..................................................................................................................15
1-30 2...17
3
.................................................................................................................... 19
3.1 ..................................................................................................................19
1-3 3..20
3.2 ..................................................................................................................20
3.3 ..................................................................................................................20
12-14 3.20
3.4 ..................................................................................................................21
3.5 ..................................................................................................................21
15-20 3.23
3.6 ..................................................................................................................23
25-30 3....25
4-11 326
4 ,
................... 27
4.1. ................................................................................................................27
1-30 4....29
1. QNX Momentics
............................................................................................................................................ 30
2. 1
( ) .................................................................................................... 39
3. 2
( ) ............................................................................................................................. 54
4. 3
( /) .......................................................................................... 82
5. 4
( ) .................................................................................................................................. 126
........................................................................................... 134
.. 2008. . .
()
(). . :
- ( ) /
,
.
, ;
- ;
- ,
.
,
( ).
QNX Momentics
QSS (http://www.qnx.com). :
- , Neutrino
, ;
- API POSIX, , POSIX- .
, , QNX Momentics.
QNX Momentics.
QNX, (Vmware, Virtual PC ..), .. .
Photon.
IDE Momentics. , (10).
,
, .
,
.
, C QNX Neutrino
( _r), . , .
,
.
Safety .
(. errno,perror(), strerror(), assert()).
C
-. . ,
, :
MS Windows
MS Word;
Word-
Unicode UTF8 (,
pedit Photon QNX);
QNX .
.
. 9 2.1 ( 9: .5-2; .6-2; .7-2; .8-1; .9-3)
2.1
. 5
. 6
. 7
. 8
. 9
fork()
1.1
( -> -> )
parent1
( - localtime(),
strftime()), pid, ,
, .
(.1).
parent2 .
pid. () ,
main(), 2 . -
2. daughter 1
daughter 2 1 2 (, 0 -). N 0.
1 N 1, 2 1.
(. 3 4).
30 sleep().
- , N, Photon pidin sin . ,
? pidin .
, N , .
, ,
.
. N. N
? .
1.2
( -> -> ->-)
parent1
( - localtime(),
strftime()), pid,
parent2, ,
.
(.1). pid.
daughter .
2 - grandchild1 grandchild2
(. 2) 50
. - 1 (1 ) 1 (2
)
N 0.
(. 3 4).
, N, .
.
, - N, Photon pidin sin
. ,
? pidin . .
N. N
? .
1.3
( -> ->
-> )
parent1
( - localtime(),
strftime()), pid, (. 2). (. 1) ,
, . parent2 , pid,
daughter1 daughter2
(. 2), 45 . N 0: N 1, .
(. 3 4). , , , N,
. ,
, .
, -
N,
Photon pidin . ,
?
pidin
. .
N. N
? .
1
1.
spawnl()
2.
spawnlp()
3.
spawnv()
4.
spawnvp()
/
/
/
/
1.
2.
3.
4.
5.
6.
10
10
10
9
10
9
10
10
9
10
10
10
RR
RR
FIFO
FIFO
FIFO
FIFO
RR
FIFO
RR
RR
FIFO
FIFO
3
( sched_yield())
1.
2.
3.
/ 1
/ 2
N
N 10000
1.
2.
3.
/ 1
/ 2
N 11000 ( RR->FIFO
FIFO->RR): N>0 1, N<0 2 .
.
N 11000 : N>0 1 +1, N<0
2 -1.
.
10
1-30
1
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
1-10
1.1
. 1
. 2
. 3
. 4
1
3
2
4
3
4
1
2
3
4
6
5
4
3
2
1
3
5
2
4
1
2
3
3
3
2
1
1
2
3
3
3
1
2
1
3
3
3
3
1
11-20
1.2
. 1
. 2
. 3
. 4
4
4
3
3
1
1
2
2
2
4
3
6
1
4
2
5
1
2
3
4
3
2
3
1
3
2
1
1
3
3
1
3
3
3
1
3
3
3
3
2
11
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
21-30
1.3
. 1
. 2
. 3
. 4
4
1
2
3
2
1
3
4
1
2
1
2
6
5
4
3
2
6
4
1
3
3
2
1
3
1
1
3
3
2
3
2
3
3
1
3
3
1
3
3
12
2.1
( )
(.5) --. (.6)
, . . , ,
(.7).
, (.8). :
parent,
daugter grandchild. .
. , , RR. 10 .
RR ,
. (.9), , .
. .
2.2
( )
- main() N 64- uint64_t , 0.
30 ,
13
2.3
( )
2.2, supervisor
incrementor, decrementor.
2.4
( )
2.2 , . 6. N ,
.
2.5
( )
2.3 , . 6. N ,
.
14
2.6
( -)
- - ,
.
delay(T), T , rand_r()
T = rand_r() % D,
2000 -,
D =
6000 -.
D 3 . -
60 , 1 2.
60 3.
- .
- RR
(.9) . .
- (read-write lock). 65 , .
2.7
( -)
2.4, .
15
2.8
( -, )
1 () 1000
. 3 -. ,
.
1000 ,
.9. , .
. , ,
,
. . 9. ispunct(), isdigit(), isalpha().
2.9
( -, )
2.6, . ,
.
5
1.
2.
16
1.
2.
3.
pthread_create()
fork()
spawnv()
7
1.
2.
1.
2.
3.
4.
1.
2.
3.
4.
5.
6.
(condition variable)
/ (read-write lock)
(sleepon lock)
17
1-30
2
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
1-10
2.1
. 5
. 6
11-16
2.3
- 2.2
1
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
. 7
. 8
. 9
1
2
4
1
2
1
3
4
1
3
1
2
3
2
1
1
2
3
3
1
2.4
2.5
. 6
1
17-22
2.6
2
3
-
3
2
2.7
. 9
2
3
1
1
2
-
18
22.
3
23-30
23.
24.
25.
26.
27.
28.
29.
30.
2.8
2.9
. 9
-
-
1
5
3
4
1
4
2
4
6
5
3
5
2
5
6
4
19
3.1
( POSIX, )
-
, a, o, t.
, - N=20. ,
t ,
. t 200 , delay().
-
,
.
-
, t
t = t*(N/N),
( ), , ( ).
. POSIX, .
:
1)
, - , ,
; 2) ; 3)
(. 9)
( ).
20
3.2
( POSIX)
3.1, .
1-3
3
1.
2.
3.
3.1
3.2
. 9
1
2
-
3.3
(, QNX Neutrino)
(Eratosthene, Eratosthenes, Erasmus, 276-194 . ..) . .
2 .
. ,
. . 0, , , .
, ,
.
21
, ,
( 4-11) 10.
3.4
( QNX Neutrino. 3.1)
. 5 t p. 4 . (.11) -.
, - .
11
12-14
3
12.
13.
14.
QNX Neutrino
QNX Neutrino
SIGRTMAX
3.5
(, QNX Neutrino)
mat1
mat2 m , n n , p (m 9, n 9, p 9)
mat
{ = mat
{1 * mat
1
2
32
m, p
m ,n
n, p
22
n 1
(*)
Sk
1.
QNX Neutrino / ,
.
/ 2.
SIGRTMIN
.
,
3.
. QNX Neutrino (., ,
name_attach())
13
mat main
1.
QNX Neutrino,
main ( MsgDeliverEvent())
23
2.
QNX Neutrino,
main ,
-
15-20
3
15.
16.
17.
18.
19.
20.
15 - 20
3.5
12
1
1
2
2
3
3
13
1
2
1
2
1
2
3.6
(, QNX Neutrino)
3.5 ( ) . -
POSIX, -.
-, , , -, .
i mat1 j mat2 . mat .12. , -, .14.
mat
.13.
24
14
1.
2.
name_attach() name_open()
21-30
3
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
21 - 30
3.6
12
2
3
1
2
13
2
1
2
1
1
2
2
2
1
2
14
1
2
2
2
1
2
1
2
1
2
.10
4-11 3
( 3.3 - )
4 -11
5.
6.
7.
MsgSend()
, value
MsgSendPulse()
SIGUSR1
,
value
pthread_kill()
SignalKill()
SIGSPECIALMAX
,
value
MsgReceive()
sigwaitinfo()
- , (),
(. MsgDeliverEvent). (chid)
c .
-,
.
- -
.
(tid), . -
tid=1,
tid=2, 3, .
tid
25
4.
9.
11.
, value
MsgSend()
MsgSendPulse()
SIGUSR1
,
value
SignalKill()
SIGSPECIALMAX
,
value
MsgReceive()
26
10.
8.
tid+1 .
-
spawn*()) ,
(), (. MsgDeliverEvent).
(chid)
. -,
.
sigwaitinfo()
27
4
,
4.1.
( )
1.1, 1.2,
1.3 :
1. , (
-),
, N
13.57 , . .
.15. ()
( joinable). - .
2.
()
(. 16),
SIGEV_UNBLOCK.
28
, ,
. , . ,
time.
1-30
4
15
, -
1.*
1, 2, 3
-
SIGUSR2
timer_create()
4, 5, 6
timer_settime() -3
16, 17, 18,
-
19, 29, 30
-
SIGALRM
20, 21, 22
-5
7, 8, 9
TimerAlarm()
SIGALRM 13, 14, 15
setitimer()
23, 24, 25
delay()
-
29
1.1
1.2
1.3
16
,
pthread_join()
.
1) 20 ( ,
), 2) 10 - .
.
1) 18.05 ( ,
), 2) 6.33 - .
.
1) 15.78 (
, ), 2) 12.95 -
.
30
1.
QNX Momentics
UNIX- ,
, #
root $ .
. ,
myprog.out ,
$./myprog.out.
,
SIGINT (Ctrl-C) SIGQUIT
(Ctrl-\) - , .
,
$slay _ ( SIGTERM).
.
31
32
,
, , ..),
Memory fault (core dumped).
/var/dumps .core.
-
, ..
( 2) gdb ,
.
gcc (qcc gcc), ,
, .
core_dump_case.c .
QNX Momentics 6.3.2 SP3
,
,
( . ).
// core_dump_case.c
#include <stdlib.h>
#include <stdio.h>
/* //***
# gcc -ggdb core_dump_case.c -ocore_dump_case.out
# ./core_dump_case.out
//
Memory fault (core dumped)
//
# gdb core_dump_case.out /var/dumps/core_dump_case.out.core
GNU gdb 5.2.1qnx-nto
Copyright 2002 Free Software Foundation, Inc.
33
*/
/* //***
# gcc -ggdb core_dump_case.c -ocore_dump_case.out
# ./core_dump_case.out
12 11 10 9 8 7 6 5 4 3 2 1
//
Memory fault (core dumped)
//
# gdb core_dump_case.out /var/dumps/core_dump_case.out.core
GNU gdb 5.2.1qnx-nto
Copyright 2002 Free Software Foundation, Inc.
34
This GDB was configured as "ntox86"...
Program terminated with signal 11, Segmentation fault. //
- ( SIGSEGV =
11)
Reading symbols from /usr/qnx630/target/qnx6/x86/lib/libc.so.2...done.
Loaded symbols for /usr/qnx630/target/qnx6/x86/lib/libc.so.2
#0 0x0804849e in main () at core_dump_case.c:8
8
for (p = foo, i = 12; i > 0; p++, i--) *p = i; /*
, - */
(gdb)quit //
#
35
This GDB was configured as "ntox86"...
Program terminated with signal 11, Segmentation fault. //
- ( SIGSEGV =
11)
Reading symbols from /usr/qnx630/target/qnx6/x86/lib/libc.so.2...done.
Loaded symbols for /usr/qnx630/target/qnx6/x86/lib/libc.so.2
#0 0xb0320ce7 in __flist_dequeue_bin () from // - libc.so.2,
/usr/qnx630/target/qnx6/x86/lib/libc.so.2
//
(gdb) bt /* bt ,
, */
#0 0xb0320ce7 in __flist_dequeue_bin () from
/usr/qnx630/target/qnx6/x86/lib/libc.so.2
#1 0xb0320791 in _list_release () from
/usr/qnx630/target/qnx6/x86/lib/libc.so.2
#2 0xb0320f41 in __prelocked_free () from
/usr/qnx630/target/qnx6/x86/lib/libc.so.2
#3 0xb0320ff0 in __free () from /usr/qnx630/target/qnx6/x86/lib/libc.so.2
#4 0xb031ea4e in free () from /usr/qnx630/target/qnx6/x86/lib/libc.so.2
/* , free() main() ( 11 ) */
#5 0x08048578 in main () at core_dump_case.c:11
(gdb)quit //
# */
1.1
QNX
cd
chkfsys
clear
find
grep
. cd
DOS/Windows. , UNIX / : cd /home/mydir mydir, /home
qnx4 c .
. #chkfsys m /dev/hd0t79; #chkfsys rs /
. : #df -P. qnx4 /dev/hd0t79 ( hd0t77 hd0t78)
. grep
.
. (*.html) ,
( Examples)
/home/mydir/examples.txt:
#cd /usr/help/product/neutrino_2.11_en/lib_ref
#find . name *.html | xargs grep s F Examples > /home/mydir/examples.txt
.
. examples.txt
( sem, )
sem_examp.txt:
#grep s F sem /home/mydir/examples.txt > /home/mydir/sem_examp.txt
36
df
ls
more
mount
umount
passwd
pidin
37
on
. dir DOS/Windows.
: ls /home/mydir
. DOS/Windows.
: #pidin | more
, /.
. FAT,
/fs/floppy: #mount t dos /dev/fd0 /fs/floppy.
QNX 6.3.0 USB- .
. io-usb.
.
: umount /fs/floppy
/
.
. myprog.out, /home/mydir, 15 FIFO: #on p15f /home/mydir/myprog.out
.
. #passwd _ ;
#passwd __ ( root)
.
: #pidin ;
#pidin p 1716269 pid = 1716269 ;
#pidin | grep myprog myprog ;
#pidin arg | grep devc-ser8250 , devc-ser8250
pwd
qcc,
QCC
sin
su
38
gcc
, .
QNX- - .
. C,
myprog.c , myprog.out:
#qcc myprog.c o myprog.out;
, C++ : QCC myprog.cpp o myprog.out.
, (, sin(x)):
#qcc myprog.c omyprog.out l /lib/libm.a. l libm.a.
GNU-. , QNX , , qcc, .
. , (,
sin(x)): #gcc myprog.c omyprog.out -Ldir /lib/libm.a.
.
: #sin;
#sin P myprog th myprog;
#sin P io-graphics args , io-graphics
.
: $su root .
2.
1
( )
2.1
C QNX Neutrino
spawnl()
spawnlp()
spawnvp()
exit()
system()
getpid()
getppid()
wait()
sched_getparam()
sched_setparam()
. . spawn*()
,
. . - wait*()
39
spawnv()
sched_getscheduler()
sched_setscheduler()
40
. (main()) getprio()
1 ,
. (main()) 1 , .
setprio()
getprio() setprio() - .
sched_yield()
READY
pthread_create()
()
pthread_attr_init()
/
pthread_attr_setdetachstate()
/ pthread_attr_setinheritsched()
pthread_attr_setschedpolicy() . PTHREAD_EXPLICIT_SCHED
pthread_attr_setinheritsched()
. pthread_attr_setschedparam() PTHREAD_EXPLICIT_SCHED
pthread_attr_setinheritsched()
pthread_getschedparam()
pthread_setschedparam()
pthread_exit()
pthread_setcancelstate()
pthread_setcanceltype()
pthread_testcancel()
pthread_cancel()
pthread_setname_np()
pthread_getname_np()
/
(/ )
QNX Neutrino Core OS 6.3.2.
open()
close()
dup()
dup2()
setbuf()
time()
/ /
,
. NULL,
( ,
UNIX 1 1970 .
00 00 00 -
41
C QNX Neutrino,
2.2
strftime()
localtime()
asctime()
sleep()
mmap()
UTC (.. ) )
. , .
42
43
. 13
RR, 40 . N
(. ) .
, pid
, - 9 FIFO.
- : 100 101,
. - - N 0, :
- N<= 0, 1 N 1 , N<15001. N=15000 2;
- N>0, 2 N 1 , N> -15001. N= -15000 1.
-, ,
- .
:
- , 14;
- . pid ;
- 19 RR;
- 35 .
- .
, 2.0
1 sin. 7
.
zad1_primer-prim_foutp_cancel.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sched.h>
#include <process.h>
#include <errno.h>
#include<fcntl.h>
#define STDOUT 1
int N = 0, stop=1;
{
int *number;
pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, NULL);/*
:
*/
pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
//
number = (int*) arg;
printf("grandchild1 of primary process starts with tid %d number %d\n", pthread_self(),
*number);
/* N. stop
while(stop)
*/
{
if (N<=0) while (N<15001) ++N;
//printf("1: %d\n", N);
44
sched_yield(); //
}
}
//-----------------------------------------------------------------------------------------------void* function_daughter()
//
{
pthread_attr_t attr;
struct sched_param param;
int grndchld1=100, grndchld2=101; //
void* arg;
45
int second_pid;
char buffer[15];
pthread_t gch1, gch2;
. Photon
. , - . ,
Photon shell 10, , FIFO (11) -. */
pthread_attr_setschedparam(&attr, ¶m);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO); // FIFO
arg = (void*) &grndchld1;
pthread_create( &gch1, &attr, &function_grandchild1, arg );
arg = (void*) &grndchld2;
46
//------------------------------------------------------------------------------------------------
/* output-prim.txt,
STDOUT. printf()
output-prim.txt. */
fptr = open("output-prim.txt",O_CREAT|O_RDWR,S_IREAD|S_IWRITE);
oldstdout = dup(STDOUT);
dup2(fptr,STDOUT);
close(fptr);
setbuf(stdout, NULL); //
time_of_day = time( NULL );
strftime( buffer, 80, " %A %B %d, %Y %H:%M:%S", localtime( &time_of_day ) );
// buffer ,
47
zad1_primer-sec.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
//--------------------------------------------------------------------void* function( void* arg ) //
{
int policy;
struct sched_param param;
time_t time_of_day;
char buffer[ 80 ];
, sin, system() */
sleep(2);
}
return( 0 );
}
49
printf( "
This is thread %d of secondary process with pid %d\n", pthread_self(), getpid());
printf("\n
Output of sin utility:\n");
pthread_getschedparam(pthread_self(), &policy, ¶m);
while(param.sched_priority >=7)
{
param.sched_priority --; // 1
pthread_setschedparam(pthread_self(), policy, ¶m);
time_of_day = time( NULL );
printf("
Local time %s\n", asctime(localtime(&time_of_day)));
system("sin -P zad1_primer-sec th"); /* ,
Round-Robin */
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
param.sched_priority = 19;
pthread_attr_setschedparam(&attr, ¶m);
pthread_attr_setschedpolicy(&attr, SCHED_RR);
pthread_create( NULL, &attr, &function, NULL );
sleep( 35 ); // 35
time_of_day = time( NULL );
ctime_r( &time_of_day, buf );
printf("
The secondary process with pid %d finished at %s\n", getpid(), buf);
return EXIT_SUCCESS;
}
50
setprio(getpid(), 14); // 14
setbuf(stdout, NULL); //
time_of_day = time( NULL );
ctime_r( &time_of_day, buf ); // buf
printf("
Secondary process with pid %d starts at %s My parent's pid=%d\n", getpid(), buf,
atoi(argv[1]));
/* 19
pthread_attr_init( &attr );
output_prim.txt
Primary process with pid=540708 starts at Thursday September 28, 2006 13:52:45
This is thread 2 of primary process
Seco
Secondary process with pid 544805 starts at Thu Sep 28 13:52:45 2006
My parent's pid=540708
This is thread 2 of secondary process with pid 544805
Output of sin utility:
Local time Thu Sep 28 13:52:45 2006
ndary process with pid 544805 launched
648K
544805
B0328E11
1 B0327A5D
4K
36K
5
1
12K
648K
4K
36K
12K
648K
544805
51
. . - . .
1 14r
2 16r
NANOSLEEP
REPLY
B0328E11
1 B0327A5D
5
1
544805
B0328E11
1 B0327A5D
4K
36K
5
1
12K
648K
4K
36K
5
1
12K
648K
4K
36K
5
1
12K
648K
4K
36K
5
1
12K
648K
4K
36K
12K
648K
52
zad1_primer-sec.out
1 14r
NANOSLEEP
2 14r
REPLY
544805
B0328E11
1 B0327A5D
544805
B0328E11
1 B0327A5D
544805
1 14r
2 11r
NANOSLEEP
REPLY
B0328E11
1 B0327A5D
5
2
544805
B0328E11
1 B0327A5D
4K
36K
5
2
12K
648K
4K
36K
5
3
12K
648K
7, , FIFO 9 - .
The secondary process with pid 544805 finished at Thu Sep 28 13:53:20 2006
Daughter of the primary process: secondary process with pid 544805 stopped
The primary process comes to the end at
Thursday September 28, 2006 13:53:25. N=5486
N -. N
,
pthread_cancel(gch1); pthread_cancel(gch2);
stop = 0;
53
zad1_primer-sec.out
1 14r
NANOSLEEP
2 9r
REPLY
3.
2
( )
C QNX Neutrino
3.1
pthread_mutexattr_init()
pthread_mutexattr_setpshared()
PTHREAD_MUTEX_INITIALIZER
pthread_mutex_lock()
pthread_mutex_unlock()
pthread_mutex_destroy()
sem_init()
sem_destroy()
sem_open()
sem_close()
sem_unlink()
sem_post()
/
,
(. )
/
1 ( )
54
pthread_mutex_init()
1 , 0
pthread_condattr_init()
/
pthread_condattr_setpshared()
,
pthread_cond_init()
PTHREAD_COND_INITIALIZER
pthread_cond_wait()
(. )
pthread_cond_signal()
,
pthread_cond_broadcast()
,
pthread_cond_destroy()
- pthread_rwlockattr_init()
/
pthread_rwlockattr_setpshared()
-
- pthread_rwlock_init()
,
pthread_rwlock_destroy()
-
pthread_rwlock_wrlock()
-
pthread_rwlock_rdlock()
-
pthread_rwlock_unlock()
-
sem_wait()
55
pthread_barrierattr_setpshared() /
, pthread_barrier_init()
pthread_barrier_destroy()
pthread_barrier_wait()
pthread_sleepon_lock()
,
pthread_sleepon_wait()
pthread_sleepon_signal()
,
pthread_sleepon_broadcast()
,
pthread_sleepon_unlock()
pthread_barrierattr_init()
56
#include
#include
#include
#include
#include
#include
<stdio.h>
<string.h>
<fcntl.h>
<sys/mman.h>
<semaphore.h>
<errno.h>
struct shared /* ,
mas, - sema /*
57
2.1. -
- fork(). - fork()
-, .
, first, second, third.
.
, , ,
RR. 10 .
RR , .
, , . - , , , ,
-. , .
.
-
sem_t sema;
char mas[360];
} shar;
struct shared *addr;
//---------------------------------------------------------------------word_write(char arg[]) /* ,
, */
{
*/
58
int i, j, k, m;
strcat(addr->mas, " ");
for(i=0; i<strlen(arg); i++)
{
strncat(addr->mas, &arg[i], 1);
for(j=0; j<1000; j++)
for(k=0; k<1000; k++) m *= (k+j)/(k*j+1);
}
setbuf(stdout, NULL);
//
shm_unlink( "/bolts" );/* /bolts
, - . */
fd = shm_open( "/bolts", O_RDWR | O_CREAT, 0777 );
/* ( )
( -) /bolts */
ftruncate( fd, sizeof( shar ));/* /bolts
, */
addr = mmap(0,sizeof( shar ),PROT_READ | PROT_WRITE,MAP_SHARED,fd,0);
pid = fork(); // ,
if( pid == 0 ) {
//
printf("child with pid %d\n", getpid());
pid2 = fork(); /* -, */
if (pid2 == 0) {
// -
printf("grandchild with pid %d\n", getpid());
for(n=0; n<10; n++)
{
while(sem_wait(&addr->sema)&& errno==EINTR);
word_write(thrd);
59
//
strcpy(addr->mas, "first"); /* "first" */
sem_init(&addr->sema, 1, 1);// 1 ( )
close( fd );
// -
shm_unlink( "/bolts" );/* . ,
*/
sem_post(&addr->sema);
}
printf("grandchild finishies\n");
}
else {
// ,
for(n=0; n<10; n++)
{
while(sem_wait(&addr->sema)&& errno==EINTR);
/*
word_write().
sem_wait()
, EINTR */
}
wait(NULL);
// -
printf("child finishies\n");
}
} else { ,
printf("parent with pid %d\n", getpid());
for(n=0; n<10; n++)
{
while(sem_wait(&addr->sema)&& errno==EINTR);
word_write(frst);
sem_post(&addr->sema);
}
wait(NULL);
//
printf("parent: shm= %s\n", addr->mas);
60
word_write(sec);
sem_post(&addr->sema);/* */
sem_destroy(&addr->sema); // .
printf("parent finishies\n");
}
}
. .
pcahrielndt wwiitthh ppiidd 786471782374
pcahrielndt
. .
wwiitthh ppiidd 827431823334
61
62
- collector logger.
. Collector
, logger .
#include
#include
#include
#include
#include
#include
<stdio.h>
<pthread.h>
<inttypes.h>
<fcntl.h>
<errno.h>
<sys/mman.h>
63
printf("sensor1 starts\n");
while(1)
{
usleep(100000);
sensor_1 = rand_r(&seed3) / 100;
//printf("*%d ", sensor_1);
}
64
}
//----------------------------------------------------------------------------void *sensor2()// - . sensor_2
{
printf("sensor2 starts\n");
while(1)
{
sensor_2 = rand_r(&seed4) / 100;
//printf("**%d ", sensor_2);
usleep(100000);
}
}
//----------------------------------------------------------------------------void *recorder1() // -
{
pthread_barrier_wait(&barrier); //
printf("recorder1 starts\n");
while(1)
{
pthread_mutex_lock (&mtx1);
/* is_data sensor_read */
65
66
pthread_barrier_wait(&barrier); //
printf("recorder2 starts\n");
while(1)
{
pthread_mutex_lock (&mtx1);
while (is_data ==2 || is_data == 3) pthread_cond_wait (&cv1,
&mtx1);
usleep(rand_r(&seed2)*79);
sensor_read[1] = sensor_2;
printf(" \nrecorder2 %d\n", sensor_read[1]);
if (is_data ==0 || is_data ==1) is_data += 2;
pthread_cond_broadcast (&cv1);
pthread_mutex_unlock(&mtx1);
}
}
//----------------------------------------------------------------------------void * collector ()
//-
{
int n;
printf ("collector starts\n");
while(1)
{
pthread_mutex_lock (&mtx2); /*
, logger */
while (!data_logged) pthread_cond_wait (&cv2, &mtx2);/* -
67
cv2, logger
data_logged 1 */
mean1=variance1=mean2=variance2=0;/*
*/
for(n=1; n<=10; n++)
{
pthread_mutex_lock (&mtx1);
while (is_data < 3 ) pthread_cond_wait (&cv1, &mtx1);
// process data
printf ("--collector: %d %d %d\n", n, sensor_read[0],
sensor_read[1]);
delta1 = sensor_read[0] - mean1;
delta2 = sensor_read[1] - mean2;
mean1 = mean1 + delta1 / n;
mean2 = mean2 + delta2 / n;
variance1= variance1 + delta1*(sensor_read[0]-mean1);
variance2= variance2 + delta2*(sensor_read[1]-mean2);
sensor_read[0] = sensor_read[1] = is_data = 0;
pthread_cond_broadcast (&cv1);
pthread_mutex_unlock (&mtx1);
}
variance1= variance1 / (10-1);
variance2= variance2 / (10-1);
printf("Collector: mean1=%d mean2=%d variance1=%d
variance2=%d\n", mean1, mean2, variance1, variance2);
data_logged = 0;
printf("logger starts\n");
num_logs = *(int*) arg; /* - logger.dat.
*/
// POSIX- logger.dat
fd = open( "logger.dat", O_WRONLY | O_CREAT | O_APPEND | O_TRUNC |
O_SYNC, S_IRUSR | S_IWUSR );
//
//fd = creat( "logger.dat", S_IRUSR | S_IWUSR );//
sprintf(buffer, "#\tmean1\tmean2\tvar1\tvar2\t\tlog_time\n");
68
}
//----------------------------------------------------------------------------void *logger(arg)
//-
{
int num_logs;
int fd, j;
char buffer[100];
int len;
char log_time[26];
time_t time_of_day;
struct timespec * stop_time;
69
printf("main starts\n");
setbuf(stdout, NULL); //
seed1 = (unsigned int) time(NULL);//
70
seed3 = 1234;
seed4 = 4321;
71
2.6. ( , sensor())
,
sens[5]. .
- ( - re-
72
#include <pthread.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#define el 5
#define repeat 15
// 5
// 15
unsigned sens[el]={0};
//
pthread_rwlock_t rwl;
pthread_barrier_t barrier_el, barrier_el2;
73
corder()) - (( - analyzer()). -
array_of_max, . -
( ,
- ).
15 . , ,
. () .
- , -: - (..) sens, - (.. )
,
.
74
//-----------------------------------------------------------------------------------------------------stdout_to_file() /* stdout
logged_data.dat.
*/
{
fflush(stdout);
fgetpos(stdout, &pos);
file_descriptor = dup(fileno(stdout));
mapped_fd = fileno(freopen("logged_data.dat", "w", stdout));/*
logged_data.dat ,
stdout reopen()*/
map_addr = (int *) mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE,
mapped_fd, 0); // .
map_addr-
}
//-----------------------------------------------------------------------------------------------------file_to_stdout() /* stdout
logged_data.dat.
{
fflush(stdout);
dup2(file_descriptor, fileno(stdout));
close(file_descriptor);
clearerr(stdout);
fsetpos(stdout, &pos);
msync(map_addr, 1024, MS_SYNC);/*
*/
munmap(map_addr, 1024);//
75
}
//-----------------------------------------------------------------------------------------------------int compare( const void *op1, const void *op2 )
/* qsort()
lsearch()
{
const int *p1 = (const int*) op1;
const int *p2 = (const int*) op2;
//printf("---%d\n", *p1 - *p2);
return(*p1 - *p2);
}
//-----------------------------------------------------------------------------------------------------void *sensor()// , - ()
{
int i = pthread_self()-2;
static unsigned int seed = 1234;
printf("Sensor %d starts\n", i);
while(1)
{
}
//------------------------------------------------------------------------------------------------------
76
77
}
pthread_rwlock_unlock(&rwl); // -
pthread_barrier_wait(&barrier_el2);/* 5
, */
}
//-----------------------------------------------------------------------------------------------------void *analyzer() //, - ( 2)
{
char encrypted_array[64] = {0};
int i;
78
}
printf("\narray_of_max ");// 15
for( i = 0; i < num; i++) printf( "%d ", array_of_max[i]); printf("\n");
qsort(array_of_max, num, sizeof(int), compare); /*
*/
printf("sorted array_of_max ");
for( i = 0; i < num; i++ ) printf( "%d ", array_of_max[i]);
printf("\n");
free(array_of_max);
printf("recorder ****\n");
for(i=0; i<el; i++) pthread_cancel(sens_thr[i]);/*
- - */
pthread_cancel(anlzr);
79
printf("main starts\n");
stdout_to_file(); /*
printf() */
setbuf(stdout, NULL);
pthread_rwlock_init(&rwl, NULL);
pthread_attr_init(&attr);
pthread_barrier_init(&barrier_el, NULL, el);
80
}
//-----------------------------------------------------------------------------------------------------main()
{
int i;
pthread_t rcrdr;
pthread_attr_t attr;
int rpt = repeat; // repeat 15 times reading of sensor series
81
4.
3
( /)
. .4.1
C QNX Neutrino
POSIX
POSIX
POSIX
( )
POSIX
POSIX
POSIX
,
82
shm_open()
shm_unlink()
mmap()
munmap()
ftruncate()
shm_ctl()
mq_open()
mq_close()
mq_unlink()
mq_setattr()
mq_send()
mq_receive()
mq_notify()
// , POSIX
#include <mqueue.h>
#include <stdlib.h>
//-----------------------------------------------------------------------------------------------------------------------main()
{
mqd_t pmq;
struct { int t;
float p;
} pair; // -
int i;
char rcv_buf[4096];
int received;
unsigned int msg_prio;
struct mq_attr mqstat;
sigset_t set;
pid_t child_pid;
int status;
84
mq_unlink("/my_pmq"); /* /my_pmq,
( ) */
pmq = mq_open("/my_pmq", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR , NULL);
//
mq_getattr(pmq, &mqstat); /* :
printf(" reader:POSIX mq created with attributes%ld %ld %ld %ld %ld\n",
mqstat.mq_maxmsg, /* - ,
*/
mqstat.mq_msgsize,
//
mqstat.mq_curmsgs,
// -
mqstat.mq_sendwait,
// 0,
mqstat.mq_recvwait); // 0,
child_pid = spawnl( P_NOWAIT, "mq_sensors-3.out", "mq_sensors-3.out",
NULL); //
if(child_pid>0) printf(" reader: mq_sensors with pid=%d launched\n",
child_pid);// pid
for(i=0; i<15; i++)//
{
received = mq_receive( pmq, rcv_buf, 4096, NULL );
printf(" reader: received %d bytes = %s", received, rcv_buf);
85
}
mq_close(pmq);// ,
mq_unlink("/my_pmq");
printf(" reader: Killing %d ...\n", child_pid);
kill(child_pid, SIGUSR1);// , SIGUSR1
waitpid(child_pid, &status, WEXITED);/*
*/
printf(" reader: WIFSIGNALED returns %d WIFEXITED returns %d\n",
WIFSIGNALED(status), WIFEXITED(status));
if (WIFEXITED(status))
{
printf(" reader: Sensors terminated normally, exit status = %d\n",
WEXITSTATUS(status));
}
else if (WIFSIGNALED(status))
{
printf(" reader: Sensors terminated abnormally by signal = %X\n",
WTERMSIG(status));
}
}
//=========================================================================
// mq_sensors-3.c,
#include <stdio.h>
#include <pthread.h>
#include <mqueue.h>
#include <stdlib.h>
#include <limits.h>
86
mqd_t pmq;
pthread_barrier_t barrier;
//-----------------------------------------------------------------------------------------------------------------------void handler(signo)// - SIGUSR1
{
if(signo == SIGUSR1)
{
printf("snsrs: catched stop signal %d. Terminate\n", signo);
fflush(stdout);
_exit( EXIT_SUCCESS );
}
}
//-----------------------------------------------------------------------------------------------------------------------void* sensor(arg)// -
{
static unsigned int* seed;
char snd_buf[20], snd_bufs[20];
struct
{
int t;
float p;
} pair;
sigset_t set;
unsigned int prio;
getpid(), pthread_self());
}
//-----------------------------------------------------------------------------------------------------------------------main()
87
sigemptyset( &set );
sigaddset(&set, SIGALRM);
pthread_sigmask(SIG_SETMASK, &set, NULL);/*
SIGALRM, main() */
while(1)
{
pthread_barrier_wait( &barrier ); /* , main()
*/
pair.t = rand_r(seed) / 100;
pair.p = rand_r(seed) / 1000.0;
sprintf(snd_buf, "%d %d %.2f\n", pthread_self(), pair.t, pair.p);
printf("snsrs: %s", snd_buf);
if(pthread_self()== 4) prio = MQ_PRIO_MAX;
else prio = 20 - pthread_self();
mq_send(pmq, snd_buf, 20,prio);
/* .
20 - pthread_self(). 3
MQ_PRIO_MAX=32 (. limits.h) */
}
88
pthread_t tid[5];
int seed[5] = {1234, 2143, 3412, 4321, 1423};/*
*/
pthread_attr_t attr;
int i;
char rcv_buf[4096];
//struct itimerval value; /* @ -
setitimer().
ualarm() @*/
sigset_t set;
int sig;
struct sigaction act;
pthread_barrier_init( &barrier, NULL, 6);
pthread_attr_init(&attr);
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
*/
ualarm(4000000, 4000000);/* 4 ,
4 */
printf("snsrs: interval timer launched\n");
for(i=0; i<5; i++) pthread_create(&tid[i], &attr, &sensor,
(void *) &seed[i]);
while(1)
{
sigwait(&set, &sig); /* SIGALRM ( 14 - . signal.h)
printf("snsrs: accepted signal %d\n", sig); fflush(stdout);
pthread_barrier_wait( &barrier );
}
89
value.it_value.tv_sec = 4;
value.it_value.tv_usec = 0;
value.it_interval.tv_sec = 4;
value.it_interval.tv_usec = 0;
setitimer ( ITIMER_REAL, &value, NULL);
}
. .4.2
C QNX Neutrino
/
( ) ( )
/ ( sigprocmask()
)
pthread_sigmask() / ( )
sigwait()
sigwaitinfo()
kill()
(.. , sigqueue()
)
SignalKill()
, , ..
pthread_kill()
3.5. ( 3.1, ).
90
sigaction()
sigemptyset()
sigaddset()
sigdelset()
sigfillset()
. 5 t p.
4 .
-. , -
.
91
// -,
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <process.h>
#include <sys/netmgr.h>
//-----------------------------------------------------------------------------------------------------------------------main()
{
pid_t child_pid;
int stat_loc;
sigset_t wait_set;
siginfo_t info;
int sensor, t;
float p;
extern void handler();
struct sigaction act_to_sensor;
sigset_t set;
int i, status;
// -
child_pid= spawnl( P_NOWAIT,"sig_sensors2.out","sig_sensors2.out",NULL);
if(child_pid>0) printf("sensors with pid=%d launched\n", child_pid);
for(i=0; i<6*5; i++)
// 6
{
sigwaitinfo(&wait_set, &info);// SIGRTMAX
printf("
PARENT receive signal %d with value %d ",
info.si_signo, info.si_value);
// (value) :
sensor = info.si_value.sival_int % 10;
92
sigemptyset(&wait_set);
// , sigwaitinfo()
sigaddset(&wait_set, SIGRTMAX);
t = info.si_value.sival_int / 1000000;
p = (float) ((info.si_value.sival_int - sensor) % 1000000)/1000.0;
printf(" sensor=%d t=%d p=%.2f\n", sensor, t, p);
fflush(stdout);
SignalKill(ND_LOCAL_NODE,child_pid,0,SIGUSR1,SI_USER,12345);/*
SIGUSR1 SI_USER (=0 POSIX, . c SignalKill())
12345 ( )
*/
sigwaitinfo(&set, &info);/* SIGCHLD
*/
switch (info.si_signo) {
case SIGCHLD:
printf(" PARENT: received signal SIGCHLD. A child with pid=%d
terminated\n", info.si_pid);
break;
default:
}
93
// SIGCHLD :
sigemptyset( &set );
sigaddset(&set, SIGCHLD);
act_to_sensor.sa_flags = 0;
act_to_sensor.sa_mask = set;
act_to_sensor.sa_handler = NULL; &handler;
sigaction( SIGCHLD, &act_to_sensor, NULL );
wait(&status); //
if (WIFEXITED(status))
{
printf("
child terminated normally, exit status = %d\n",
WEXITSTATUS(status));
}
else if (WIFSIGNALED(status))
{
printf("
child terminated abnormally by signal = %X\n",
WTERMSIG(status));
}
//=========================================================================
// sig_sensors2.c,
-
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <math.h>
94
}
//-----------------------------------------------------------------------------------------------------------------------void handler(signo)/* - .
. */
{
}
95
//-----------------------------------------------------------------------------------------------------------------------// - SIGUSR1
void signal_handler(int signo, siginfo_t *info, void *extra)
{
printf("snsrs: Received termination signal %d with code %d
and value %d\n", info->si_signo, info->si_code,
info->si_value.sival_int);
_exit(EXIT_SUCCESS);
}
//-----------------------------------------------------------------------------------------------------------------------void* sensor(arg) //
{
static unsigned int* seed;
struct
{
int t;
float p;
} pair;
sigset_t set_for_sensor, mask;
static count=0;
char snd_buf[30];
siginfo_t info;
int s_pair;
int pid, ppid, tid;
int aux;
pid = getpid();
ppid = getppid();
tid = pthread_self();
printf("snsrs: sensor
// pid
// pid
// tid -
%d %d starts. PARENT pid is %d\n",
pid, tid,
ppid);
sigfillset(&mask);
// , -
sigdelset(&mask, SIGALRM);// SIGALRM
pthread_sigmask(SIG_SETMASK, &mask, NULL);
while(1)
{
pthread_sigmask(SIG_UNBLOCK, &set_for_sensor, NULL);
/*
set_for_sensor , .. SIGALRM */
sigwaitinfo(&set_for_sensor, &info); // SIGALRM
if(info.si_code == SI_TIMER) printf("received signal %d from timer
with value %d\n", info.si_signo, info.si_value);
pthread_sigmask(SIG_BLOCK, &set_for_sensor, NULL); /*
set_for_sensor , .. SIGALRM
- */
pair.t = rand_r(seed) / 100; //
96
sigemptyset(&set_for_sensor);
// , -
sigaddset(&set_for_sensor, SIGALRM); // SIGALRM
}
//-----------------------------------------------------------------------------------------------------------------------main()
97
pthread_attr_init(&attr); // -
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
/* SIGUSR1,
*/
sigdelset(&set, SIGUSR1);
act.sa_flags = 0;
act.sa_mask = set; /*
- */
act.sa_handler = &signal_handler; //
sigaction( SIGUSR1, &act, NULL );
sigfillset( &set );
98
pthread_t tid[5];
int seed[5] = {1234, 2143, 3412, 4321, 1423};
pthread_attr_t attr;
int i;
struct sigevent event;
struct itimerspec itime;
timer_t timer_id;
sigset_t set, set_for_main;
struct sigaction act;
int signal_value = pthread_self();
// 1
short signal_code = SI_TIMER;
// -1
. .4.3
C QNX Neutrino
99
sigemptyset(&set_for_main); /* main.
SIGALRM, -*/
sigaddset(&set_for_main, SIGALRM);
pthread_sigmask(SIG_SETMASK, &set_for_main, NULL);
/* , signal_code
= SI_TIMER (= -1 POSIX) signal_value (=1) :
SIGEV_SIGNAL_CODE_INIT( &event, SIGALRM, signal_value, signal_code );
timer_create(CLOCK_REALTIME, &event, &timer_id);
// :
itime.it_value.tv_sec = 4;
//4
itime.it_value.tv_nsec = 0;
itime.it_interval.tv_sec = 4; //4
itime.it_interval.tv_nsec = 0;
timer_settime(timer_id, 0, &itime, NULL);
printf("snsrs: interval timer launched\n");
// - :
for(i=0; i<5; i++) pthread_create(&tid[i], &attr, &sensor,
(void *) &seed[i]);
for(i=0; i<5; i++) pthread_join(tid[i], NULL);
ChannelCreate()
ChannelDestroy()
ConnectAttach()
ConnectDetach()
MsgSend()
MsgReceive()
MsgInfo()
MsgReply()
MsgWrite()
MsgRead()
MsgSendPulse ()
MsgReceivePulse()
MsgDeliverEvent()
3.6. ( API
QNX Neutrino). -,
, -
100
MsgError()
,
QNX Neutrino
(. - MsgSend*())
(.
- MsgReceive*())
,
struct _msg_info
(. - MsgReplyv())
(. MsgWritev())
(. MsgReadv())
. - , / . 6 -. , , . , , (. 1).
101
//message_ring_first.c
- PARENT
#include <stdlib.h>
#include <sys/netmgr.h>
/* ND_LOCAL_NODE=0 QNET,
*/
#include <process.h>
#include <sys/neutrino.h>
//system("clear"); //
par_id = getpid();
printf("PARENT with pid %d started. ", par_id);
chid = ChannelCreate(0); //
// , :
itoa(nd, nd_s, 10);
// QNET,
itoa(par_id, pid_s, 10); // pid
102
//--------------------------------------------------------------------------------------------------------------------main()
{
int chid;
int rcvid;
int coid;
int nd = ND_LOCAL_NODE;
// 0
pid_t par_id, chil_id;
char my_msg[7] = "PREVED";
char rmsg[20]="";
char nd_s[8 * sizeof( int ) + 1], pid_s[8 * sizeof( int ) + 1],
chid_s[8 * sizeof( int ) + 1],count_s[8 * sizeof( int ) + 1];
int status = 0;
char reply[15] = "Hi, Son!";
pid_t child_id;
int count=0;
int i;
struct _msg_info info;
}
//===============================================================================
103
// message_ring_other.c // - ( message_ring-other.out)
#include <sys/neutrino.h>
#include <sys/netmgr.h>
#include <process.h>
//argv[1] // pid
//argv[2] /* .
104
*/
ppid = atoi(argv[3]);
// pid
chid = atoi(argv[4]);
//
other_pid = getpid();
// pid
count = atoi(argv[5]);
//
105
//
chid = ChannelCreate(0);
itoa(other_pid, par_pid_s, 10);
itoa(chid, chid_s, 10);
itoa(count, count_s, 10);
child_id = spawnl(P_NOWAIT, "message_ring-other.out",
"message_ring-other.out",
argv[1], argv[2], par_pid_s, chid_s, count_s, NULL);
delay(100);
rcvid = MsgReceive(chid, rmsg, sizeof(rmsg), &info);
printf("%d: received message from parent %d is \"%s\"\n", other_pid,
info.pid, rmsg);
MsgReply( rcvid, status, reply, 15 );
ChannelDestroy(chid);
106
rmsg);
ConnectDetach(coid);
exit(0);
//
107
108
- (.. ) RSA-
p q p*q, p*q (1024 2048-).
.
RSA_Server5, RSA_Client5 RSA_Receiver5.
// cl_ser5.h
#include <inttypes.h>
#include <sys/neutrino.h>
#include <math.h>
#include <sys/dispatch.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define ATTACH_POINT "RSA-Server" // ,
( /dev/name/local/)
typedef struct _my_data { //
//struct _pulse hdr;
struct sigevent evnt;
uint64_t data;
} my_data_t;
if(u&1) s = (s*t)%n;
u >>=1;
t = (t*t)%n;
}
return s;
}
================================================================================
// RSA_Server5.c
//# gcc -oRSA_Server5.out RSA_Server5.c -Ldir /lib/libm.a
//# qcc -oRSA_Server5.out RSA_Server5.c -l /lib/libm.a
109
#include "cl_ser5.h"
}
*uu = u[0]; *vv = u[1];
//printf("***%lld %lld %lld %lld
v[0], v[1], v[2]);
return u[2];
// gcd
%lld
110
}
//---------------------------------------------------------------------------------uint64_t isprime(uint64_t s)
{//
uint64_t m, n, i = 1;
long long u, v;
for(n=2ULL; n <= (uint64_t) ceil(sqrt((double)s)); n++)
if(euclid(s, n, &u, &v) > 1) {i=0; break;}
if(i) return s; else return i;
}
//---------------------------------------------------------------------------------main()
{
name_attach_t *attach;
int rcvid;
uint64_t sqrt_ullmax, i, p=0ULL, q=0ULL, e, aux, gcd, plain_text,
cipher;
int64_t u, v, d;
int k1, k2, j;
j = 0; //
for(i = sqrt_ullmax/scale; i<sqrt_ullmax/scale+scale; i++) {
if(isprime(i))
{
if(j==k1) p=i;
if(j==k2) {q=i; break;}
j++;
}
}
111
/*------------------------- RSA----------------------------*/
srand((int) (time(NULL)*rand()));
k1 = rand() / 1000;
// k1 k2
do k2=rand() / 1000; while(k2 <= k1);
sqrt_ullmax = (uint64_t) sqrt((double)ULLONG_MAX);
112
//printf("p=%lld
q=%lld\n", p, q);
aux = (p-1)*(q-1);
do e = rand() / 8000; while (e<=3); // RAND_MAX=32767u (. stdlib.h)
e = (e % 2) ? e : e - 1;
//printf("e0=%lld ", e);
for(e; e < aux; e += 2)
if((gcd = euclid(e, aux, &u, &v)) == 1ULL) break; // 2 =< e <= (p-1)(q-1)
d = u;
//printf("e=%lld d=u=%lld v=%lld test: e*u+aux*v=%lld (must be 1)\n",
e, u, v, e*u+aux*v);
if(u < 0)
{
d += aux;
/*printf("d=%lld e*d %% test: aux=%lld (must be 1)\n",
d, e*d % aux); */
}
printf("Private key: (e=%lld p*q=%lld).\n
Public key: (d=%lld p*q=%lld).\n", e, p*q, d, p*q);
/*------------------------- RSA-------------------------*/
113
}
*ptr = d;
//
*++ptr = p*q;
ptr -= 6;
for(j=0; j < 7; j++) {printf("%lld ", *ptr); if(j==6) break; ptr++; }
ptr -= 6;
printf("\n");
MsgDeliverEvent(receiver_rcvid, &r_event); /* RSA_Receiver ,
, */
114
do {// RSA_Receiver
rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), NULL);
} while(msg.evnt.sigev_code != _PULSE_CODE_MINAVAIL+2);
MsgWrite(rcvid, ptr, 7*sizeof(uint64_t), 0);// RSA_Receiver
MsgReply(rcvid, 0, NULL, 0); // RSA_ Receiver
free(ptr);
name_detach(attach, 0);
main()
{
my_data_t msg;
uint64_t reply=12345;
int fd, i;
if ((fd = name_open(ATTACH_POINT, 0)) == -1) { //
return EXIT_FAILURE;
//
}
msg.evnt.sigev_notify = SIGEV_PULSE; /*
, , RSA_Client5 */
115
}
// RSA_Client5.c
//#gcc -oRSA_Client5.out RSA_Client5.c -Ldir /lib/libm.a
//#qcc -oRSA_Client5.out RSA_Client5.c -l /lib/libm.a
#include "cl_ser5.h"
msg.evnt.sigev_coid = fd;
//msg.evnt.sigev_priority =
msg.evnt.sigev_code = _PULSE_CODE_MINAVAIL;
//msg.evnt.sigev_value =
for (i = 0; i < 5; i++) //
{
//5
msg.data = rand() / 873;
printf("CLIENT : sending %u \n", msg.data);
if (
MsgSend(fd, &msg, sizeof(msg), NULL, 0) == -1) break;
//printf("\t Received Reply from Server %u\n", reply);
}
// RSA_Receiver5.c
//qcc -oRSA_Receiver5.out RSA_Receiver5.c -l /lib/libm.a /*
*/
// ,
#include "cl_ser5.h"
#define MY_PULSE_CODE _PULSE_CODE_MINAVAIL+5
#define MY_PULSE_VALUE 97531
//-------------------------------------------------------------------------------------------------------------------------------------main()
{
int fd;
116
}
name_close(fd);
117
do
{
msg.evnt.sigev_code = _PULSE_CODE_MINAVAIL+2;
MsgSend(fd, &msg, sizeof(msg), buf, sizeof(buf));
/*
, 5 - buf */
for(i=0; i < 7; i++) printf("%lld ", buf[i]);/*
*/
printf("\n");
d = buf[5];
pq = buf[6];
/*------------------------------------------*/
// = (^d) mod pq
for(i=0; i < 5; i++)
{
plain_text = x_pow_y_mod_z1(buf[i], d, pq);
printf("plain_text=%lld\n", plain_text);
}
name_close(fd);
// , name_open()
ConnectDetach(coid); //
ChannelDestroy(chid); //
119
}
( ). C 64 , RSA .
, , GNU GMP
(http://www.swox.com/gmp/). UNIX- - bc
. bc RSA- / bc (pipes) UNIX.
#include <stdio.h>
#include <limits.h>
#include <inttypes.h>
#include <stdlib.h>
#include <unistd.h>
}
//------------------------------------------------------------------------------------------------------main()
{
uint64_t t, e=17ULL, p=3480837ULL, q=3482211ULL;
char buff[BUFF_SIZE], sh_command[100], bc_command[100],
gzip_command[100], pq[100], cipher[100];
FILE *pipe_fp, *pipe_fp2;
120
f = popen(sh_command, "r");// bc
fgets(s_rezult[0], 100, f);//
//puts(s_rezult);
return *s_rezult;
/* QNX 6.2.
"proba.gz" */
remove("proba.gz");
for( t=175ULL; t < 186; t++)
{
sprintf(bc_command, "(%lld^%lld)%%%lld", t, e, p*q);
printf("%s = ", bc_command);
sprintf(sh_command, "echo \"%s\" | bc", bc_command);
pipe_fp = popen(sh_command, "r");
fgets(cipher, 100, pipe_fp);
pclose(pipe_fp);
printf("%s", cipher);
sprintf(gzip_command, "echo \"%s\" | gzip -fc >> proba.gz",
121
}
else if( ! memcmp( "6.3.", buff, 4)) // QNX 6.3.
{
sprintf(pq, "%lld", p*q);
for( t=175ULL; t < 186; t++)
{
printf("(%u", t);
printf("^%u)", e);
printf("%%%s=", pq);
strcpy(cipher, x_pow_y_mod_z3(t, e, pq));
printf("%s", cipher);
}
}
/* :
# ./big_pow_with_bc6.out
ULONG_MAX=4294967295, ULLONG_MAX=4294967295, ULONGLONG_MAX=4294967295
x=3480837*3482211=577719951765990823(%lld)
x=3480837*3482211=950726055(%u)
QNX Release: 6.3.0
(175^17)%12121008890607=11222799716509
122
(176^17)%12121008890607=11998102008764
(177^17)%12121008890607=6101387353053
(178^17)%12121008890607=8988491121169
(179^17)%12121008890607=2723319392012
(180^17)%12121008890607=7105026443589
(181^17)%12121008890607=3909385636786
(182^17)%12121008890607=4902636545753
(183^17)%12121008890607=7174218074796
(184^17)%12121008890607=10072806839179
(185^17)%12121008890607=4517402993057
#
123
# ./big_pow_with_bc6.out
ULONG_MAX=4294967295, ULLONG_MAX=4294967295, ULONGLONG_MAX=4294967295
x=3480837*3482211=-3344241241(%lld)
x=3480837*3482211=950726055(%u)
QNX Release: 6.2.0
(175^17)%12121008890607 = 11222799716509
(176^17)%12121008890607 = 11998102008764
(177^17)%12121008890607 = 6101387353053
(178^17)%12121008890607 = 8988491121169
(179^17)%12121008890607 = 2723319392012
(180^17)%12121008890607 = 7105026443589
(181^17)%12121008890607 = 3909385636786
(182^17)%12121008890607 = 4482252218624
(183^17)%12121008890607 = 7174218074796
#bc
(992802946^2)%1234749221
127094068
(992802946^3)%1234749221
210000111
(992802946^4)%1234749221
1414285714285714286193147029 ???
(992802946^5)%1234749221
915194871
(992802946^6)%1234749221
270614289
(992802946^7)%1234749221
459978554
124
(184^17)%12121008890607 = 10072806839179
(185^17)%12121008890607 = 4517402993057
# */
. - bc QNX Momentics :
QNX (6.2 6.3)
, . , QNX 6.2 . QNX 6.3 (. ). . .
bc 6.3 6.2.
. , , ( ):
(992802946^8)%1234749221
501686632
:
# bc
(65534^101)%2345678
1113772 -
(65540^101)%2345678
3000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000030000000000000000000000000000000\
0000000000000000000000000000000293672 = 293672
, 2008 . 6.3.0
SP3+QNX Neutrino Core OS 6.3.2A
125
(65535^101)%2345678
3000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000002197645
= 2197645
QNX, bc !
126
5. 4
( )
. .5.1
C QNX Neutrino
timespec
,
timer_create()
TimerCreate()
()
timer_settime()
TimerSettime()
timer_gettime()
TimerInfo()
,
timer_delete()
TimerDestroy()
,
ualarm()
SIGALRM
, SIGALRM
alarm()
TimerAlarm()
timer_timeout()
TimerTimeout()
clock_getres()
( )
ClockPeriod()
( )
clock_gettime()
ClockTime()
clock_settime()
ClockCycles()
64-
4.1.
. main , . ,
. ,
main, , .
.
TSC . . time
( #time ./_) .
127
SYSPAGE_ENTRY()
, timespec
timespec2nsec()
timespec
nsec2timespec()
(
sleep()
)
delay()
usleep()
,
nanosleep()
timespec
,
clock_nanosleep()
timespec
#include
#include
#include
#include
#include
#include
<stdlib.h>
<pthread.h>
<sys/neutrino.h>
<errno.h>
<inttypes.h>
<sys/syspage.h>
128
event.sigev_notify = SIGEV_UNBLOCK; /* ,
MsgReceivePulse_r */
timeout.tv_sec = 2; // pthread_join() 2.0
timeout.tv_nsec = 0;
timeout_in_sec =(double)timeout.tv_sec + double)timeout.tv_nsec/BILLION;
//
do //
{
timer_timeout(CLOCK_REALTIME, _NTO_TIMEOUT_RECEIVE, &event,
&timeout, NULL);
ret = MsgReceivePulse_r( chid, &pulse, sizeof(pulse), NULL );
129
if(ret == -ETIMEDOUT) //
{
printf("\tChild: ret=%d timeout %.3f s on pulse waiting
elapsed\n", ret, timeout_in_sec);
}
else
// , main
{
printf("\tChild: ret=%d, received termination pulse with code %d
and value %d\n", ret, pulse.code, pulse.value.sival_int);
clock_gettime( CLOCK_REALTIME, &stop_time); /*
*/
printf("\tChild thread stopped at %u sec %ld nsec system
time\n",(uint64_t)stop_time.tv_sec, stop_time.tv_nsec);
tick_time=(uint64_t(stop_time.tv_secstart_time.tv_sec)*BILLION+
(stop_time.tv_nsec - start_time.tv_nsec);/*
*/
accum = (double) tick_time / (double)BILLION; );/*
*/
ClockPeriod(CLOCK_REALTIME, NULL, &tick, 0); /*
*/
printf( "\tChild: System clock tick is %ld ns.
Child time is %lf s %lf tick.\n", tick.nsec, accum,
(double)tick_time / (double)tick.nsec);
pthread_exit(NULL);
}
//-------------------------------------------------------------------------------------------------------------------------int main( void )
{
pthread_attr_t attr;
pthread_t thr;
struct sigevent event, timer_event;
struct itimerspec itime;
struct timespec timeout;
double wait_time = 0.0, timeout_in_sec;
int ret;
timer_t timer_id;
uint64_t cps, cycle1, cycle2, ncycles;
130
}
} while (1);
131
float sec;
cycle1=ClockCycles( ); // TSC main
printf("Main with pid %d starts\n", getpid());
event.sigev_notify = SIGEV_UNBLOCK; /* ,
pthread_join()*/
timeout.tv_sec = 1; // pthread_join() 1.05
timeout.tv_nsec = 50000000;
// 0.05
timeout_in_sec = (double)timeout.tv_sec+(double)timeout.tv_nsec/BILLION;
//
chid = ChannelCreate(0);
//
timer_event.sigev_notify = SIGEV_PULSE;// , -
timer_event.sigev_coid = ConnectAttach(0, 0, chid, _NTO_SIDE_CHANNEL,0);
timer_event.sigev_priority = getprio(0);/*
*/
timer_event.sigev_code = MY_PULSE_CODE; //
timer_event.sigev_value.sival_int = 12345;// ,
132
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&thr, &attr, &function, NULL );//
for(;;)//
{
timer_timeout( CLOCK_REALTIME, _NTO_TIMEOUT_JOIN, &event, &timeout,
NULL);
ret = pthread_join(thr, NULL);
if(ret == ETIMEDOUT) //
{
wait_time += (double) timespec2nsec(&timeout) / BILLION;
printf("Main: ret=%d. Join_timeout %.3f s elapsed.
Waiting for child thread termination %.3f s\n",
ret, timeout_in_sec, wait_time );
}
else
// pthread_join() .
{
printf("Main: ret=%d. Child thread terminated\n", ret);
break;
}
}
ChannelDestroy(chid);
timer_delete(timer_id);
cycle2=ClockCycles( ); // TSC main
ncycles=cycle2-cycle1;
cps = SYSPAGE_ENTRY(qtime)->cycles_per_sec;
sec=(float)ncycles/cps;
133
134
1. , . QNX Neutrino 2: QNX Realtime
Platform - .: , 2001 - 480 .
- .:-, 2005 . -400 .
2. , .. QNX:
. 2- ., . . .: , 2004. 192 .
3. , .. QNX Momentics: . .:,2005.-256 .
http://swd.ru/index.php3?pid=499
4. , .. POSIX. 1
2. : : , 351400 /
.. . .: - . . 2005.-384 .
http://www.intuit.ru/department/se/pposix/
http://www.intuit.ru/department/se/posix2/
5. . . UNIX: = UNIX Network
Programming Volume 2: . ./ . . , W. R.
Stevens. -.: , 2003. -576 .: . -(-).
6. ., . QNX/UNIX: /
., . .: -, 2006. -288 .
- :
- qnx.org.ru.
- openqnx.com.
- qnxclub.net.