操作系统概念第七版答案(含编程代码)(6)

2019-01-07 14:17

Operating system concepts(Seventh edition) 2008.3

/* set the size of the sequence */

shared_memory->sequence_size = seq_size;

/* now fork a child process */

if ( (pid = fork()) == (pid_t)-1) { return 1; }

/**

* now create a child process and have the child process set * the the shared memory segment to a certain value.

* The parent process will inquire on this shared value when * it returns from wait(). Thus, the call to wait() provides the synchronization. */

if (pid == 0) { /** child code */

printf(\shared_memory);

/* now have the child generate the Fibonacci sequence .... */ shared_memory->fib_sequence[0] = 0; shared_memory->fib_sequence[1] = 1;

for (i = 2; i < shared_memory->sequence_size; i++)

shared_memory->fib_sequence[i] = shared_memory->fib_sequence[i-1] + shared_memory->fib_sequence[i-2];

/* now detach the shared memory segment */ shmdt((void *)shared_memory); }

else { /* parent code */ wait(NULL);

for (i = 0; i < shared_memory->sequence_size; i++)

printf(\

/* now detach and remove the shared memory segment */ shmdt((void *)shared_memory); shmctl(segment_id, IPC_RMID, NULL); }

return 0; }

Operating system concepts(Seventh edition) 2008.3

3.11 Most UNIX and Linux systems provide the ipcs command. This command lists the status of various POSIX interprocess communicationmechanisms,including

shared-memory segments. Much of the information for the command 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: Mode Meaning 0400 Read permission of owner. 0200 Write permission of owner. 0040 Read permission of group. 0020 Write permission of group. 0004 Read permission of world. 0002 Write permission of world. 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 fromthe 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() returns 0; 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

Operating system concepts(Seventh edition) 2008.3

Answer:

// This program illustrates the functionality of the ipcs command on POSIX systems.

// This program is written for Linux 2.4 systems.

// Getting it to work on Solaris, OS X, etc. will require modifying the source code where commented. // Usage: gcc -o sm sm.c // ./sm

#include #include #include

int main(int argc, char *argv[]) {

/* the segment number */ int segment_id;

/* permissions of the segment */ unsigned short mode;

/** the shared memory segment */ struct shmid_ds shmbuffer;

/** do some error checking */ if (argc < 2) {

fprintf(stderr,\ return -1; }

/**

* this needs to be set to the * shared memory segment number * being attached to. */

segment_id = atoi(argv[1]);

/* get the shm_ds information */

if (shmctl(segment_id, IPC_STAT, &shmbuffer) == -1) {

fprintf(stderr,\ return -1; }

Operating system concepts(Seventh edition) 2008.3

/** now report the fields in shm_ds */

printf(\\\t\\t KEY \\t MODE \\t\\t OWNER \\t SIZE \\t ATTTACHES \\n\ printf(\\\t\\t --- \\t ---- \\t\\t ----- \\t ---- \\t --------- \\n\

/** Linux has __key rather than key field */

printf(\

/** Mac OS X Darwin uses the key field */

//printf(\

/** report on the permission */ mode = shmbuffer.shm_perm.mode;

/** report on the permission */ mode = shmbuffer.shm_perm.mode;

/** OWNER */

if (mode & 0400) printf(\ else

printf(\ if (mode & 0200) printf(\ else

printf(\ if (mode & 0100) printf(\ else

printf(\

/** GROUP */

if (mode & 0040)

printf(\ else

printf(\ if (mode & 0020) printf(\ else

printf(\ if (mode & 0010) printf(\ else

printf(\

Operating system concepts(Seventh edition) 2008.3

/** WORLD */

if (mode & 0004)

printf(\ else

printf(\ if (mode & 0002) printf(\ else

printf(\ if (mode & 0001) printf(\ else

printf(\

/** Darwin (Mac OS X) has user_from_uid() function */

//printf(\ printf(\ printf(\ printf(\

//printf(\

printf(\

return 0; }

Chapter 4

4.1 Provide two programming examples in which multithreading does not provide better


操作系统概念第七版答案(含编程代码)(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:信息碎片化降低人们认知水平

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: