【发布时间】:2021-07-18 00:24:35
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libio.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/shm.h>
#include <ctype.h>
int main(int argc, char** argv) {
int shmID;
char* shmptr, *array, *filearray;
FILE* infile = fopen(argv[1], "r");
if (!infile) {
printf("No file exists\n");
}
int length = 0;
char ch;
ch = getc(infile);
while ((ch = getc(infile)) != EOF) {
filearray[length] = ch;
length++;
}
length++;
fclose(infile);
shmID = shmget(IPC_PRIVATE, length, IPC_CREAT | 0666);
if (shmID < 0) {
printf("There is an error while creating memory \n");
}
int pid = fork();
if (pid > 0) {
shmptr = shmat(shmID, NULL, 0);
if (shmptr == (char * ) - 1) {
printf("There is an error while attaching memory \n");
}
int j = 0;
while ( * array != '!') {
if (array[j] >= 'A' && array[j] <= 'Z') {
array[j] = tolower(array[j]);
} else if (array[j] >= 'a' && array[j] <= 'z') {
array[j] = toupper(array[j]);
} else if (array[j] >= '0' && array[j] <= '9') {
j--;
}
j++;
}
* shmptr = '@';
shmdt(shmptr);
shmctl(shmID, IPC_RMID, NULL);
} else if (pid == 0) {
shmptr = shmat(shmID, NULL, 0);
if (shmptr == (char * ) - 1) {
printf("There is an error while attaching memory \n");
}
array = shmptr;
int x;
for (x = 0; x <= length; x++) {
* array = filearray[x];
array++;
}
* array = '!';
while ( * shmptr != '@') {
sleep(1);
}
int k = 0;
//here we restore the values back into file
while (*array != '!') {
printf("%c", array[k]);
k++;
}
shmdt(shmptr);
} else if (pid < 0) {
printf("Error\n");
}
return 0;
}
这是代码。我打算做的是从文件中获取数据并将其输入到数组中。我们这样做是为了有一个临时数据点来存储。然后我应用适当的检查来查看在创建共享内存或 fork 命令时是否有错误。 在此之后,我们进入我们打算进入的孩子
- 附加内存
- 检查 1 的错误。
- 获取指向 shmptr 的数组(尝试使用另一个 char 数组指针,但仍然遇到问题),该数组最初应为 NULL,但长度为 L(文件中的字符数)
- 将文件数组中的值复制到数组中(充当类似于链接列表中的移动头),然后作为最后一个块附加!告诉父母数组结束了。
- 使用 @ 作为要添加到数组的字符,这样我们就可以知道等待时间已经结束。
在他的父母中:
- 附加内存
- 获取数组
- 大写、小写并检查值是否为整数(通过 j 删除整数--返回 1 个位置,然后 j++ 移回相同位置)
- 在末尾附加一个@。
- 当孩子重新运行时,它看到了这个 @ 并且应该打印数组
希望我很清楚。谢谢你的帮助。
【问题讨论】:
-
缺少发生错误的示例输入文件。
-
我冒昧地为您修好了牙套和压痕。下次,请确保这是您做的第一件事,而不是最后一件事。没有正确大括号和缩进的代码是无法阅读的,因此您在排除故障时遇到困难也就不足为奇了。
标签: c linux operating-system ipc shared-memory