Вы находитесь на странице: 1из 30

6.

POSIX:

1)
2)
3)
4)

;
;
;
.
;
5) .
;
6) ,
;
7) .
POSIX)

POSIX ()/

.../

;/

.
;/

.
/

15

26

,
)

30

POSIX
.
- 0 32767.
, Linux
.

SEMMSL.
semget.
, semget,
. , ;
,
, .

semctl,
,
. ,
,
semctl.
, ,
semop :

;
;
.

,
. ,
semop .
, , ;
, .
0; ,
.
. SEM_UNDO ,
, . . ,
.
IPC_NOWAIT semop
, ,
(
). ''
''. , ,
,
`` ''.
semop ,
, `` ''. ,
.
. , , , , -,
,
, .
,
. ,
. ,
.

, , -1
, errno .

POSIX ()
-
, -
.
. ,
, post wait.
wait .
0,
, wait, .D
0, wait , -
( , ) . post
.
, post .
,
wait , post
. , , post wait
.
,
.
,
. (. 3). 3.
- sem_t p, q; int data; void *producer
(void *) {
while(1) { int t=produce();
sem_wait(&p); data=t; sem_post(&q);
} return NULL;
} void * consumer(void *) {
}
while (1) { int t;
sem_wait(&q); t=data; sem_post(&p); consume(1);
} return NULL;
.
,
,
wait,
post.
- ,
, .
3 p.

, - POSIX Real
Time Extension, POSIX. Solaris
- librt.so.D
lrt.D
POSIX Thread API,

pthread_. ,
, POSIX
, .
sem_t <semaphore.h>.
...
- ,
. Linux ,
( , ,
System V ). , ,
.
, .
, ,
, .

-
,
,
.

. ,
,
,
; .

,
;
.


.D

sem_t,
.
sem_init(3RT).
:
sem_t * sem int pshared 0
,

unsigned int value


.
sem_destroy(3RT).
sem_post(3RT), sem_wait(3RT), sem_trywait(3RT),
sem_timedwait(3RT) sem_getvalue(3RT). sem_getvalue(3RT),
,
. , ,
sem_getvalue(3RT)
. , sem_getvalue(3RT)
, , sem_wait(3RT)
.D sem_getvalue
. POSIX,
, sem_getvalue(3RT)
, .
Solaris 10 .
sem_open(3RT).

. :
const char * name . /
/. , 14
. ,

( Solaris Linux ). ,
.
int flags . 0, O_CREAT O_CREAT|O_EXCL,
O_CREAT O_EXCL , <sys/fcntl.h>.
open
(2).D 0 , O_CREAT
,
, O_EXCL , ,
.
mode_t mode , , flags
O_CREAT.D ,
, .
, .

, ,
.
unsigned int value ,
flags O_CREAT.
.
sem_open(3RT) (sem_t *).
errno.
,
.
.
, Unix-
. .

-
sem_close(3RT).D
exit(2) exec(2).D

. ,
sem_unlink(3RT).
( sem_open
O_CREAT ) .
sem_unlink(3RT)
, ,
sem_close(3RT).D sem_unlink(3RT)
.
.
, , .
API ,
, . ,
API ,
API .


().
sem_open() O_CREAT,
sem_init().
(
, fork()).
sem_t.

, .
(, )
.

, ,
, .
, ,
, , sem_open() sem_init() (. 4.11),
, .
#include <semaphore.h>

sem_t *sem_open (const char *name, int oflag, ...);


int sem_init (sem_t *sem, int pshared, unsigned value);

4.11. sem_open() sem_init(). (html, txt)


;
.
;
, sem_unlink(3RT).
( sem_open
O_CREAT )
. sem_unlink(3RT)
, ,
sem_close(3RT). sem_unlink(3RT)
.
. ,
, .


semget semctl , shmget shmctl
. semget , ,
, , shmget; .
, ; ,
.
, ,
. , ,
. , semctl ,
, IPC_RMID , union semun
( ).

