77范文网 - 专业文章范例文档资料分享平台

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

来源:网络收集 时间:2019-01-07 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库操作系统概念第七版答案(含编程代码)(6)在线全文阅读。

操作系统概念第七版答案(含编程代码)(6).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/409896.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: