使用系统调用SHMGET(),SHMAT()编制一长度为1K的消息发送和接收的程序。 要求: (1)main函数建立一个KEY 为75 的共享区,并将第一个字节置为0。作为数据空的标志等待其他进程发来的消息。收到数据后显示“ Main recieved help ”。 (3)创建一个进程P1,P1建立一个KEY为75的共享区,当共享取得第一个字节为0时,main空闲,可发送请求:P1填入“HELP”,发送数据后显示“P1 send help”。
时间: 2024-02-25 14:52:41 浏览: 32
好的,我已经理解了你的问题。下面是一份使用SHMGET()和SHMAT()系统调用编写的长度为1K的消息发送和接收的程序:
```
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <string.h>
#include <unistd.h>
#define SHM_SIZE 1024
#define SHM_KEY 75
int main(int argc, char *argv[]) {
int shmid, *data;
char *message;
// Create shared memory segment
if ((shmid = shmget(SHM_KEY, SHM_SIZE, IPC_CREAT | 0666)) < 0) {
perror("shmget");
exit(1);
}
// Attach shared memory segment to process address space
if ((data = shmat(shmid, NULL, 0)) == (int *) -1) {
perror("shmat");
exit(1);
}
// Set first byte to 0 to indicate that data area is empty
*data = 0;
while (1) {
// Wait for data to arrive
while (*data == 0) {
sleep(1);
}
// Display received message
message = (char *) data + sizeof(int);
printf("Main received %s\n", message);
// Reset data area to indicate empty
*data = 0;
}
// Detach shared memory segment from process address space
if (shmdt(data) == -1) {
perror("shmdt");
exit(1);
}
// Remove shared memory segment
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
perror("shmctl");
exit(1);
}
return 0;
}
```
在这个程序中,我们创建一个名为“SHM_KEY”的共享区,大小为1K。我们使用一个整数指针来指向共享区,第一个字节表示数据区是否为空。如果第一个字节为0,则数据区为空,等待其他进程发送消息。如果第一个字节为1,则数据区有数据,主进程将显示接收到的消息,并将第一个字节重置为0,以指示数据区为空。
现在,我们需要创建进程P1来填写“HELP”并发送数据。下面是P1的代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <string.h>
#include <unistd.h>
#define SHM_SIZE 1024
#define SHM_KEY 75
int main(int argc, char *argv[]) {
int shmid, *data;
char *message;
// Create shared memory segment
if ((shmid = shmget(SHM_KEY, SHM_SIZE, 0666)) < 0) {
perror("shmget");
exit(1);
}
// Attach shared memory segment to process address space
if ((data = shmat(shmid, NULL, 0)) == (int *) -1) {
perror("shmat");
exit(1);
}
// Wait for data area to become available
while (*data != 0) {
sleep(1);
}
// Fill data area with message
message = (char *) data + sizeof(int);
strcpy(message, "HELP");
// Set first byte to 1 to indicate data area is full
*data = 1;
printf("P1 send help\n");
// Detach shared memory segment from process address space
if (shmdt(data) == -1) {
perror("shmdt");
exit(1);
}
return 0;
}
```
在这个程序中,我们打开之前创建的共享区,等待主进程将第一个字节设置为0,表示数据区为空。此时,我们填充数据区并将第一个字节设置为1,以指示数据区不为空。然后,我们打印一条消息,表示我们已经发送了帮助请求。
这就是使用SHMGET()和SHMAT()系统调用编写的长度为1K的消息发送和接收的程序。希望这可以回答你的问题。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)