( ).
, Linux
.
5.2 .
.(sem_al.c)
O
O
O
O
O
O
O
O
O

#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/types.h>
/* union semun . */
union semun {
O
int val;
O
struct semid_ds *buf;
O
unsigned short int *array;
O
struct seminfo *__buf;

O
};
O
/* ,
. */
O
int binary_semaphore_allocation (key_t key, int sem_flags)
O
{
O
O
return semget (key, 1, sem_flags);
O
}
O
* .

O
O
. -1 . */
O
int binary_semaphore_deallocate (int semid)
O
{
O
O
union semun ignored_argument;
O
O
return semctl (semid, 1, IPC_RMID, ignored_argument);
O
}


. semop .
. - struct
sembuf , , . -
.

struct sembuf :
sem_num - , .
sem_op - , . sem_op , .
sem_op - ,
. ,
, ,
sem_op (
). sem_op , ,
.

sem_flg - . IPC_NOWAIT,
; , semop
. SEM_UNDO , Linux
, .
5.4 .
(sem_w.c)
O
#include <sys/types.h>
O
#include <sys/ipc.h>
O
#include <sys/sem.h>
O
/* . ,
, 1. */
O
int binary_semaphore_wait (int semid)
O
{
O
O
struct sembuf operations[1];
O
O
/* ( ) . */
O
O
operations[0].sem_num = 0;
O
O
/* 1. */
O
O
operations[0].sem_op = -1;
O
O
/* */
O
O
operations[0].sem_flg = sem_undo;
O
O
return semop (semid, operations, 1);

O
}
O
/* : 1.
. */
O
int binary_semaphore_post (int semid)
O
{
O
O
struct sembuf operations[1];
O
O
/* . */
O
O
operations[0].sem_num = 0;
O
O
/* increment by 1. */
O
O
/* 1. */
O
O
operations[0].sem_op = 1;
O
O
/* . */
O
O
operations[0].sem_flg = sem_undo;
O
O
return semop (semid, operations, 1);
O
}
SEM_UNDO
. ,
, , ""
. , , ,
, .

ipcs -s ,
.
ipcrm sem , .
, 5790517,
:
% ipcrm sem 5790517

=)
( name sem_open())
,
. ( oflag) :
O_CREAT / O_EXCL. O_CREAT,
sem_open()
: ( mode_t) (
unsigned int).
sem_open()
SEM_FAILED,
sem_t, sem_init() " " -1.

, sem_init()
POSIX-2001 ; ,
. sem_t
sem. value
. pshared
, ;

.
,
sem_close(), sem_unlink();
sem_destroy
() (. 4.12).
, -1.

#include <semaphore.h>
int sem_close (sem_t *sem);
int sem_unlink (const char *name);
int sem_destroy (sem_t *sem);

4.12. . (html,
txt)

, sem_close()
, sem_destroy()
, .
sem_destroy() ,
.
sem_wait(), sem_trywait()
sem_timedwait() (. 4.13 4.14).

#include <semaphore.h>
int sem_wait (sem_t *sem);
int sem_trywait (sem_t *sem);

4.13. . (html, txt)

#include <semaphore.h>
#include <time.h>
int sem_timedwait (sem_t *restrict sem,
const struct timespec *restrict abstime);

4.14.
. (html, txt)

,
-
,
. sem_trywait()
, sem_wait()
, sem_timedwait() ,
.
sem_post()
(. 4.15). (
) ,
- ,
.

#include <semaphore.h>
int sem_post (sem_t *sem);

4.15. sem_post(). (html, txt)

sem_getvalue() (. 4.16)
, .

#include <semaphore.h>
int sem_getvalue (sem_t *restrict sem, int *restrict sval);
4.16. sem_getvalue(). (html, txt)

sval.
, .
,
[1], ,

