当前位置: 技术问答>linux和unix
读共享内存的问题
来源: 互联网 发布时间:2016-09-14
本文导语: 两个程序,一个向共享内存写数据(时间日期数据),一个读数据,当写数据的时候,读数据的程序会挂掉,发生段错误,以下是代码: 结构体 typedef struct daytime { char *year; char *month; char *day; char *hour; char *min; ...
两个程序,一个向共享内存写数据(时间日期数据),一个读数据,当写数据的时候,读数据的程序会挂掉,发生段错误,以下是代码:
结构体
typedef struct daytime
{
char *year;
char *month;
char *day;
char *hour;
char *min;
char *sec;
int flag;
}dt;
//写进程
int write(){
char cmd[256]={0};
char buf1[10],buf2[10];
const char *y = getCGIvarByKey("year",pVars);
int mon = atoi(getCGIvarByKey("month",pVars));
sprintf(buf1,"%02d",mon);
int d = atoi(getCGIvarByKey("day",pVars));
sprintf(buf2,"%02d",d);
const char *h = getCGIvarByKey("hour",pVars);
const char *min = getCGIvarByKey("minute",pVars);
const char *s = getCGIvarByKey("second",pVars);
int running = 1;
void *shared_memory = (void *)0;
dt *pdt;
char buffer[BUFSIZ];
int shmid;
shmid = shmget((key_t)1234, sizeof(dt), 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);
pdt = (dt *)shared_memory;
pdt->year = y;
pdt->month = buf1;
pdt->day = buf2;
pdt->hour = h;
pdt->min = min;
pdt->sec = s;
pdt->flag = 1;
cgiDebug(pdt->year);
if(shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failedn");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
//读进程
int main()
{
printf("begin");
int running = 1;
void *shared_memory = (void *)0;
int shmid;
shmid = shmget((key_t)1234, sizeof(dt), 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);
}
dt *pdt = (dt *)shared_memory;
printf("flag=%dn", pdt->flag);
// pdt->flag = 0;
while(running)
{
if(pdt->flag)
{
printf("flag");
char cmd[256] = {0};
char buf1[8] = {0};
char buf2[8] = {0};
int mon = atoi(pdt->month);
int day = atoi(pdt->day);
sprintf(buf1,"%02d",mon);
sprintf(buf2,"%02d",day);
strcat(cmd,pdt->year);
strcat(cmd,buf1);
strcat(cmd,buf2);
strcat(cmd," ");
strcat(cmd,pdt->hour);
strcat(cmd,":");
strcat(cmd,pdt->min);
strcat(cmd,":");
strcat(cmd,pdt->sec);
printf("cmd=%sn", cmd);
system(cmd);
pdt->flag = 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);
}
错误代码:
pc : [] lr : [] Not tainted
sp : bffffc60 ip : 400920e8 fp : bffffd94
r10: 4013d180 r9 : 00008658 r8 : 4013f430
r7 : 00000001 r6 : 00008930 r5 : 4001bd80 r4 : bffffdc4
r3 : 00000000 r2 : bffffc77 r1 : 00086c00 r0 : bffffc78
Flags: nZCv IRQs on FIQs on Mode USER_32 Segment user
Control: C000317F Table: 33568000 DAC: 00000015
Segmentation fault
高手给看看什么问题!谢谢
结构体
typedef struct daytime
{
char *year;
char *month;
char *day;
char *hour;
char *min;
char *sec;
int flag;
}dt;
//写进程
int write(){
char cmd[256]={0};
char buf1[10],buf2[10];
const char *y = getCGIvarByKey("year",pVars);
int mon = atoi(getCGIvarByKey("month",pVars));
sprintf(buf1,"%02d",mon);
int d = atoi(getCGIvarByKey("day",pVars));
sprintf(buf2,"%02d",d);
const char *h = getCGIvarByKey("hour",pVars);
const char *min = getCGIvarByKey("minute",pVars);
const char *s = getCGIvarByKey("second",pVars);
int running = 1;
void *shared_memory = (void *)0;
dt *pdt;
char buffer[BUFSIZ];
int shmid;
shmid = shmget((key_t)1234, sizeof(dt), 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);
pdt = (dt *)shared_memory;
pdt->year = y;
pdt->month = buf1;
pdt->day = buf2;
pdt->hour = h;
pdt->min = min;
pdt->sec = s;
pdt->flag = 1;
cgiDebug(pdt->year);
if(shmdt(shared_memory) == -1)
{
fprintf(stderr, "shmdt failedn");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
//读进程
int main()
{
printf("begin");
int running = 1;
void *shared_memory = (void *)0;
int shmid;
shmid = shmget((key_t)1234, sizeof(dt), 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);
}
dt *pdt = (dt *)shared_memory;
printf("flag=%dn", pdt->flag);
// pdt->flag = 0;
while(running)
{
if(pdt->flag)
{
printf("flag");
char cmd[256] = {0};
char buf1[8] = {0};
char buf2[8] = {0};
int mon = atoi(pdt->month);
int day = atoi(pdt->day);
sprintf(buf1,"%02d",mon);
sprintf(buf2,"%02d",day);
strcat(cmd,pdt->year);
strcat(cmd,buf1);
strcat(cmd,buf2);
strcat(cmd," ");
strcat(cmd,pdt->hour);
strcat(cmd,":");
strcat(cmd,pdt->min);
strcat(cmd,":");
strcat(cmd,pdt->sec);
printf("cmd=%sn", cmd);
system(cmd);
pdt->flag = 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);
}
错误代码:
pc : [] lr : [] Not tainted
sp : bffffc60 ip : 400920e8 fp : bffffd94
r10: 4013d180 r9 : 00008658 r8 : 4013f430
r7 : 00000001 r6 : 00008930 r5 : 4001bd80 r4 : bffffdc4
r3 : 00000000 r2 : bffffc77 r1 : 00086c00 r0 : bffffc78
Flags: nZCv IRQs on FIQs on Mode USER_32 Segment user
Control: C000317F Table: 33568000 DAC: 00000015
Segmentation fault
高手给看看什么问题!谢谢
|
你的结构体定义错误:
结构体
typedef struct daytime
{
char *year;
char *month;
char *day;
char *hour;
char *min;
char *sec;
int flag;
}dt;
都是指针,改成
结构体
typedef struct daytime
{
char year[4];
char month[2];
char day[2];
char hour[2];
char min[2];
char sec[2];
int flag;
}dt;
结构体
typedef struct daytime
{
char *year;
char *month;
char *day;
char *hour;
char *min;
char *sec;
int flag;
}dt;
都是指针,改成
结构体
typedef struct daytime
{
char year[4];
char month[2];
char day[2];
char hour[2];
char min[2];
char sec[2];
int flag;
}dt;
|
你加几条printf语句,看看在什么地方Segmentation fault
|
恩。不能定义成指针,因为没有空间在结构体中存放数据。