IPC阵列程序
我正在尝试使用共享内存概念制作2个程序。一个程序接受整数数组,另一个程序对数组进行排序。然而,我不能正确地做这件事,也不能认为我的程序有什么问题。请检查一下,告诉我我做错了什么 谢谢 接受数组的程序IPC阵列程序,c,arrays,ipc,shared-memory,C,Arrays,Ipc,Shared Memory,我正在尝试使用共享内存概念制作2个程序。一个程序接受整数数组,另一个程序对数组进行排序。然而,我不能正确地做这件事,也不能认为我的程序有什么问题。请检查一下,告诉我我做错了什么 谢谢 接受数组的程序 #include<unistd.h> #include<sys/shm.h> #include<stdio.h> #include<stdlib.h> struct shared_use_st { int n; int wby ;
#include<unistd.h>
#include<sys/shm.h>
#include<stdio.h>
#include<stdlib.h>
struct shared_use_st
{
int n;
int wby ;
int array[100];
};
int main()
{
int shmid,running,i ;
running = 1;
void * shared_memory = (void*)0;
struct shared_use_st * shared_stuff ;
shmid = shmget((key_t)12345,sizeof(struct shared_use_st) , 0666|(IPC_CREAT));
shared_memory = shmat(shmid, (void *)0, 0);
if (shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failed\n");
exit(0);
}
printf("Memory attached at %X\n", (int)shared_memory);
shared_stuff = (struct shared_use_st *)shared_memory;
printf("\nEnter the no of array : ");
scanf("%d",&shared_stuff->n);
while(running) // infinite loop
{
while(shared_stuff->wby==1)
{
printf("waiting for producer 2");
sleep(4);
}
for(i=0;i<shared_stuff->n;i++)
{
printf("\nEnter the array : ");
scanf("%d",&(shared_stuff->array[i]));
}
shared_stuff->wby = 1 ;
} // while
return 0;
} // main
#包括
#包括
#包括
#包括
结构共享\u使用\u st
{
int n;
int wby;
整数数组[100];
};
int main()
{
int shmid,running,i;
运行=1;
void*共享内存=(void*)0;
结构共享\u使用\u st*共享\u内容;
shmid=shmget((键)12345,sizeof(结构共享使用),0666(IPC创建));
共享内存=shmat(shmid,(void*)0,0;
如果(共享内存==(无效*)-1)
{
fprintf(stderr,“shmat失败\n”);
出口(0);
}
printf(“连接在%X\n的内存,(int)共享_内存”);
共享的东西=(结构共享的使用)共享的内存;
printf(“\n输入数组的编号:”);
scanf(“%d”,&shared\u stuff->n);
(运行时)//无限循环
{
while(shared_stuff->wby==1)
{
printf(“等待制作人2”);
睡眠(4);
}
对于(i=0;in;i++)
{
printf(“\n输入数组:”);
scanf(“%d”和(共享的内容->数组[i]);
}
共享内容->wby=1;
}//而
返回0;
}//主要
对数组进行排序的程序-
#include<unistd.h>
#include<sys/shm.h>
#include<stdio.h>
#include<stdlib.h>
struct shared_use_st
{
int n;
int wby ;
int array[100];
};
int main()
{
int shmid,i,j,running = 1,temp ;
void * shared_memory = (void*)0;
struct shared_use_st * shared_stuff ;
shmid = shmget((key_t)12345,sizeof(struct shared_use_st) , 0666|IPC_CREAT) ;
shared_memory = shmat(shmid, (void *)0, 0);
if (shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failed\n");
exit(0);
}
printf("Memory attached at %X\n", (int)shared_memory);
shared_stuff = (struct shared_use_st *)shared_memory;
while(running) // infinite loop
{
if(shared_stuff->wby==1)
{
printf("\n sortinggaaa the array in ascending order");
for(i=0;i<shared_stuff->n;i++)
{
for(j=0;j<((shared_stuff->n)-1);j++)
{
if(shared_stuff->array[j]>shared_stuff->array[j+1])
{
temp = shared_stuff->array[j];
shared_stuff->array[j] = shared_stuff->array[j+1] ;
shared_stuff->array[j+1] = temp ;
}
}
}
sleep(5);
printf("Array is now sorted");
shared_stuff->wby = 0;
} //if
} //while
return 0;
} //main
#包括
#包括
#包括
#包括
结构共享\u使用\u st
{
int n;
int wby;
整数数组[100];
};
int main()
{
int shmid,i,j,运行=1,温度;
void*共享内存=(void*)0;
结构共享\u使用\u st*共享\u内容;
shmid=shmget((键)12345,sizeof(结构共享使用),0666(IPC创建);
共享内存=shmat(shmid,(void*)0,0;
如果(共享内存==(无效*)-1)
{
fprintf(stderr,“shmat失败\n”);
出口(0);
}
printf(“连接在%X\n的内存,(int)共享_内存”);
共享的东西=(结构共享的使用)共享的内存;
(运行时)//无限循环
{
如果(共享的内容->wby==1)
{
printf(“\n按升序排序数组”);
对于(i=0;in;i++)
{
对于(j=0;jn)-1);j++)
{
如果(共享内容->数组[j]>共享内容->数组[j+1])
{
temp=shared_stuff->array[j];
共享内容->数组[j]=共享内容->数组[j+1];
共享的东西->数组[j+1]=temp;
}
}
}
睡眠(5);
printf(“数组现在已排序”);
共享内容->wby=0;
}//如果
}//而
返回0;
}//主要
您到底不能做什么?你不能管理他们?你有错误吗?等等?什么是制作人?谁初始化共享内存,什么先开始-很多不清楚的事情,而不是使用轮询标志和睡眠,这是丑陋的,我确信有问题,使工作正常(如果没有说“错”),最好看看标准的生产者消费者模板与信号量,并从这一点开始