. ,
POSIX-2001, .
, , .
- , ,
.

, /
(. 4.17).

/* * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
*/
/* / (/).
*/
/*
*/
/* ,
. */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
#include <errno.h>
/* */
static int my_buf;
/* , */
static sem_t w_sem;
/* , */
static sem_t r_sem;
/* */
static int sum = 0;
/* * * * * * * * * * * * * * * * * * * * * * * */
/* , */
/* * * * * * * * * * * * * * * * * * * * * * * */
void *start_writer (void *dummy) {

while (sem_wait (&w_sem) == 0) {


my_buf = rand ();
if (sem_post (&r_sem) != 0) {
perror ("SEM_POST-R");
return (NULL);
}
}
return (NULL);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * */
/* ,
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * */
void *start_reader (void *dummy) {
while (sem_wait (&r_sem) == 0) {
sum += my_buf;
if (sem_post (&w_sem) != 0) {
perror ("SEM_POST-W");
return (NULL);
}
}
return (NULL);
}
/* * * * * * * * * * * * * * * * * *
/* ,
/*
/* * * * * * * * * * * * * * * * * *
int main (void) {
pthread_t w_ptid;/*
pthread_t r_ptid;/*
/*
/*
/*
if

* * * * * */
*/
*/
* * * * * */
- */
- */

.
*/
,*/
-
*/
(sem_init (&w_sem, 0, 1) == -1) {
perror ("SEM_INIT-W");
return (-1);

}
if (sem_init (&r_sem, 0, 0) == -1) {
perror ("SEM_INIT-R");
return (-1);

}
/* - */
if ((errno = pthread_create (&w_ptid, NULL,
start_writer, NULL)) != 0) {
perror ("PTHREAD_CREATE-W");
return (errno);
}
if ((errno = pthread_create (&r_ptid, NULL,
start_reader, NULL)) != 0) {
perror ("PTHREAD_CREATE-R");
return (errno);
}
/* */
sleep (10);
/* */
(void) pthread_cancel (w_ptid);
(void) pthread_cancel (r_ptid);
/* */
(void) pthread_join (w_ptid, NULL);
(void) pthread_join (r_ptid, NULL);
/* */
if (sem_destroy (&w_sem) != 0) {
perror ("SEM_DESTROY-W");
return (-1);
}
if (sem_destroy (&r_sem) != 0) {
perror ("SEM_DESTROY-R");
return (-1);
}
printf (" : %d\n", sum);
return 0;
}
4.17. , /
. (html, txt)


.
, ,

.
.
,
, ,
POSIX-2001, .
, ,
POSIX-2001,
.
sem_trywait(). -, sem_trywait
() ,
, . -,

,
sem_post().

, ,

. ,
sem_post()
, , , .
, ,
. , ,
.
, -
, ...

,
,
, , ,
(. 4.18).
/*
/*
/*
/*
/*

* * * * * * * * * * * * * * * * * * * * * *

/ (/).

,

*/
*/
*/
*/
*/

/*
/*
/*
/*

.*/

*/

*/
* * * * * * * * * * * * * * * * * * * * * * */

#include
#include
#include
#include
#include
#include

<unistd.h>
<stdlib.h>
<stdio.h>
<semaphore.h>
<pthread.h>
<errno.h>

/* */
static int my_buf [BUFSIZ];
/* , */
static int w_ind = 0;
/* , */
static int r_ind = 0;
/* , */
static sem_t w_sem;
/* , */
static sem_t r_sem;
/* */
static int sum = 0;

