编写成一个代码
时间: 2024-02-25 19:52:43 浏览: 16
好的,下面是完整的代码实现:
```
#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;
// Fork a child process
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(1);
}
if (pid == 0) {
// Child process (P1)
// 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);
}
exit(0);
} else {
// Parent process (Main)
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);
}
exit(0);
}
return 0;
}
```
这个程序创建一个名为“SHM_KEY”的共享区,大小为1K。然后,它使用一个整数指针来指向共享区,第一个字节表示数据区是否为空。如果第一个字节为0,则数据区为空,等待其他进程发送消息。如果第一个字节为1,则数据区有数据,主进程将显示接收到的消息,并将第一个字节重置为0,以指示数据区为空。
接下来,我们fork一个子进程,子进程填充数据区并发送数据,父进程等待接收数据并显示它。当我们使用Ctrl+C终止程序时,它将删除共享区。
希望这可以帮助你。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](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)
![](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)