134
Методические материалы по курсу Системы реального времениЗадания для лабораторных работ и методические указания по их выполнению для студентов очной формы обучения специальностей «ПО» и «МОА» кафедры ИиПОБГТУ (Редакция 9-05-2008)

Системы реального времени Методичка Для Очников 05-2008

Embed Size (px)

DESCRIPTION

Системы реального времени Методичка Для Очников 05-2008

Citation preview

  • ( 9-05-2008)

  • 2

    .............................................................................................................................................. 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. - . - .

  • 3

    ()

    (). - - . - :

    - ( ) / , . , ;

    - - ;

    - , - . -

    , ( ).

    QNX Momentics QSS (http://www.qnx.com). - :

    - , Neutrino , ;

    - API POSIX, - , - POSIX- . -

    - , - , QNX Momentics.

  • 4

    - QNX Momentics. QNX, (Vm-ware, 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 .

  • 5

    -.

    . 9 - 2.1 ( 9: .5-2; .6-2; .7-2; .8-1; .9-3)

    2.1

    . 5 . 6 . 7 . 8 . 9

    9 2 2 2 1 3

    fork()

    - -

    [ (.5)] - --. fork() [- (.6)] , . . , [ , , (.7)]. - , [ (.8)]. -: parent, daugter grandchild. . - -. , , - RR. 10 . - RR , . [ (.9)], , . -.

  • 6

    1

    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 - , . , , - .

  • 7

    . - 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 ? -.

  • 8

    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()

  • 9

    2

    / 1

    / 2

    / 1

    / 2

    1. 10 10 RR RR 2. 10 10 RR FIFO 3. 10 9 FIFO RR 4. 9 10 FIFO RR 5. 10 10 FIFO FIFO 6. 9 10 FIFO FIFO

    3

    ( sched_yield())

    / 1 / 2

    1. - N

    2. N 10000

    3.

    4

    / 1 / 2

    1.

    N 11000 - ( RR->FIFO

    FIFO->RR): N>0 1, N0 1 +1, N

  • 10

    1-30 1

    1-10 -

    1.1

    . 1 . 2 . 3 . 4

    1. 1 6 1 3 2. 3 5 2 3 3. 2 4 3 1 4. 4 3 3 2 5. 3 2 3 1 6. 4 1 2 3 7. 1 3 1 3 8. 2 5 1 3 9. 3 2 2 3

    10. 4 4 3 1

    11-20 -

    1.2

    . 1 . 2 . 3 . 4

    11. 4 3 3 1 12. 4 6 2 3 13. 3 1 3 3 14. 3 4 1 3 15. 1 2 3 1 16. 1 5 2 3 17. 2 1 1 3 18. 2 2 1 3 19. 2 3 3 3 20. 4 4 3 2

  • 11

    21-30 -

    1.3

    . 1 . 2 . 3 . 4

    21. 4 1 3 3 22. 1 2 3 2 23. 2 6 2 3 24. 3 5 1 3 25. 2 4 3 1 26. 1 3 1 3 27. 3 2 1 3 28. 4 6 3 1 29. 1 4 3 3 30. 2 1 2 3

  • 12

    2

    2.1 ( )

    (.5) ---. (.6) , . - . - , , (.7). , - (.8). : parent, daugter grandchild. - . . - , - , - - RR. 10 - . RR , . - (.9), - , . -. - .

    2.2 ( )

    - main() - - N 64- uint64_t - , 0. 30 ,

  • 13

    . - while(1) {}. (incre-mentor) +1 N , UNIX 01.01.1970, N ( time()). (supervisor) , N , N . (decrementor) - N N -1 . - 1 N, , - . - - .

    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(), isal-pha().

    2.9 ( -, )

    2.6, -. , .

    5

    1. 2.

  • 16

    6 -

    1. pthread_create() 2. fork() 3. spawnv()

    7

    1. 2.

    8

    ,

    1. 2.

    3. , 4.

    9

    1. 2. 3. 4. (condition variable) 5. / (read-write lock) 6. (sleepon lock)

  • 17

    1-30 2

    1-10 -

    2.1

    . 5 . 6 . 7 . 8 . 9

    1.

    1 1 1

    1 1 2. 2 2 3. 4 3 4. 1 2 5. 2 1 6. 2 2 2 1 1 7. 3 2 8.

    2 2 2 4 3

    9. 1 3 10. 3 1

    11-16 -

    2.2 2.3 2.4 2.5

    . 6 11. 1 - - - 12. - 1 - - 13. - - 2 - 14. - - - 3 15. - - 3 - 16. - - - 2

    17-22

    2.6 2.7

    . 9 17. 2 - 18. - 1 19. 3 - 20. - 2 21. 1 -

  • 18

    22. - 3

    23-30

    - -

    2.8 2.9

    . 9 -

    --

    - - -

    --

    23. 1 5 - - 24. - - 3 4 25. - - 1 4 26. - - 2 4 27. 6 5 - - 28. 3 5 - - 29. 2 5 - - 30. - - 6 4

  • 19

    3

    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

    -

    3.1 3.2

    . 9 1. 1 - 2. 2 - 3. - 3

    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. QNX Neutrino 13. QNX Neutrino 14. SIGRTMAX

    3.5 (, QNX Neutrino)

    mat1 mat2 m , n n , p - (m 9, n 9, p 9)

    { { 321pnnmpm

    matmatmat,,,

    2*1= mat1 mat2 -10 +10.

    mat i, j - - i- mat1 j- mat2 - ( 0)

  • 22

    =

    =1

    0,,, 2*1

    n

    kS

    jkkiji

    k

    matmatmat 44 344 21 (*)

    mati,j -, - i mat1 j mat2. - , , -. - - (.12) -, mat, . (.13) main, - .

    12

    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 - 20 -

    3.5 12 13

    15. 1 1 16. 1 2 17. 2 1 18. 2 2 19. 3 1 20. 3 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 - 30 -

    3.6 12 13 14

    21. 2

    2 1 22. 1 2 23. 2 2 24.

    3 1 2

    25. 1 1 26. 2 2 27.

    1 2 1

    28. 2 2 29. 1 1 30. 2 2 2

  • 25

    .10

    4-11 3

    ( 3.3 - ) 4 -11

    . -

    4.

    - MsgSend()

    MsgReceive() -

    - - , - (), (. MsgDeliverE-vent). (chid) c . - -, .

    5. , - value MsgSendPulse()

    6. SIGUSR1 , value pthread_kill()

    SignalKill()

    sigwaitinfo()

    - - . (tid), - . - tid=1, tid=2, 3, . tid

    7. SIGSPECIALMAX , value

  • 26

    tid+1 - .

    8.

    - MsgSend()

    MsgReceive() -

    - - - spawn*()) , (), - (. MsgDeliverEvent). (chid) . - - -, .

    9. , - value MsgSendPulse()

    10. SIGUSR1 , value

    SignalKill() sigwaitinfo()

    11. SIGSPECIALMAX , value

  • 27

    4

    , -

    4.1. ( )

    1.1, 1.2, 1.3 :

    1. , ( -), , N 13.57 , . - . .15. () ( joina-ble). - - .

    2. () (. 16), SIGEV_UNBLOCK. pthread_join() . , pthread_join() - ( ETIMEDOUT), - pthread_join() timeout elapsed pthread_join() . pthread_join() - ( EOK), - pthread_join() returned successfully.

    3. :

    1) clock_gettime(). 2) ClockCycles(). 3) getrusage().

    , . -

  • 28

    , , . , - . - , time.

    1-30 4

    15 , -

    - 1.*

    -

    1, 2, 3

    timer_create() timer_settime()

    - - 10, 11, 12, SIGUSR1

    -

    26, 27, 28 SIGALRM

    4, 5, 6 SIGUSR2 -3

    16, 17, 18, - -19, 29, 30 - -

    20, 21, 22 SIGALRM -5 7, 8, 9 TimerAlarm()

    SIGALRM - 13, 14, 15 setitimer()

    23, 24, 25 delay() -

  • 29

    16

    , pthread_join()

    1.1

    . - 1) 20 ( -, ), 2) 10 - .

    1.2

    . - 1) 18.05 ( -, ), 2) 6.33 - -.

    1.3

    . - 1) 15.78 ( , - ), 2) 12.95 - .

  • 30

    1. QNX Momentics

    UNIX- , , # root $ .

    - . , myprog.out , $./myprog.out.

    , SIGINT (Ctrl-C) SIGQUIT (Ctrl-\) - , - . , $slay _ ( SIGTERM). . $kill SIGKILL pid ( #kill 9 pid. 9 - SIGKILL), pid -.

    QNX - ( . QNX Neutrino User's Guide/Using the Photon microGUI/Hotkeys and shortcuts, - pedit) : Ctrl-Alt-1, Ctrl-Alt-2, Ctrl-Alt-3, Ctrl-Alt-4- 1, 2, 3, 4 ( 4 ); Ctrl-C ( ); Ctrl-Alt-V ( pterm); Ctrl-Alt-C ; Ctrl-V (); , - / -;

  • 31

    Ctrl-Alt-, Ctrl-Alt- - Photon; Alt-F4 ; Alt-Tab . FAT32 - ( - /) QNX. - /fs hd0-dos*. CD- ( - /fs/cd0), USB- (USB Mass Storage Device). - FAT32- - , ( ):

    #mount t dos /dev/hd0t11.1 /fs/hd3. t dos ( dos, .. FAT32), /dev/hd0t11.1 - , /fs/hd3 , QNX- ( - , /fs). :

    #mount t dos /dev/fd0 /fs/floppy. NTFS QNX, - .

    - , . 1.1. - , QNX Momentics . - , - , summary (). -, - Utilities Summary ( ). - . . - C. - Summary of Functions.

    - , - ( malloc(), calloc(), realloc(), free()). (-

  • 32

    , , , - ..), Memory fault (core dumped). /var/dumps - .core.

    - , .. ( 2) gdb - , -. gcc (qcc gcc), , -, . core_dump_case.c .

    QNX Momentics 6.3.2 SP3 , , ( . ).

  • 33

    // core_dump_case.c #include #include int main( void ) { int *foo, *p, i; //foo = (int *)malloc(10*4); //*** for (p = foo, i = 12; i > 0; p++, i--) *p = i; for (i = 12; i > 0; foo++, i--) printf("%d ", *foo); printf("\n"); free(foo); } /* //*** # 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.

  • 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 // # */ /* //*** # 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.

  • 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 // # */

  • 36

    1.1 QNX

    ,

    cd

    . cd DOS/Windows. , UNIX- / : cd /home/mydir - mydir, /home

    chkfsys qnx4 c . . #chkfsys m /dev/hd0t79; #chkfsys rs / clear

    df . : #df -P. - qnx4 /dev/hd0t79 ( hd0t77 hd0t78)

    find

    . 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

    grep

    . . examples.txt ( sem, ) sem_examp.txt: #grep s F sem /home/mydir/examples.txt > /home/mydir/sem_examp.txt

  • 37

    ls . dir DOS/Windows. : ls /home/mydir

    more . DOS/Windows. : #pidin | more

    mount

    , /. . FAT, /fs/floppy: #mount t dos /dev/fd0 /fs/floppy. QNX 6.3.0 USB- . . io-usb.

    umount . : umount /fs/floppy

    on

    / . . myprog.out, /home/mydir, - 15 FIFO: #on p15f /home/mydir/myprog.out

    passwd

    . . #passwd _ ;

    #passwd __ (- root)

    pidin

    . : #pidin ; #pidin p 1716269 pid = 1716269 ; #pidin | grep myprog myprog ; #pidin arg | grep devc-ser8250 , - devc-ser8250

  • 38

    pwd , .

    qcc, QCC

    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.

    gcc

    GNU-. -, QNX , , qcc, -. . , (, sin(x)): #gcc myprog.c omyprog.out -Ldir /lib/libm.a.

    sin

    . : #sin; #sin P myprog th myprog; #sin P io-graphics args , - io-graphics

    su . : $su root .

  • 39

    2. 1

    ( ) 2.1

    C QNX Neutrino

    spawnl()

    spawnlp() spawnv()

    spawnvp() . . spawn*() exit() system() , getpid()

    getppid()

    wait() -. . - wait*()

    sched_getparam() sched_setparam()

  • 40

    sched_getscheduler() sched_setscheduler()

    getprio() . (main()) - 1 ,

    setprio()

    . (main()) - 1 , . 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()

  • 41

    pthread_exit()

    pthread_setcancelstate() / -

    pthread_setcanceltype() (/ ) - pthread_testcancel() pthread_cancel() pthread_setname_np()

    QNX Neutrino Core OS 6.3.2. pthread_getname_np()

    C QNX Neutrino,

    2.2

    open() / / close() dup()

    dup2() ,

    setbuf() . NULL,

    time() ( , UNIX 1 1970 . 00 00 00 -

  • 42

    UTC (.. ) ) strftime() localtime() asctime()

    sleep() -. , - .

    mmap()

  • 43

    . 13 RR, 40 . N (. ) .

    , pid , - 9 FIFO. - : 100 101, . - - - N 0, :

    - N -15001. N= -15000 - 1.

    -, , - .

    : - , 14; - . pid ; - 19 RR; - 35 . - .

    , 2.0 1 - sin. 7 .

  • 44

    zad1_primer-prim_foutp_cancel.c #include #include #include #include #include #include #include #define STDOUT 1 int N = 0, stop=1; //------------------------------------------------------------------------------------------------ void* function_grandchild1( void* arg ) // - { 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); while(stop) /* N. stop

    */ { if (N

  • 45

    sched_yield(); // } } //------------------------------------------------------------------------------------------------ void* function_grandchild2( void* arg ) // - { int *number; pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, NULL); pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL); number = (int*) arg; printf("grandchild2 of primary process starts with tid %d number %d\n", pthread_self(),

    *number); while(stop) { if (N>0) while (N> -15001) --N; //printf("2: %d\n", N); sched_yield(); } } //------------------------------------------------------------------------------------------------ void* function_daughter() // { pthread_attr_t attr; struct sched_param param; int grndchld1=100, grndchld2=101; // void* arg;

  • 46

    int second_pid; char buffer[15]; pthread_t gch1, gch2; itoa(getpid(), buffer, 10); // pid - printf( "This is thread %d of primary process\n", pthread_self() ); errno = EOK; // second_pid = spawnl( P_NOWAIT, "./zad1_primer-sec.out", "./zad1_primer-sec.out", buffer, NULL ); if(second_pid != -1) printf("Secondary process with pid %d launched\n", second_pid); else

    { printf("Secondary process spawn error: %s\n", strerror( errno )); exit(EXIT_FAILURE);

    } pthread_attr_init( &attr ); // - pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); param.sched_priority = 9; /* 11- -

    . Photon . - , - . , Photon shell 10, -, FIFO (11) -. */

    pthread_attr_setschedparam(&attr, &param); pthread_attr_setschedpolicy(&attr, SCHED_FIFO); // FIFO arg = (void*) &grndchld1; pthread_create( &gch1, &attr, &function_grandchild1, arg ); arg = (void*) &grndchld2;

  • 47

    pthread_create( &gch2, &attr, &function_grandchild2, arg ); second_pid = wait(NULL); // printf("Daughter of the primary process: secondary process with pid %d stopped\n", second_pid); pthread_cancel(gch1); // - pthread_cancel(gch2); //stop = 0; // - } //------------------------------------------------------------------------------------------------ int main( void ) { pthread_attr_t attr; struct sched_param param; time_t time_of_day; char buffer[ 80 ];

    int fptr, oldstdout; /* 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 ,

  • 48

    printf( "Primary process with pid=%d starts at %s \n", getpid(), buffer ); pthread_attr_init( &attr );// pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); param.sched_priority = 13; pthread_attr_setschedparam(&attr, &param); // 13 pthread_attr_setschedpolicy(&attr, SCHED_RR);// Round-Robin pthread_create( NULL, &attr, &function_daughter, NULL ); sleep( 40 ); // 40 , - time_of_day = time( NULL ); strftime( buffer, 80, " %A %B %d, %Y %H:%M:%S", localtime( &time_of_day ) ); printf("The primary process comes to the end at %s. N=%d\n", buffer, N); return EXIT_SUCCESS; // }

  • 49

    zad1_primer-sec.c #include #include #include //--------------------------------------------------------------------- void* function( void* arg ) // { int policy; struct sched_param param; time_t time_of_day; char buffer[ 80 ]; 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, &param); while(param.sched_priority >=7) { param.sched_priority --; // 1 pthread_setschedparam(pthread_self(), policy, &param); time_of_day = time( NULL ); printf(" Local time %s\n", asctime(localtime(&time_of_day))); system("sin -P zad1_primer-sec th"); /* ,

    , sin, system() */ sleep(2); } return( 0 ); }

  • 50

    int main(int argc, char **argv) { pthread_attr_t attr; char buf[26]; time_t time_of_day; struct sched_param param; 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])); pthread_attr_init( &attr ); /* 19

    Round-Robin */ pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); param.sched_priority = 19; pthread_attr_setschedparam(&attr, &param); 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; }

  • 51

    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 . - . - - . . grandchild1 of primary process starts with tid 3 number 100 grandchild2 of primary process starts with tid 4 number 101 zad1_primer-sec.out 544805 4K 36K 12K 648K 6 1 14r NANOSLEEP B0328E11 5 2 18r REPLY 1 B0327A5D 0 Local time Thu Sep 28 13:52:47 2006 zad1_primer-sec.out 544805 4K 36K 12K 648K 6 1 14r NANOSLEEP B0328E11 5 2 17r REPLY 1 B0327A5D 1 Local time Thu Sep 28 13:52:50 2006 zad1_primer-sec.out 544805 4K 36K 12K 648K 6

  • 52

    1 14r NANOSLEEP B0328E11 5 2 16r REPLY 1 B0327A5D 1 Local time Thu Sep 28 13:52:52 2006 zad1_primer-sec.out 544805 4K 36K 12K 648K 6 1 14r NANOSLEEP B0328E11 5 2 15r REPLY 1 B0327A5D 1 Local time Thu Sep 28 13:52:54 2006 zad1_primer-sec.out 544805 4K 36K 12K 648K 6 1 14r NANOSLEEP B0328E11 5 2 14r REPLY 1 B0327A5D 1 Local time Thu Sep 28 13:52:57 2006 zad1_primer-sec.out 544805 4K 36K 12K 648K 6 1 14r NANOSLEEP B0328E11 5 2 13r REPLY 1 B0327A5D 1 Local time Thu Sep 28 13:52:59 2006 zad1_primer-sec.out 544805 4K 36K 12K 648K 6 1 14r NANOSLEEP B0328E11 5 2 12r REPLY 1 B0327A5D 1 Local time Thu Sep 28 13:53:01 2006 zad1_primer-sec.out 544805 4K 36K 12K 648K 7

  • 53

    1 14r NANOSLEEP B0328E11 5 2 11r REPLY 1 B0327A5D 2 Local time Thu Sep 28 13:53:04 2006 zad1_primer-sec.out 544805 4K 36K 12K 648K 7 1 14r NANOSLEEP B0328E11 5 2 10r REPLY 1 B0327A5D 2 Local time Thu Sep 28 13:53:06 2006 zad1_primer-sec.out 544805 4K 36K 12K 648K 8 1 14r NANOSLEEP B0328E11 5 2 9r REPLY 1 B0327A5D 3 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; .

  • 54

    3. 2

    ( ) C QNX Neutrino

    3.1

    pthread_mutexattr_init() - pthread_mutexattr_setpshared() /

    pthread_mutex_init() , -

    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 ( )

  • 55

    sem_wait() 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() -

  • 56

    pthread_barrierattr_init() - 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()

  • 57

    2.1. - - fork(). - - fork() -, . , first, second, third. . , , , RR. 10 . RR , . - , , . -- , , , , -. -, . .

    - #include #include #include #include #include #include struct shared /* , { mas, - sema /*

  • 58

    sem_t sema; char mas[360]; } shar; struct shared *addr; //---------------------------------------------------------------------- word_write(char arg[]) /* , , */ { int i, j, k, m; strcat(addr->mas, " "); for(i=0; imas, &arg[i], 1); for(j=0; j

  • 59

    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); // strcpy(addr->mas, "first"); /* "first" - */ sem_init(&addr->sema, 1, 1);// 1 ( ) close( fd ); // - shm_unlink( "/bolts" );/* . ,

    */ 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; nsema)&& errno==EINTR); word_write(thrd);

  • 60

    sem_post(&addr->sema); } printf("grandchild finishies\n"); } else { // , for(n=0; nsema)&& errno==EINTR); /*

    word_write(). sem_wait() , EINTR */

    word_write(sec); sem_post(&addr->sema);/* */ } wait(NULL); // - printf("child finishies\n"); } } else { , printf("parent with pid %d\n", getpid()); for(n=0; nsema)&& errno==EINTR); word_write(frst); sem_post(&addr->sema); } wait(NULL); // printf("parent: shm= %s\n", addr->mas);

  • 61

    sem_destroy(&addr->sema); // . printf("parent finishies\n"); } } . .

    pcahrielndt wwiitthh ppiidd 786471782374 grandchild with pid 786472 grandchild finishies child finishies parent: shm= first first second third first second third first second third first second third first second third first second third first second third first second third first second third first second third parent finishies

    . . pcahrielndt wwiitthh ppiidd 827431823334 grandchild with pid 827432 grandchild finishies child finishies parent: shm= first f sierc tsohtni fdri sdre tschtio frniddr t sshtei fcriodrn tsdht si fericdro tsnhtdi f sriedrc tsohtni fdri sdre tshctoi fnriddr t sshetci forindrd ts shtei fcriodrn tsdhti sercdond second parent finishies

  • 62

    2.2. - recorder1 recorder2 - sensor1 sensor2, - , . Re-corder1 recorder2 , - , . sensor_read[2]. - recorder1 recorder2 - , .. - . sen-sor_read collector. - () - 10 . , , logger logger.dat .

    , - :

    - sensor_read . recorder1, recorder2 collector ;

    - : - sensor_read (, record-er1), - - (recorder2); - sensor_read collector. , - -, .

  • 63

    - collector logger. . Collector , logger .

    #include #include #include #include #include #include pthread_mutex_t mtx1 = PTHREAD_MUTEX_INITIALIZER; // pthread_cond_t cv1 = PTHREAD_COND_INITIALIZER; // pthread_mutex_t mtx2 = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cv2 = PTHREAD_COND_INITIALIZER; pthread_barrier_t barrier; int sensor_1, sensor_2; int sensor_read[2]; unsigned int seed1, seed2, seed3, seed4;// . int is_data = 0; // 00 01 10 11 int data_logged = 1; // 0-data are not written to file, 1-data are written to file int mean1, variance1, mean2, variance2, delta1, delta2; /*

    */ //----------------------------------------------------------------------------- void *sensor1()// - . sensor_1

  • 64

    { printf("sensor1 starts\n"); while(1) { usleep(100000);

    sensor_1 = rand_r(&seed3) / 100; //printf("*%d ", sensor_1); } } //----------------------------------------------------------------------------- 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)

  • 65

    { pthread_mutex_lock (&mtx1); /* - is_data sensor_read */

    while (is_data ==1 || is_data == 3) pthread_cond_wait (&cv1,&mtx1); // is_data cv1 /* - is_data:

    0- sensor_read ; 1- sensor_read 1- ; 2- sensor_read 2- ; 3- sensor_read . . */ usleep(rand_r(&seed1)*100); // 1 sensor_read[0]=sensor_1; // 1 printf(" \nrecorder1 %d\n", sensor_read[0]); if (is_data ==0||is_data ==2) is_data += 1;// - pthread_cond_broadcast (&cv1);/* - cv1 , sensor_read is_data */ pthread_mutex_unlock(&mtx1); /* mtx1, is_data sensor_read */ } } //----------------------------------------------------------------------------- void *recorder2() // -

  • 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

  • 68

    pthread_cond_signal(&cv2); /* logger cv2, */ pthread_mutex_unlock (&mtx2); /* , logger . data_logged */ } } //----------------------------------------------------------------------------- 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; 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");

  • 69

    write(fd, buffer, strlen(buffer));// for(j = 0; j < num_logs; j++) /* .

    */ { pthread_mutex_lock (&mtx2);// while (data_logged) pthread_cond_wait (&cv2, &mtx2);/* cv2 , . */ time_of_day = time( NULL ); // ctime_r(&time_of_day, log_time); sprintf(buffer, "#%d\t%d\t%d\t%d\t%d\t%s", j, mean1, mean2,

    variance1, variance2, log_time); printf("Logger: %s\n", buffer); write(fd, buffer, strlen(buffer));// - data_logged = 1; strcpy( buffer, ""); pthread_cond_signal(&cv2); /* collector, */

    pthread_mutex_unlock (&mtx2);/* data_logged */

    } close(fd);// - stop_time = (struct timespec *) mmap(0, sizeof(struct timespec),

  • 70

    PROT_READ | PROT_WRITE, MAP_ANON, NOFD, 0);/* mal-loc()/free(), mmap()/munmap() */

    clock_gettime( CLOCK_REALTIME, stop_time); /* - , logger, */

    pthread_exit((void*) stop_time); /* , ( main), */

    } //----------------------------------------------------------------------------- main () {

    pthread_attr_t attr; pthread_t snsr1, snsr2, rcrd1, rcrd2, lggr; int fd; char read_line[100]; char buf; struct timespec *logger_end_time = NULL; long long sec; long nsec; uint64_t current_time; int num_logs = 3; printf("main starts\n"); setbuf(stdout, NULL); // seed1 = (unsigned int) time(NULL);//

  • 71

    seed3 = 1234; seed4 = 4321;

    pthread_barrier_init( &barrier, NULL, 2);/* 2 */ pthread_attr_init( &attr ); pthread_attr_setschedpolicy( &attr, SCHED_FIFO); /* FIFO - */ pthread_create (&snsr1, &attr, sensor1, NULL); pthread_create (&snsr2, &attr, sensor2, NULL); pthread_create (&rcrd1, NULL, recorder1, NULL); seed2 = (unsigned int) time(NULL); // f pthread_create (&rcrd2, NULL, recorder2, NULL); pthread_create (NULL, NULL, collector, NULL); pthread_create (&lggr, NULL, logger, (void*) &num_logs); pthread_join(lggr, (void**) &logger_end_time); /* - , */ pthread_cancel(snsr1); // pthread_cancel(snsr2); pthread_cancel(rcrd1); pthread_cancel(rcrd2);

    ClockTime_r( CLOCK_REALTIME, NULL, &current_time);// printf("Current system time=%lld ns\n", (long long) current_time);

  • 72

    printf("Data logging stopped at %lld sec %ld nsec system time\n", logger_end_time->tv_sec, logger_end_time->tv_nsec); munmap((void *) logger_end_time, sizeof(struct timespec));/*

    , */ setvbuf(stdout, NULL, _IOFBF, 0); // fd = open( "logger.dat", O_RDONLY | O_SYNC, S_IRUSR | S_IWUSR );/*

    logger.dat */ while (!eof(fd)) // logger.dat

    while(1) {

    read(fd, &buf, sizeof(buf)); if (buf == '\n') {printf("\n"); break;} else printf("%c", buf);

    } close(fd); //unlink(fd); // logger.dat

    } 2.6. (- , sensor()) , sens[5]. . - ( - re-

  • 73

    corder()) - (( - analyzer()). - array_of_max, - . - ( , - ). 15 . , , . - () .

    - , - -: - (..) - sens, - (.. ) , . #include #include #include #include #include #define el 5 // 5 #define repeat 15 // 15 unsigned sens[el]={0}; // pthread_rwlock_t rwl; pthread_barrier_t barrier_el, barrier_el2;

  • 74

    int file_descriptor, mapped_fd; static int *map_addr; fpos_t pos; pthread_t sens_thr[5], anlzr; //------------------------------------------------------------------------------------------------------ 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);

  • 75

    dup2(file_descriptor, fileno(stdout)); close(file_descriptor); clearerr(stdout); fsetpos(stdout, &pos); msync(map_addr, 1024, MS_SYNC);/*

    */ munmap(map_addr, 1024);// } //------------------------------------------------------------------------------------------------------ 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

    if(i==0) sleep(3); /* 3 . barrier_el 3 */ pthread_testcancel();// pthread_barrier_wait(&barrier_el); /* - , 5

    . tid 2, 3 . */

    pthread_rwlock_wrlock(&rwl); // - sens[i] = rand_r(&seed)/1000; // sens[]

    // RAND_MAX=32767u printf("%d ", sens[i]); pthread_rwlock_unlock(&rwl); /* - .

    - , */

    pthread_barrier_wait(&barrier_el2); /* - , , .. 7 5 -, 2 . , sens[] , .. - . , , */

    } } //------------------------------------------------------------------------------------------------------

  • 77

    void *recorder(void* arg) // , - ( 1) { int i, j, max; int repetitions; // unsigned num=0; // array_of_max[] int *array_of_max; /*

    */ printf("Recorder %d starts\n", pthread_self()); repetitions = *(int*) arg; array_of_max = (int*)calloc(el*repeat, sizeof(int)); for( j=0; j 0) { printf("\n#%d\tRec->", j);

    for(i=0; i

  • 78

    for( i = 0; i < num; i++ ) printf( "%d ", array_of_max[i]); printf("\n"); // -

    } pthread_rwlock_unlock(&rwl); // - pthread_barrier_wait(&barrier_el2);/* 5 , */ } 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

  • 79

    static unsigned int seed = 2630; static char __key[64]; static int count=0; char buffer[80] = { "Data registration starts at " }; char time_buffer[30]; time_t time_of_day; printf("Analyzer %d starts\n", pthread_self()); time_of_day = time(NULL); strftime(time_buffer, 60, "%a %b %d, %Y %R:%S",

    localtime( &time_of_day)); strcat(buffer, time_buffer); printf("%s\n", buffer); // // 64- for(i=0; i RAND_MAX/2) ? 1:0; setkey(__key ); while(1) { pthread_rwlock_rdlock(&rwl); /* - . */ if(count>0)// - , { printf(" **anal : encrypted array -> "); for(i=0; i

  • 80

    if(i

  • 81

    pthread_barrier_init(&barrier_el2, NULL, el+2); pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&attr, SCHED_FIFO); attr.flags = attr.flags | PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_ASYNCHRONOUS; /* FIFO */ for(i=0; i

  • 82

    4. 3

    ( /) . .4.1

    C QNX Neutrino POSIX

    shm_open() POSIX shm_unlink() POSIX mmap() munmap() ftruncate() ( ) shm_ctl() mq_open() POSIX mq_close() POSIX mq_unlink() POSIX mq_setattr() mq_send() mq_receive() mq_notify() ,

  • 83

    3.1 3.2. - . 5 - t p, (t1,p1), (t2,p2), (t5, p5). 4 , - POSIX. - . 3. 3 . POSIX, - - t p 3. // , POSIX #include #include //------------------------------------------------------------------------------------------------------------------------ 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;

  • 84

    sigset_t set; pid_t child_pid; int status; 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

  • 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 #include #include #include

  • 86

    #include 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;

  • 87

    printf("snsrs: sensor %d %d starts\n", getpid(), pthread_self()); seed = (unsigned int*) arg; 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) */

    } } //------------------------------------------------------------------------------------------------------------------------ main()

  • 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); pmq = mq_open("/my_pmq", O_RDWR, S_IRUSR | S_IWUSR , NULL);/* / */ if(pmq > 0)

    printf("snsrs: POSIX message queue with descriptor=%d opened\n", pmq); else return EXIT_FAILURE; sigemptyset( &set ); sigaddset(&set, SIGALRM);

  • 89

    pthread_sigmask(SIG_SETMASK, &set, NULL);/* SIGALRM, sigwait()*/ act.sa_flags = 0; act.sa_mask = set; act.sa_handler = &handler; sigaction( SIGUSR1, &act, NULL );/* SIGUSR1 */ /* 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); */ ualarm(4000000, 4000000);/* 4 , 4 */ printf("snsrs: interval timer launched\n"); for(i=0; i

  • 90

    }

    . .4.2 C QNX Neutrino

    sigaction() / sigemptyset() ( ) ( ) sigaddset() sigdelset() sigfillset()

    sigprocmask() / ( -) pthread_sigmask() / ( ) sigwait() sigwaitinfo() kill()

    sigqueue() (.. , - ) SignalKill() , , .. pthread_kill() 3.5. ( 3.1, ). -

  • 91

    . 5 t p. 4 . - -. , - - . // -, #include #include #include #include #include //------------------------------------------------------------------------------------------------------------------------ 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;

  • 92

    printf("PARENT with pid %d started\n", getpid()); // SIGRTMAX, : sigemptyset(&set); // set act_to_sensor.sa_flags = SA_SIGINFO; // act_to_sensor.sa_mask = set; /* ,

    - */ act_to_sensor.sa_handler = NULL; /* - .

    */ sigaction( SIGRTMAX, &act_to_sensor, NULL );//

    sigemptyset(&wait_set); // , sigwaitinfo() sigaddset(&wait_set, SIGRTMAX); // -

    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

  • 93

    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); }

    // 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 ); 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: }

  • 94

    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)); } } //------------------------------------------------------------------------------------------------------------------------ void handler(signo)/* - . . */ { } //========================================================================= // sig_sensors2.c, - #include #include #include #include

  • 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;

  • 96

    pid = getpid(); // pid ppid = getppid(); // pid tid = pthread_self(); // tid - printf("snsrs: sensor %d %d starts. PARENT pid is %d\n", pid, tid,

    ppid); seed = (unsigned int*) arg; sigfillset(&mask); // , - sigdelset(&mask, SIGALRM);// SIGALRM pthread_sigmask(SIG_SETMASK, &mask, NULL); sigemptyset(&set_for_sensor); // , - sigaddset(&set_for_sensor, SIGALRM); // SIGALRM 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; //

  • 97

    pair.p = rand_r(seed) / 1000.0; sprintf(snd_buf, "%d %d %d %.2f ", count, tid, pair.t, pair.p); printf("snsrs: %s\n", snd_buf); fflush(stdout); aux = (int) (pair.p*1000.0); /* , s_pair int SIGRTMAX*/ aux = aux - aux%10; s_pair =pair.t*1000000 + aux +tid; sigqueue ( ppid, SIGRTMAX, (union sigval) s_pair); /* SIGRTMAX - */ if (count < 4) /* - .

    - , SIGALRM, , */

    { kill(pid, SIGALRM); count++; } else { printf("\n"); count = 0; } } } //------------------------------------------------------------------------------------------------------------------------ main()

  • 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

    pthread_attr_init(&attr); // - pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&attr, SCHED_FIFO); sigfillset( &set ); /* SIGUSR1, */ sigdelset(&set, SIGUSR1);

    act.sa_flags = 0; act.sa_mask = set; /*

    - */ act.sa_handler = &signal_handler; //

    sigaction( SIGUSR1, &act, NULL );

  • 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

  • 100

    ChannelCreate() , QNX Neutrino ChannelDestroy() ConnectAttach() ConnectDetach() MsgSend() (. - MsgSend*())

    MsgReceive() (. - MsgReceive*())

    MsgInfo() , struct _msg_info MsgReply() (. - MsgReplyv())

    MsgError()

    MsgWrite() (. - MsgWritev())

    MsgRead() (. - MsgReadv()) MsgSendPulse () MsgReceivePulse() MsgDeliverEvent()

    3.6. ( API QNX Neutrino). -, , -

  • 101

    . - , -/ . 6 -. - , , . - , , (. 1).

    //message_ring_first.c - PARENT #include #include /* ND_LOCAL_NODE=0 QNET, */ #include #include

  • //--------------------------------------------------------------------------------------------------------------------- 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]; 102int status = 0; char reply[15] = "Hi, Son!"; pid_t child_id; int count=0; int i;

    struct _msg_info info; //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

  • 103

    itoa(chid, chid_s, 10); // itoa(count, count_s, 10); // - printf("PARENT's nd = %d chid = %d\n", nd, chid); chil_id = spawnl(P_NOWAIT, "message_ring-other.out", // -

    "message_ring-other.out", // pid_s, chid_s, pid_s, chid_s, count_s, NULL );

    delay(100); // //

    for(i=0; i

  • 104

    // message_ring_other.c // - ( message_ring-other.out) #include #include #include //--------------------------------------------------------------------------------------------------------------------- int main(int argc, const char *argv[]) // nd, chid {

    int nd = ND_LOCAL_NODE; // 0; pid_t other_pid; pid_t ppid; int chid; int coid; int rcvid; int status = 0; char son_msg[] = "PREVED, PARENT!";

    char rmsg[20]=""; char par_pid_s[8 * sizeof(int) + 1], chid_s[8 * sizeof(int) + 1], count_s[8 * sizeof(int) + 1];

    pid_t child_id; char reply[15] = "Hi, Son!"; int count = 0;

    struct _msg_info info; //argv[1] // pid //argv[2] /* .

  • 105

    */ ppid = atoi(argv[3]); // pid chid = atoi(argv[4]); // other_pid = getpid(); // pid count = atoi(argv[5]); // printf("\n\tDESCENDANT #%d with pid %d started.

    Parent's nd=%d ppid=%d chid=%d\n", count, other_pid, nd, ppid, chid); //

    ++count; if(count

  • 106

    rmsg); ConnectDetach(coid); exit(0); // }

    // 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); }

  • 107

    QNX Neutrino. RSA_Server () RSA, -. - RSA_Client () 5 QNX-Neutrino . - RSA_Receiver () . RSA-Receiver .

    , , RSA ( ):

    - p q. ( RSA_Server p q isprime()).

    - e, (p-1)*(q-1). e , .. (p-1)*(q-1) .

    - d, (e*d) % (p-1)(q-1) = =1. ( e d - RSA_Server uclid(), - ).

    - (, p*q) () ( encryption-), (d, p*q) () - (d - decryption-). C , - T, C = (T^e)%(p*q), T = (C^e)%(p*q) ( ^ , % - . (x^y)%n x_pow_y_mod_z1(), cl_ser5.h).

  • 108

    - (.. ) RSA- p q p*q, p*q - (1024 2048-).

    . RSA_Server5, RSA_Client5 RSA_Receiver5. // cl_ser5.h #include #include #include #include #include #include #include #define ATTACH_POINT "RSA-Server" // , ( /dev/name/local/) typedef struct _my_data { // //struct _pulse hdr; struct sigevent evnt; uint64_t data; } my_data_t; //------------------------------------------------------------------------------------------------------- uint64_t x_pow_y_mod_z1(uint64_t x, uint64_t y, uint64_t n) { // (x^y)%n RSA uint64_t s=1, t=x, u=y, i; while(u) {

  • 109

    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 #include "cl_ser5.h" #define BUFF_SIZE (256 + 1) //---------------------------------------------------------------------------------- uint64_t euclid( uint64_t m, uint64_t n, int64_t* uu, int64_t* vv) { // . (laws.pdf) int64_t u[3]={1,0,m}, v[3]={0,1,n}, t[3], q; int i; while(v[2] != 0) { q = u[2]/v[2]; for(i=0; i

  • 110

    //printf("*q=%lld u[0]=%lld u[1]=%lld u[2]=%lld v[0]=%lld v[1]=%lld v[2]=%lld\n", q,u[0],u[1],u[2],v[0],v[1],v[2]);

    } *uu = u[0]; *vv = u[1]; //printf("***%lld %lld %lld %lld %lld %lld\n", u[0], u[1], u[2],

    v[0], v[1], v[2]); return u[2]; // gcd } //---------------------------------------------------------------------------------- uint64_t isprime(uint64_t s) {// uint64_t m, n, i = 1; long long u, v; for(n=2ULL; n 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;

  • 111

    int scale = 80003; char buf[50], buff[BUFF_SIZE], pq[100]; // buf -for long p*q numbers uint64_t* ptr; struct sigevent r_event; /* ,

    MsgDeliverEvent */ my_data_t msg; int cycle_brk = 0; int receiver_rcvid; setbuf(stdout, NULL); if( confstr( _CS_RELEASE, buff, BUFF_SIZE ) > 0 ) // QNX

    printf( "SERVER: QNX Release: %s. Generating keys...\n", buff );

    /*------------------------- RSA----------------------------*/ srand((int) (time(NULL)*rand())); k1 = rand() / 1000; // k1 k2 do k2=rand() / 1000; while(k2

  • 112

    //printf("p=%lld q=%lld\n", p, q); aux = (p-1)*(q-1); do e = rand() / 8000; while (e

  • 113

    ptr = calloc(7, sizeof(uint64_t)); /* 5 ( d, p*q) */

    while (1) { rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), NULL); /* */ if (rcvid == -1) break; // MsgReceive() else if (msg.evnt.sigev_code == _PULSE_CODE_MINAVAIL) /* RSA_Client */ { plain_text = msg.data; // printf("SERVER: received message %u\n", plain_text); /*-------------------------------------------*/ // = (^e) mod (p*q) printf("\tENCRYPTION : (%lld^%lld)%%%lld ",

    plain_text, e, p*q); cipher = x_pow_y_mod_z1(plain_text, e, p*q);// printf("cipher=%lld\n", cipher); *ptr++ = cipher; //

    MsgError(rcvid, 0);// RSA_Client if(++cycle_brk >= 7) break;// , } else if(msg.evnt.sigev_code == _PULSE_CODE_MINAVAIL+1) /* RSA_Receiver */ {

  • 114

    printf("received message with pulse code _PULSE_CODE_MINAVAIL+1\n"); receiver_rcvid = rcvid; // RSA_Receiver r_event = msg.evnt; /* , RSA_Receiver */

    MsgError(receiver_rcvid, 0); /* RSA_ Receiver */

    if(++cycle_brk >= 7) break; /* , */ } else { // , printf("received other's message \n"); MsgError(rcvid, 0); if(++cycle_brk >= 7) break; /* ,

    */ } } *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 ,

    , */

  • 115

    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); } // 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" 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 */

  • 116

    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); } name_close(fd); } // 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;

  • 117

    struct _pulse my_pulse; my_data_t msg; int i; int chid, coid; uint64_t buf[7]; // 5 , (d, p*q)- int64_t d; uint64_t plain_text, pq; chid = ChannelCreate(0); /* , RSA_Server5 , */ coid = ConnectAttach( 0, 0, chid, _NTO_SIDE_CHANNEL, 0 );/* */ if ((fd = name_open(ATTACH_POINT, 0)) == -1) {// return EXIT_FAILURE; // } msg.evnt.sigev_notify = SIGEV_PULSE; /*

    , , RSA_Receiver5 */

    msg.evnt.sigev_coid = coid; //msg.evnt.sigev_priority = msg.evnt.sigev_code = _PULSE_CODE_MINAVAIL+1; msg.evnt.sigev_value.sival_int = MY_PULSE_VALUE; MsgSend( fd, &msg, sizeof(msg), NULL, 0 ); // send event to server //printf("*** _PULSE_CODE_MINAVAIL+1=%d\n", _PULSE_CODE_MINAVAIL+1);

  • 118

    do { MsgReceivePulse(chid, &my_pulse, sizeof(struct _pulse), NULL); /* , . MsgDeliverEvent() */ printf("* received pulse event from server with code %d\n", my_pulse.code); } while (my_pulse.code !=_PULSE_CODE_MINAVAIL+1); printf("*** received pulse event from server with code %d and value %d\n", my_pulse.code, my_pulse.value.sival_int); 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); }

  • 119

    name_close(fd); // , name_open() ConnectDetach(coid); // ChannelDestroy(chid); //

    } ( ). - C 64 , RSA . , , GNU GMP (http://www.swox.com/gmp/). UNIX- - bc . bc RSA- / - bc - (pipes) UNIX-. #include #include #include #include #include #define BUFF_SIZE (256 + 1) // ULONGLONG_MAX = 4294967295 sqrt(ULONGLONG_MAX) = 65535 USHRT_MAX 65535 //------------------------------------------------------------------------------------------------------- char* x_pow_y_mod_z3(uint64_t x, uint64_t y, char z[]) // t e p*q { // (t^e)%(p*q) QNX 6.3.* char *s_rezult[100]; FILE* f;

  • 120

    char sh_command[]="bc < a_pow_b_mod_n-bc";/* bc */

    // : "bc a_pow_b_mod_n-bc"; char bc_commands[]= // bc

    "s=\nwhile(y!=0){\n\tif(y%2) s=(s*x)%n\n\ty /= 2\n\tx=(x*x)%n\n}\ns\n"; remove("a_pow_b_mod_n-bc");// "a_pow_b_mod_n-bc"- bc

    ff = fopen("a_pow_b_mod_n-bc", "a");// fprintf(ff, "x=%lld\ny=%lld\nn=%s\n", x, y, z); fprintf(ff, "%s", bc_commands); fclose(ff); f = popen(sh_command, "r");// bc fgets(s_rezult[0], 100, f);// //puts(s_rezult); return *s_rezult; } //------------------------------------------------------------------------------------------------------- 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;

  • 121

    printf( "ULONG_MAX=%u, ULLONG_MAX=%u, ULONGLONG_MAX=%u\n",ULONG_MAX,

    ULLONG_MAX, ULONGLONG_MAX);/* ( limits.h) */

    printf("x=3480837*3482211=%lld(%%lld)\n", 348083*348221);

    printf("x=3480837*3482211=%u(%%u)\n", 348083*348221); /* , */

    confstr( _CS_RELEASE, buff, BUFF_SIZE ); // (release) QNX printf( "QNX Release: %s\n", buff ); if( ! memcmp( "6.2.", buff, 4)) /* 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",

  • 122

    cipher); // #gzip -df proba.gz pipe_fp2 = popen(gzip_command, "w"); pclose(pipe_fp2); } } 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

  • 123

    (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 # # ./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

  • 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. . , , ( - ):

    #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

  • 125

    (992802946^8)%1234749221 501686632

    : # bc (65534^101)%2345678 1113772 - (65535^101)%2345678 3000000000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000002197645 = 2197645 (65540^101)%2345678 3000000000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000000000000000000000000000000000000\ 0000000000000000000000000000000000000030000000000000000000000000000000\ 0000000000000000000000000000000293672 = 293672 , 2008 . 6.3.0 SP3+QNX Neutrino Core OS 6.3.2A

  • 126

    QNX, bc !

    5. 4 ( )

    . .5.1 C QNX Neutrino

    timespec - , timer_create() TimerCreate() () timer_settime() TimerSettime() timer_gettime() TimerInfo() , timer_delete() TimerDestroy()

    ualarm() - , SIGALRM

    alarm() TimerAlarm() , SIGALRM timer_timeout() TimerTimeout() clock_getres()

    ClockPeriod() ( )

    - ( ) clock_gettime() ClockTime() clock_settime()

    - ClockCycles() 64-

  • 127

    - SYSPAGE_ENTRY()

    timespec2nsec() - , timespec

    nsec2timespec() timespec sleep()

    -

    ( )

    delay() usleep()

    nanosleep() , timespec clock_nanosleep() , timespec 4.1. . main - , . , . , main, , . . TSC . - . time ( #time ./_) -.

  • 128

    #include #include #include #include #include #include #define MY_PULSE_CODE _PULSE_CODE_MINAVAIL+3 #define BILLION 1000000000L; // 1 int chid; // //--------------------------------------------------------------------------------------------------------------------------------- void * function( void* arg )// , { struct sigevent event; struct timespec timeout; int ret; struct _pulse pulse;

    struct timespec start_time, stop_time; double accum, timeout_in_sec; uint64_t tick_time;

    struct _clockperiod tick; clock_gettime( CLOCK_REALTIME, &start_time); /*

    */ printf("\tChild: this is thread %d\n", pthread_self());

  • 129

    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 ); 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);

  • 130

    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); } } while (1); } //-------------------------------------------------------------------------------------------------------------------------- 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;

  • 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;// , timer_create(CLOCK_REALTIME, &timer_event, &timer_id);// itime.it_value.tv_sec = 7; // 7.0 . itime.it_value.tv_nsec = 0; itime.it_interval.tv_sec = 0; itime.it_interval.tv_nsec = 0; timer_settime(timer_id, 0, &itime, NULL); // pthread_attr_init( &attr );

  • 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

    printf("Main time is %f s\n", sec);// main TSC return EXIT_SUCCESS; }

  • 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.

    1 1.1 1.2 1.3 1-30 1

    2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 1-30 2

    3 3.1 3.2 1-3 3

    3.3 3.4 12-14 3

    3.5 15-20 3

    3.6 21-30 3 4-11 3

    4 , 4.1. 1-30 4

    1. QNX Momentics 2. 1 ( ) 3. 2 ( ) 4. 3( /) 5. 4( )