| Home / lib / S_Software manuals / | ||
Stevens R. Unix network programming vol.2 (PH, 1999)(T)(C)(564s).djvu |
|
Size 6.0Mb Date Jan 24, 2005 |
that the semaphore's sem_otime value is initialized to 0 by semget and will be set
nonzero by the creator's call to semop...
Chapter 10 Exercises 279
Exercises
10.1 Modify the produce and consume functions in Section 10.6 as follows...
This was similar to a
mutex lock (Chapter 7), in which the semaphore value is 0 if the resource is
locked, or 1 if the resource is available...
When a new semaphore set is created, the following members of the semid_ds
structure are initialized:
• The uid and cuid members of the sem_perm structure are set to the effective
user ID of the process, and the gid and cgid members are set to the effective
group ID of the process...
It might contain
other members, and we have no guarantee that the members are in the order that we show...
• When a thread is put to sleep waiting for a semaphore operation to complete
(we will see that the thread can be waiting either for the semaphore value to be 0
or for the value to be greater than 0), and the thread catches a signal, and the sig-
signal handler returns, the semop function is interrupted and returns an error of
EINTR...
Also, the sleep returns prematurely with an error if a
caught signal interrupts the function or if the semaphore is removed...
semrmid Program
The next program, shown in Figure 11.3, removes a semaphore set from the system...
Unlike the
previous two programs, this program allows the user to specify fewer operations than
members of the semaphore set...
Since the last
operation cannot be performed, and since we specified nonblocking, an error of EAGAIN
is returned...
System V Semaphores Chapter 11
11.8 Summary
The following changes occur when moving from Posix semaphores to System V
semaphores:
1...
If we execute this program, we see that the memory pointed to by ptr is indeed
shared between the parent and child...
Shared Memory Introduction
Chapter 12
We now modify our program from Figure 12.10 to use a Posix memory-based
semaphore instead of a Posix named semaphore, and store this semaphore in the shared
memory...
BSD provides anonymous memory mapping, which completely avoids having
to create or open a file...
Shared Memory Introduction Chapter 12
Command-line arguments
e-ii The command-line arguments specify the pathname of the file that will be created
and memory mapped, the size to which that file is set, and the size of the memory map-
mapping...
The kernel lets us read
and write that portion of the final page beyond our mapping (since the kernel's memory
protection works with pages), but anything that we write to this extension is not written
to the file...
When we run this program, we see that as we increase the size of the file, we are
able to reference the new data through our established memory map...
• mlock causes a specified range of addresses of the process to be memory resi-
resident, where the function arguments are a starting address and a number of bytes
from that address, munlock unlocks a specified region of memory...
Use our
msgcreate and msgsnd programs from Section 6.6 to create the message queue, and then
place records onto the queue...
• For a regular file: If the size of the file was larger than length, the extra data is
discarded...
pxshm/test3.c
1 #include "unpipc.h"
2 int
3 main(int argc, char **argv)
4 {
5 int fdl, fd2, *ptrl, *ptr2;
6 pid_t childpid;
7 struct stat stat;
8 if (argc != 2)
9 err_guit("usage: test3 <name>");
10 shm_unlink(Px_ipc_name(argv[1]));
11 fdl = Shm_open(Px_ipc_name(argv[l] ) , O_RDWR | O_CREAT | O_EXCL, FILE_MODE);
12 Ftruncate(fdl, sizeof(int));
13 fd2 = Open("/etc/motd", O_RDONLY);
14 Fstat(fd2, &stat);
15 if ( (childpid = ForkO) == 0) {
16 /* child */
17 ptr2 = Mmap (NULL, stat .st_size, PROT_READ, MAP_SHARED, fd2, 0) ;
18 ptrl = Mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE,
19 MAP_SHARED, fdl, 0) ;
20 printf("child: shm ptr = %p, motd ptr = %p\n", ptrl, ptr2);
21 sleepE);
22 printf("shared memory integer = %d\n", *ptrl);
23 exit@);
24 }
25 /* parent: mmap in reverse order from child */
26 ptrl = MmaplNULL, sizeof (int), PROT_READ | PROT_WRITE, MAP_SHARED, fdl, 0);
27 ptr2 = Mmap(NULL, stat .st_size, PROT_READ, MAP_SHARED, f d2, 0);
28 printf("parent: shm ptr = %p, motd ptr = %p\n", ptrl, ptr2);
29 *ptrl = 777;
30 Waitpid(childpid, NULL, 0);
31 exit@);
32 }
pxshm/test3.c
Figure 13.6 Shared memory can appear at different addresses in different processes...
| © 2007 eKnigu | ||