/* * * * * * * * * * * * * * * * * * * * * * * */
/* , */
/* * * * * * * * * * * * * * * * * * * * * * * */
void *start_writer (void *dummy) {
while (sem_wait (&w_sem) == 0) {
my_buf [w_ind] = rand ();
w_ind = (w_ind + 1) % BUFSIZ;
if (sem_post (&r_sem) != 0) {
perror ("SEM_POST-R");
return (NULL);
}
}

return (NULL);
}
/* * * * * * * * * * * * * * * */
/* ,
*/
/* */
/* * * * * * * * * * * * * * * */
void *start_reader (void *dummy) {
while (sem_wait (&r_sem) == 0) {
sum += my_buf [r_ind];
r_ind = (r_ind + 1) % BUFSIZ;
if (sem_post (&w_sem) != 0) {
perror ("SEM_POST-W");
return (NULL);
}
}
return (NULL);
}
/* * * * * * * * * * * * * * * * * *
/* ,
/*
/* * * * * * * * * * * * * * * * * *
int main (void) {
pthread_t w_ptid;/*
pthread_t r_ptid;/*

/*
/*
/*
/*
if

* * * * * */
*/
*/
* * * * * */
- */
- */

.
*/
,*/
,*/
-
*/
(sem_init (&w_sem, 0, BUFSIZ) == -1) {
perror ("SEM_INIT-W");
return (-1);

}
if (sem_init (&r_sem, 0, 0) == -1) {
perror ("SEM_INIT-R");
return (-1);
}
/* - */
if ((errno = pthread_create (&w_ptid, NULL,

start_writer, NULL)) != 0) {
perror ("PTHREAD_CREATE-W");
return (errno);
}
if ((errno = pthread_create (&r_ptid, NULL,
start_reader, NULL)) != 0) {
perror ("PTHREAD_CREATE-R");
return (errno);
}
/* */
sleep (10);
/* */
(void) pthread_cancel (w_ptid);
(void) pthread_cancel (r_ptid);
/* */
(void) pthread_join (w_ptid, NULL);
(void) pthread_join (r_ptid, NULL);
/* */
if (sem_destroy (&w_sem) != 0) {
perror ("SEM_DESTROY-W");
return (-1);
}
if (sem_destroy (&r_sem) != 0) {
perror ("SEM_DESTROY-R");
return (-1);
}
printf (" : %d\n", sum);
return 0;
}
4.18. , /
. (html, txt)

w_sem ,
, r_sem - .
, .
.
sem_wait() sem_post() ""
"" .

sem_wait(), ""
, ,
sem_post() , .
,
. , ..
(. 4.19).
,
.
/*
.
. :
mudrecSem [-a | -p | -I] [-t _]
_ ...
:
-t _ -
:
-a - ;
-p - ;
-I - ( )
:
.
:
mudrecSem -p -t 300
A B C D E F G H I J K L M N\
O P Q R S T U V W X Y Z
*/
static char rcsid[] __attribute__((unused)) = \
"$Id: mudrecSem.c,v 1.2 2004/03/18 10:28:38 sambor Exp $";
#include
#include
#include
#include
#include
#include
#include
#include

<unistd.h>
<stdlib.h>
<stdio.h>
<pthread.h>
<semaphore.h>
<signal.h>
<string.h>
<fcntl.h>

