Unix的编程共享内存奇怪的结果 [英] Unix Programming Shared Memory strange results
问题描述
我一直在努力信号灯,现在分享了一个星期的内存,并且有一定的困难,所以我试图让这个程序,孩子们都应该写共享多维整数数组的存储和父亲是假设阅读是共享的内存阵列。
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&; SYS / ipc.h>
#包括LT&; SYS / shm.h>
#包括LT&;&stdio.h中GT;
#包括LT&; SYS / fcntl.h>
#包括LT&;&semaphore.h GT;
#包括LT&; SYS / wait.h>#定义MAXCHILDS 1
#定义MAX_SIZE 10
#定义MAX_WRITES 100typedef结构{
INT M [MAX_SIZE] [MAX_SIZE];
}矩阵;
/ *叉变量* /
将为pid_t孩子[MAXCHILDS]
/ * semphores变量* /
sem_t *空,*满,*互斥;
/ *共享内存ID * /
INT的shmid;
/ *共享内存数组指针* /
矩阵* sh_mem;
无效的init(){ / *信号灯取消链接和创建* /
sem_unlink(空);
空= sem_open(空,O_CREAT | O_EXCL,0700,50);
sem_unlink(FULL);
全= sem_open(FULL,O_CREAT | O_EXCL,0700,0);
sem_unlink(互斥);
互斥= sem_open(互斥,O_CREAT | O_EXCL,0700,1);
/ *初始化共享内存* /
的shmid = shmget的(IPC_PRIVATE,sizeof的(矩阵),IPC_CREAT | 0777);
/ *地图共享内存* /
sh_mem =(基质*)的shmat(的shmid,NULL,0);
如果(sh_mem ==(矩阵*)( - 1)){
PERROR(的shmat);
}
}无效的作家(INT米[MAX_SIZE] [MAX_SIZE],INT n_child){
INT I,K;
对于(i = 0; I< MAX_SIZE;我++){
对于(K = 0; K< MAX_SIZE; k ++){
M [] [K] = 0;
如果(ⅰ==第(k + 1)){
M [] [K] = 1;
}
}
}}
无效读卡器(INT米[MAX_SIZE] [MAX_SIZE]){
INT I = 0;
INT K = 0;
INT总和= 0;
对于(i = 0; I< MAX_SIZE;我++){
对于(K = 0; K< MAX_SIZE; k ++){
的printf(%D,米[k]的[I]);
}
综上所述++;
的printf([I =]%D [K =]%d个\\ N,I,K);
}
的printf(%D,总和);}终止无效(){
sem_close(空);
sem_close(已满);
sem_close(互斥);
sem_unlink(空);
sem_unlink(FULL);
sem_unlink(互斥);
了shmctl(的shmid,IPC_RMID,NULL);
}INT主(INT ARGC,字符** argv的)
{
INT I,总和;
在里面(); 对于(i = 0; I< MAXCHILDS;我++){
如果((的孩子[我] =叉())小于0)//出错
{
PERROR(叉失败);
出口(1);
}
如果((的孩子[我] =叉())== 0){
作家(sh_mem-> M,I);
出口(0);
}
} /*父亲*/
睡眠(10);
综上所述++;
的printf(%d个\\ N,总和);
阅读器(sh_mem->米);
等待(NULL); 终止();
返回0;
}
我有两个问题,现在...我需要将数组保存到我不似乎明白了一个内存映射文件和父亲的输出被扔是很奇怪......他的输出
0000100000 [I =] 3 [K =] 10
0000010000 [I =] 4 [K =] 10
0000001000 [I =] 5 [K =] 10
0100000000 [I =] 0 [K =] 10
0000000100 [I =] 6 [K =] 10
00.1亿[I =] 1 [K =] 10
0000000010 [I =] 7 [K =] 10
0001000000 [I =] 2 [K =] 10
0000000001 [I =] 8 [K =] 10
0000100000 [I =] 3 [K =] 10
0000000000 [I =] 9 [K =] 10
0000010000 [I =] 4 [K =] 10
0000001000 [I =] 5 [K =] 10
0000000100 [I =] 6 [K =] 10
0000000010 [I =] 7 [K =] 10
0000000001 [I =] 8 [K =] 10
0000000000 [I =] 9 [K =] 10
和它应该是这样的:
0000100000 [I =] 0 [K =] 10
0000010000 [I =] 1 [K =] 10
0000001000 [I =] 2 [K =] 10
0100000000 [I =] 3 [K =] 10
0000000100 [I =] 4 [K =] 10
00.1亿[I =] 5 [K =] 10
0000000010 [I =] 6 [K =] 10
0001000000 [I =] 7 [K =] 10
0000000001 [I =] 8 [K =] 10
0000100000 [I =] 9 [K =] 10
在code调用叉()
在两个地方。卸下第二个电话。
修改
如果((的孩子[我] =叉())== 0){
到
如果(孩子[我] == 0){
I have been working on semaphores and shared memory for a week now, and have some difficulties yet,so i tried to make this program which the children are supposed to write to a memory shared multidimensional integer array and the father is suppose to read that array from the memory that is shared.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <sys/fcntl.h>
#include <semaphore.h>
#include <sys/wait.h>
#define MAXCHILDS 1
#define MAX_SIZE 10
#define MAX_WRITES 100
typedef struct{
int m[MAX_SIZE][MAX_SIZE];
}matrix;
/*fork variables*/
pid_t child[MAXCHILDS];
/*semphores variables */
sem_t *empty, *full, * mutex;
/*share memory id*/
int shmid;
/*shared memory array pointer */
matrix * sh_mem;
void init(){
/*semaphores unlink and creation */
sem_unlink("EMPTY");
empty=sem_open("EMPTY",O_CREAT|O_EXCL,0700,50);
sem_unlink("FULL");
full=sem_open("FULL",O_CREAT|O_EXCL,0700,0);
sem_unlink("MUTEX");
mutex=sem_open("MUTEX",O_CREAT|O_EXCL,0700,1);
/*initialize shared memory */
shmid = shmget(IPC_PRIVATE,sizeof(matrix),IPC_CREAT|0777);
/*map shared memory*/
sh_mem = (matrix*)shmat(shmid,NULL,0);
if(sh_mem == (matrix*)(-1)){
perror("shmat");
}
}
void writer(int m[MAX_SIZE][MAX_SIZE],int n_child){
int i,k;
for(i = 0;i<MAX_SIZE;i++){
for(k= 0;k<MAX_SIZE;k++){
m[i][k] = 0;
if(i==(k+1)){
m[i][k] = 1;
}
}
}
}
void reader(int m[MAX_SIZE][MAX_SIZE]){
int i = 0;
int k = 0;
int sum = 0;
for(i = 0;i<MAX_SIZE;i++){
for(k= 0;k<MAX_SIZE;k++){
printf("%d",m[k][i]);
}
sum++;
printf("[i=]%d[k=]%d\n",i,k);
}
printf("%d",sum);
}
void terminate() {
sem_close(empty);
sem_close(full);
sem_close(mutex);
sem_unlink("EMPTY");
sem_unlink("FULL");
sem_unlink("MUTEX");
shmctl(shmid, IPC_RMID, NULL);
}
int main(int argc, char **argv)
{
int i,sum;
init();
for(i = 0;i<MAXCHILDS;i++){
if((child[i]= fork()) < 0) // error occured
{
perror("Fork Failed");
exit(1);
}
if((child[i] =fork())==0){
writer(sh_mem->m,i);
exit(0);
}
}
/*father*/
sleep(10);
sum++;
printf("%d\n",sum);
reader(sh_mem->m);
wait(NULL);
terminate();
return 0;
}
I have two problems right now ... I need to save the array to a memory mapped file which i dont seem to understand and the output of the father is throwing is very strange ...His output is
0000100000[i=]3[k=]10
0000010000[i=]4[k=]10
0000001000[i=]5[k=]10
0100000000[i=]0[k=]10
0000000100[i=]6[k=]10
0010000000[i=]1[k=]10
0000000010[i=]7[k=]10
0001000000[i=]2[k=]10
0000000001[i=]8[k=]10
0000100000[i=]3[k=]10
0000000000[i=]9[k=]10
0000010000[i=]4[k=]10
0000001000[i=]5[k=]10
0000000100[i=]6[k=]10
0000000010[i=]7[k=]10
0000000001[i=]8[k=]10
0000000000[i=]9[k=]10
And it should be something like this:
0000100000[i=]0[k=]10
0000010000[i=]1[k=]10
0000001000[i=]2[k=]10
0100000000[i=]3[k=]10
0000000100[i=]4[k=]10
0010000000[i=]5[k=]10
0000000010[i=]6[k=]10
0001000000[i=]7[k=]10
0000000001[i=]8[k=]10
0000100000[i=]9[k=]10
The code calls fork()
in two places. Remove the second call.
Change:
if((child[i] =fork())==0){
to:
if(child[i] == 0){
这篇关于Unix的编程共享内存奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!