使用共享内存的IPC
我正在编写一段简单的代码,其中父进程和子进程使用共享内存方法共享一个队列。但是,当子进程修改共享数据时,它不会以某种方式反映给父进程,并且父进程仍在访问以前的值。 我在其中附上示例代码我想要做什么:
int main()
{
/* Declare fork variables */
pid_t childpid;
/* Declare shared memory variables */
key_t key;
int shmid;
int *front;
int *rear;
int i;
/* Declare semaphore variables */
sem_t sem;
int pshared = 1;
unsigned int value = 1;
/* Initialize Shared Memory */
key = ftok("thread1.c",'R');
shmid = shmget(key, 1024, 0644 | IPC_CREAT);
/* Attach to Shared Memory */
rear = shmat(shmid, (void *)0, 0);
if(rear == (int *)(-1))
perror("shmat");
front = shmat(shmid, (void *)0, 0);
if(front == (int *)(-1))
perror("shmat");
/* Write initial value to shared memory */
rear= front;
/* Write to Shared Memory */
for(i=0; i<5; i++)
{
rear[i] = 5000+(i+1);
}
printf("value of front is: %d", *front);
rear = &rear[i-1];
/* Initialize Semaphore */
if((sem_init(&sem, pshared, value)) == 1)
{
perror("Error initializing semaphore");
exit(1);
}
if((childpid = fork()) < 0) // error occured
{
perror("Fork Failed");
exit(1);
}
else if(childpid == 0) // child process
{
printf("came in child process\n");
sem_wait(&sem);
for(i=0; i<3; i++){
printf("rear is: %d", *rear);
rear--;
}
sem_post(&sem);
shmdt(rear);
}
else // parent process
{
/* Write to Shared Memory */
wait(1);
sem_wait(&sem);
printf("came in parent process and rear is: %d\n", *rear);
sem_post(&sem);
shmdt(rear);
}
return 0;
}
提前致谢!
I am writing a simple code in which parent and child process are sharing one queue using shared memory method. But when child process modifies the shared data its somehow not reflecting for parent process and parent process is still accessing the previous value.
where I am attaching a sample code what I am trying to do:
int main()
{
/* Declare fork variables */
pid_t childpid;
/* Declare shared memory variables */
key_t key;
int shmid;
int *front;
int *rear;
int i;
/* Declare semaphore variables */
sem_t sem;
int pshared = 1;
unsigned int value = 1;
/* Initialize Shared Memory */
key = ftok("thread1.c",'R');
shmid = shmget(key, 1024, 0644 | IPC_CREAT);
/* Attach to Shared Memory */
rear = shmat(shmid, (void *)0, 0);
if(rear == (int *)(-1))
perror("shmat");
front = shmat(shmid, (void *)0, 0);
if(front == (int *)(-1))
perror("shmat");
/* Write initial value to shared memory */
rear= front;
/* Write to Shared Memory */
for(i=0; i<5; i++)
{
rear[i] = 5000+(i+1);
}
printf("value of front is: %d", *front);
rear = &rear[i-1];
/* Initialize Semaphore */
if((sem_init(&sem, pshared, value)) == 1)
{
perror("Error initializing semaphore");
exit(1);
}
if((childpid = fork()) < 0) // error occured
{
perror("Fork Failed");
exit(1);
}
else if(childpid == 0) // child process
{
printf("came in child process\n");
sem_wait(&sem);
for(i=0; i<3; i++){
printf("rear is: %d", *rear);
rear--;
}
sem_post(&sem);
shmdt(rear);
}
else // parent process
{
/* Write to Shared Memory */
wait(1);
sem_wait(&sem);
printf("came in parent process and rear is: %d\n", *rear);
sem_post(&sem);
shmdt(rear);
}
return 0;
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试创建 2 个具有相同共享内存 id 的独立变量。
尝试使用 2 个钥匙和 2 个共享内存。
You're trying to create 2 seperate variables with same shared memory id.
try using 2 keys and 2 shared memorys.