#include <limits.h>
#include <errno.h>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)>(b)?(b):(a))
struct mudrec {
char *name;
int left_fork, right_fork;
int eat_time, wait_time, think_time, max_wait_time;
int count;
pthread_t thread;
int private_pFdIn;
} *kafedra;
/* */
int Stop = 0; /* */
/* */
int protokol [2] = {-1, -1};
#define pFdIn (protokol [1])
#define pFdOut (protokol [0])
/* */
sem_t *semFork;
/* */
static void get_forks_simple (struct mudrec *this);
static void get_forks_odd (struct mudrec *this);
static void get_forks_maybe_infinit_time (struct mudrec
*this);
/* */
void (*get_forks) (struct mudrec *this) = get_forks_simple;
/* */
static void put_forks (struct mudrec *this);
/*
* -
*/
void *filosof (void *arg) {
struct mudrec *this = arg;
char buffer [LINE_MAX];

int bytes;
int private_pFdIn = this->private_pFdIn;
while (!Stop) {
/* */
{
int wait_time, tm = time (NULL);
sprintf (buffer, "%s: \n", this->name);
bytes = write (private_pFdIn, buffer,
strlen (buffer));
(*get_forks) (this);
wait_time = time (NULL) - tm;
this->wait_time += wait_time;
this->max_wait_time = max (wait_time,
this->max_wait_time);
sprintf (buffer,"%s: %d \n",
this->name, wait_time);
bytes = write (private_pFdIn, buffer,
strlen (buffer));
}
/* , ? */
if (Stop) {
put_forks (this);
break;
}
/* */
{
int eat_time = rand () % 20 + 1;
sleep (eat_time);
this->eat_time += eat_time;
this->count++;
sprintf (buffer,"%s: %d \n", this->name,
eat_time);
bytes = write (private_pFdIn, buffer,
strlen (buffer));
}

/* */
put_forks (this);
if (Stop) break;
/* */
{
int think_time = rand () % 10 + 1;
sleep (think_time);
this->think_time += think_time;
}
} /* while (!Stop) */
sprintf (buffer,"%s: \n", this->name);
bytes = write (private_pFdIn, buffer, strlen (buffer));
close (private_pFdIn);
return (NULL);
} /* - */
/* */
static void put_forks (struct mudrec *this) {
sem_post (&semFork [this->left_fork - 1]);
sem_post (&semFork [this->right_fork - 1]);
}
/* */
static void get_forks_simple (struct mudrec *this) {
int first = min (this->left_fork, this->right_fork);
int last = max (this->left_fork, this->right_fork);
sem_wait (&semFork [first - 1]);
sem_wait (&semFork [last - 1]);
}
/* !*/
/* ( - ) */
static void get_forks_odd (struct mudrec *this) {
int left = this->left_fork, right = this->right_fork;
int first;
int last;

if ((left & 1) > (right & 1)) {


first = left;
last = right;
} else if ((left & 1) < (right & 1)) {
first = right;
last = left;
} else {
first = max (left, right);
last = min (left, right);
}
sem_wait (&semFork [first - 1]);
sem_wait (&semFork [last - 1]);
}
/* , .
* , .
* .
*/
static void get_forks_maybe_infinit_time
(struct mudrec *this) {
int left = this->left_fork, right = this->right_fork;
for (;;) {
sem_wait
if (0 ==
sem_post
sem_wait
if (0 ==
sem_post
}

(&semFork [left - 1]);


sem_trywait (&semFork [right - 1])) return;
(&semFork [left - 1]);
(&semFork [right - 1]);
sem_trywait (&semFork [left - 1])) return;
(&semFork [right - 1]);

}
/* */
static void stop (int dummy) {
Stop = 1;
}
static void usage (char name []) {
fprintf (stderr,
": %s [-a | -p | -I] [-t _] "
"_ ...\n", name);
exit (1);
}

