当前位置: 技术问答>linux和unix
两个程序共享内存的问题
来源: 互联网 发布时间:2016-07-10
本文导语: 以下程序,两个程序通过共享内存通信,set.c是通过终端输入一个数字,然后通知get.c,get.c在终端显示出来,现在的问 题是,set后get.c无法显示数字,如果换成字符串则可以,不知道什么问题,高手帮忙看看! /* se...
以下程序,两个程序通过共享内存通信,set.c是通过终端输入一个数字,然后通知get.c,get.c在终端显示出来,现在的问
题是,set后get.c无法显示数字,如果换成字符串则可以,不知道什么问题,高手帮忙看看!
/* set.c*/
#include
#include
#include
#include
#include
#include
#include
#include "shm_com.h"
int main()
{
int running = 1;
void *shared_memory = (void *)0;
struct shared_use_at *shared_stuff;
char buffer[BUFSIZ];
int shmid;
int command;
shmid = shmget((key_t)1234, sizeof(struct shared_use_at), 0666 | IPC_CREAT);
if(shmid == -1)
{
fprintf(stderr, "shmget failedn");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid, (void *)0, 0);
if(shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failedn");
exit(EXIT_FAILURE);
}
printf("Memory attached at %Xn", (int)shared_memory);
shared_stuff = (struct shared_use_at *)shared_memory;
while(running)
{
while(shared_stuff->written_by_you == 1)
{
sleep(1);
printf("waiting for client...n");
}
printf("Enter a number: ");
scanf("%dn", &command);
shared_stuff->power = command;
/* fgets(buffer, BUFSIZ, stdin);
strncpy(shared_stuff->some_text, buffer, TEXT_SZ);*/
shared_stuff->written_by_you = 1;
if(command == 10)
{
running = 0;
}
}
if(shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failedn");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
/***************** get.c **************************/
#include
#include
#include
#include
#include
#include
#include
#include "shm_com.h"
int main()
{
int running = 1;
void *shared_memory = (void *)0;
struct shared_use_at *shared_stuff;
int shmid;
srand((unsigned int) getpid());
shmid = shmget((key_t)1234, sizeof(struct shared_use_at), 0666|IPC_CREAT);
if(shmid == -1)
{
fprintf(stderr, "shmget failedn");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid, (void *)0, 0);
if(shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failedn");
exit(EXIT_FAILURE);
}
printf("Memory attached at %Xn", (int)shared_memory);
shared_stuff = (struct shared_use_at *)shared_memory;
shared_stuff->written_by_you = 0;
while(running)
{
if(shared_stuff->written_by_you)
{
printf("You wrote: %s", shared_stuff->power);
sleep(rand() % 4);
shared_stuff->written_by_you = 0;
if(shared_stuff->power==0)
{
running = 0;
}
}
}
if(shmdt(shared_memory)==-1)
{
fprintf(stderr, "shmdt failedn");
exit(EXIT_FAILURE);
}
if(shmctl(shmid, IPC_RMID, 0)==-1)
{
fprintf(stderr, "shmctl(IPC_RMID) failedn");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
题是,set后get.c无法显示数字,如果换成字符串则可以,不知道什么问题,高手帮忙看看!
/* set.c*/
#include
#include
#include
#include
#include
#include
#include
#include "shm_com.h"
int main()
{
int running = 1;
void *shared_memory = (void *)0;
struct shared_use_at *shared_stuff;
char buffer[BUFSIZ];
int shmid;
int command;
shmid = shmget((key_t)1234, sizeof(struct shared_use_at), 0666 | IPC_CREAT);
if(shmid == -1)
{
fprintf(stderr, "shmget failedn");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid, (void *)0, 0);
if(shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failedn");
exit(EXIT_FAILURE);
}
printf("Memory attached at %Xn", (int)shared_memory);
shared_stuff = (struct shared_use_at *)shared_memory;
while(running)
{
while(shared_stuff->written_by_you == 1)
{
sleep(1);
printf("waiting for client...n");
}
printf("Enter a number: ");
scanf("%dn", &command);
shared_stuff->power = command;
/* fgets(buffer, BUFSIZ, stdin);
strncpy(shared_stuff->some_text, buffer, TEXT_SZ);*/
shared_stuff->written_by_you = 1;
if(command == 10)
{
running = 0;
}
}
if(shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failedn");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
/***************** get.c **************************/
#include
#include
#include
#include
#include
#include
#include
#include "shm_com.h"
int main()
{
int running = 1;
void *shared_memory = (void *)0;
struct shared_use_at *shared_stuff;
int shmid;
srand((unsigned int) getpid());
shmid = shmget((key_t)1234, sizeof(struct shared_use_at), 0666|IPC_CREAT);
if(shmid == -1)
{
fprintf(stderr, "shmget failedn");
exit(EXIT_FAILURE);
}
shared_memory = shmat(shmid, (void *)0, 0);
if(shared_memory == (void *)-1)
{
fprintf(stderr, "shmat failedn");
exit(EXIT_FAILURE);
}
printf("Memory attached at %Xn", (int)shared_memory);
shared_stuff = (struct shared_use_at *)shared_memory;
shared_stuff->written_by_you = 0;
while(running)
{
if(shared_stuff->written_by_you)
{
printf("You wrote: %s", shared_stuff->power);
sleep(rand() % 4);
shared_stuff->written_by_you = 0;
if(shared_stuff->power==0)
{
running = 0;
}
}
}
if(shmdt(shared_memory)==-1)
{
fprintf(stderr, "shmdt failedn");
exit(EXIT_FAILURE);
}
if(shmctl(shmid, IPC_RMID, 0)==-1)
{
fprintf(stderr, "shmctl(IPC_RMID) failedn");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
|
printf("You wrote: %s", shared_stuff->power);
power是整数,为什么用%s ?
power是整数,为什么用%s ?