首页 > 试题广场 >

Most UNIX and Linux systems pr

[问答题]
Most UNIX and Linux systems provide the ipcs command.This command lists the status of various POSIX interprocess communication mechanisms,including shared-memory segments.Much of the information for thec ommand comes from the data structure struct shmid_ds,which is available in the /usr/include/sys/shm.h file.Some of the fields of this structure include:
•int shm_segsz—size of the shared-memory segment
•short shm_nattch—number of attaches to the shared-memory segment
•struct ipc_perm shm_perm—permission structure of the shared-memory segment
The struct ipc_perm data structure(which is available in the file /usr/include/sys/ipc.h)contains the fields:
•unsigned short uid—identifier of the user of the shared-memory segment
•unsigned short mode—permission modes
•key_t key (on Linux systems,__key)—user-specified key identifier
The permission modes are set according to how the shared-memory segment is established with the shmget( )system call.Permissions are identified according to the following:

Permissions can be accessed by using the bitwise AND operator &. For example,if the statement mode & 0400 evaluates to true,the permission mode allows read permission by the owner of the shared-memory segment.
Shared-memory segments can be identified according to a user-specified key or according to the integer value returned from the shmget( )system call,which represents the integer identifier of the shared-memory segment created.The shm_ds structure for a given integer segment identifier can be obtained with the following shmctl( ) system call:
/ * identifier of the shared memory segment * /
int segment_id;
shm_ds shmbuffer;
shmctl (segment_id, IPC_STAT, &shmbuffer);
If successful,shmctl( ) returns0;otherwise,it returns-1.
Write a C program that is passed an identifier for a shared-memory segment.This program will invoke the shmctl( ) function to obtain its shm-ds structure.It will then output the following values of the given shared-memory segment:
•Segment ID
•Key
•Mode
•Owner UID
•Size
•Number of attaches


这道题你会答吗?花几分钟告诉大家答案吧!