/* */
int main (int argc, char *argv []) {
char buffer [LINE_MAX], *p;
int i, n, c;
int open_room_time = 300;
int nMudr;
struct sigaction sact;
while ((c = getopt (argc, argv, "apIt:")) != -1) {
switch (c) {
case 'a': get_forks = get_forks_simple; break;
case 'p': get_forks = get_forks_odd; break;
case 'I': get_forks = get_forks_maybe_infinit_time;
break;
case 't': open_room_time = strtol (optarg, &p, 0);
if (optarg [0] == 0 || *p != 0) usage (argv [0]);
break;
default : usage (argv [0]);
}
}
nMudr = argc - optind;
if (nMudr < 2) usage (argv [0]); /* !
/* ... */

*/

/* */
pipe (protokol);
kafedra = calloc (sizeof (struct mudrec), nMudr);
/* */
for (i = 0; i < nMudr; i++, optind++) {
kafedra [i].name = argv [optind];
/* */
kafedra [i].private_pFdIn = fcntl (pFdIn, F_DUPFD, 0);
/* , */
kafedra [i].left_fork = i + 1;
kafedra [i].right_fork = i + 2;
}
kafedra [nMudr - 1].right_fork = 1; /* */
/* */
/* */

/* */
sact.sa_handler = stop;
(void) sigemptyset (&sact.sa_mask);
sact.sa_flags = 0;
(void) sigaction (SIGINT, &sact, (
struct sigaction *) NULL);
(void) sigaction (SIGALRM, &sact,
(struct sigaction *) NULL);
alarm (open_room_time);
/* */
semFork = calloc (sizeof (sem_t), nMudr);
for (i = 0; i < nMudr; i++) {
sem_init (&semFork [i], 0, 1 /* */
/* */);
}
/* */
for (i = 0; i < nMudr; i++)
pthread_create (&kafedra [i].thread, NULL, &filosof,
(void *) &kafedra [i]);
/* */
/* */
close (pFdIn);
while (1) {
n = read (pFdOut, buffer, LINE_MAX);
if (n == 0 || (n < 0 && errno != EINTR)) break;
for (i = 0; i < n; i++) putchar (buffer [i]);
}
close (pFdOut);
/* */
for (i = 0; i < nMudr; i++) {
sem_destroy (&semFork [i]);
}
/* */
{
int full_eating_time = 0;
int full_waiting_time = 0;
int full_thinking_time = 0;
for (i = 1; i <= nMudr; i++) {
struct mudrec *this = &kafedra [i - 1];

full_eating_time += this->eat_time;
full_waiting_time += this->wait_time;
full_thinking_time += this->think_time;
if (this->count > 0) {
float count = this->count;
float think_time = this->think_time / count;
float eat_time = this->eat_time / count;
float wait_time = this->wait_time / count;
printf ("%s: %d : =%.1f "
"=%.1f =%.1f ( %d)\n",
this->name, this->count, think_time,
eat_time, wait_time, this->max_wait_time);
} else
printf ("%s: \n", this->name);
} /* for */
{
float total_time = (full_eating_time +
full_waiting_time + full_thinking_time) /
(float) nMudr;
printf(" = %.3f\n",
full_eating_time / total_time);
printf(" = %.3f\n",
full_waiting_time / total_time);
}
} /* */
free (semFork);
free (kafedra);
/* . */
printf (" \n");
return 0;
}

/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*

* * * * * * * * * * * * * * * * * * * * * * * * * */
. !*/
, !
*/
! , !
*/
.! !
*/
, !
*/
, !*/
, ! */
!*/
. !
*/
! */
, ! */
... ! */
* * * * * * * * * * * * * * * * * * * * * * * * * */

#include
#include
#include
#include
#include

<stdio.h>
<pthread.h>
<sched.h>
<semaphore.h>
<assert.h>

static sem_t sem_mi; ! /*


!
*/
!
!
!
!
!
!
!
!
/* ! */
static sem_t sem_hi; ! /* !*/
!
!
!
!
!
!
!
!
/* ! */
static double s = 0;
/* * * * * * * * * * * * * * * * * * * * * * * * * * */
/* !*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * */
void *start_hi (void *dummy) {
!
printf (" "
!
!
!
!
"\n");
!
assert (sem_wait (&sem_hi) == 0);
!
printf (" "
!
!
!
!
"\n");
!
assert (sem_post (&sem_hi) == 0);
!
}

return (NULL);

/* * * * * * * * * *
/*
/* * * * * * * * * *
void *start_mi (void
!
int i;
!
double d = 1;

* * * * * * * * * * * * * * * */
*/
* * * * * * * * * * * * * * * */
*dummy) {

!
!
!
!
!
!
!
!
!
!
!

printf (" "


!
!
!
"\n");
assert (sem_wait (&sem_mi) == 0);
/* */
for (i = 1; i < 100000000; i++) {
!
s += d/i;
!
d = -d;
}
printf (" "
!
!
!
" \n");
assert (sem_post (&sem_mi) == 0);

!
}

return (&s);

/* * * * * * * * * * * * * * * * * * * * * * * * * * */
/* ! */
/* * * * * * * * * * * * * * * * * * * * * * * * * * */
int main (void) {
!
pthread_attr_t attrob; !
/* ! */
!
!
!
!
!
!
!
!
!
!
!
/*
! */
!
struct sched_param shdprm; !/* !
*/
!
!
!
!
!
!
!
!
!
!
!
/*
!*/
!
int shdplc; ! !
!
!
!
!
/*
!*/
!
pthread_t pt_mi; ! !
!
!
/*
!
*/
!
!
!
!
!
!
!
!
!
!
!
/*
! */
!
pthread_t pt_hi; ! !
!
!
/*
!
*/
!
!
!
!
!
!
!
!
!
!
!
/*
! */
!

/* */

!
!
!

!
!

assert (sem_init (&sem_mi, 0, 0) == 0);


assert (sem_init (&sem_hi, 0, 0) == 0);

!
!
!
!
!
!

/* !*/
/* !
*/
shdprm.sched_priority = sched_get_priority_max
!
(SCHED_FIFO) 31;
assert (pthread_setschedparam (pthread_self (),
!
SCHED_FIFO, &shdprm) == 0);

!
!
!

/* !*/
/* ! */
assert (pthread_attr_init (&attrob) == 0);

!
!
!
!
!
!
!
!

/* */
assert (pthread_attr_setinheritsched (&attrob,
!
PTHREAD_EXPLICIT_SCHED) == 0);
assert (pthread_attr_setschedpolicy (&attrob,
!
SCHED_FIFO) == 0);
shdprm.sched_priority += 15;
assert (pthread_attr_setschedparam
!
(&attrob, &shdprm) == 0);

!
!
!

/* */
assert (pthread_create (&pt_mi, &attrob,
!
start_mi, NULL) == 0);

!
!
!
!
!
!
!

/* */
/* */
shdprm.sched_priority += 15;
assert (pthread_attr_setschedparam
!
(&attrob, &shdprm) == 0);
assert (pthread_create (&pt_hi, &attrob,
!
start_hi, NULL) == 0);

!
!
!
!
!
!
!
!

/* */
assert (pthread_getschedparam (pthread_self (),
!
&shdplc, &shdprm) == 0);
assert (shdplc == SCHED_FIFO);
printf (" : %d\n",
!
!
!
shdprm.sched_priority);
assert (pthread_getschedparam
!
(pt_mi, &shdplc, &shdprm) == 0);

!
assert (shdplc == SCHED_FIFO);
!
printf (&quot; : %d\n",
!
!
shdprm.sched_priority);
!
assert (pthread_getschedparam
!
!
(pt_hi, &shdplc, &shdprm) == 0);
!
assert (shdplc == SCHED_FIFO);
!
printf (" : %d\n",
shdprm.sched_priority);
!
!
!
!
!

/* */
printf (" \n"
!
!
!
" "
!
!
!
" \n");
assert (sem_post (&sem_mi) == 0);

!
!
!
!

printf (" \n"


!
!
!
" "
!
!
!
" \n");
assert (sem_post (&sem_hi) == 0);

!
!

(void) pthread_join (pt_mi, NULL);


(void) pthread_join (pt_hi, NULL);

!
!

assert (sem_destroy (&sem_mi) == 0);


assert (sem_destroy (&sem_hi) == 0);

!
}

return 0;

#include <semaphore.h>
int sem_post (sem_t *sem);
4.15. sem_post(). sem_getvalue())
, .

#include <semaphore.h>
int sem_getvalue (sem_t *restrict sem, int *restrict sval);
4.16. sem_getvalue().
sval. ,
.