我有两个进程在运行。一个是主服务器,另一个是代理服务器。它们都通过共享内存进行通信。代理服务器写入文件的名称,mainserver读取它。然后主服务器写入要由代理服务器读取的该文件的内容。以下是代码: 主服务器:
//receive data from client
memset(&buffer,'\0',sizeof(buffer));
bytes = read(c_fd, buffer, sizeof(buffer));
if(!(bytes<0 && bytes==0 )){
//SHARED MEMORY STUFF :) //
for(int i=0; i<strlen(b);i++)
printf("%c", b[i]);
char c;
int shmid;
key_t key;
char *shm;
char *s= (char *)malloc (strlen(buffer)+1);
//naming shared memory segment 5678
key=5678;
//Locate the segment
if((shmid =shmget (key,SHMSZ,0666))<0){
perror("shmget");
exit(1);
}
//attaching the segment
if((shm =shmat (shmid,NULL,0))==(char *) -1){
perror("shmat");
exit(1);
}
int count=0;
s =shm;
printf("File name received is :\n");
for(s=shm ; *s!=NULL; s++){
putchar (*s);
if((*s >='a' && *s<='z' ) || *s=='.' ){
b[count]=*s;
count++;}
// else printf("special character");
}
// *shm = '*';
//*s=NULL;
//shmdt ((void *)shmid);
//reading html file
printf("coiunt%d",count);
char *ss="page2.html";
printf("%s",b);
char shared_buffer[69999];
int handle = open(b, O_RDONLY);
if (handle!=-1) //FILE FOUND// change
{
printf("inside %s",b);
send(c_fd, "File found\n\n", 17, 0);
//reading html file
if(read(handle, shared_buffer, 1024)!=-1)
printf("READ");
//adding data to segment
//printf("sharedddd %c",shared_buffer[0]);
s= shm;
for(int i=0; i<strlen(shared_buffer); i++){
*s++= shared_buffer[i];
printf(" %s",s);
// printf("sharedddd %c",shared_buffer[i]);
}
*s= NULL;
}
else {//write(c_fd, " Not Found\n", 23); //FILE NOT FOUND
}
}
fflush(stdout);
}
代理服务器:
//receive data from client
memset(&buffer,'\0',sizeof(buffer));
bytes = read(c_fd, buffer, sizeof(buffer));
if(!(bytes<0 && bytes==0 )){
//SHARED MEMORY STUFF :) //
for(int i=0; i<strlen(b);i++)
printf("%c", b[i]);
char c;
int shmid;
key_t key;
char *shm;
char *s= (char *)malloc (strlen(buffer)+1);
//naming shared memory segment 5678
key=5678;
//Locate the segment
if((shmid =shmget (key,SHMSZ,0666))<0){
perror("shmget");
exit(1);
}
//attaching the segment
if((shm =shmat (shmid,NULL,0))==(char *) -1){
perror("shmat");
exit(1);
}
int count=0;
s =shm;
printf("File name received is :\n");
for(s=shm ; *s!=NULL; s++){
putchar (*s);
if((*s >='a' && *s<='z' ) || *s=='.' ){
b[count]=*s;
count++;}
// else printf("special character");
}
// *shm = '*';
//*s=NULL;
//shmdt ((void *)shmid);
//reading html file
printf("coiunt%d",count);
char *ss="page2.html";
printf("%s",b);
char shared_buffer[69999];
int handle = open(b, O_RDONLY);
if (handle!=-1) //FILE FOUND// change
{
printf("inside %s",b);
send(c_fd, "File found\n\n", 17, 0);
//reading html file
if(read(handle, shared_buffer, 1024)!=-1)
printf("READ");
//adding data to segment
//printf("sharedddd %c",shared_buffer[0]);
s= shm;
for(int i=0; i<strlen(shared_buffer); i++){
*s++= shared_buffer[i];
printf(" %s",s);
// printf("sharedddd %c",shared_buffer[i]);
}
*s= NULL;
}
else {//write(c_fd, " Not Found\n", 23); //FILE NOT FOUND
}
}
fflush(stdout);
}
我只添加了创建和共享内存的相关代码。请注意,这两个代码都在单独的文件中。我需要实现互斥和条件变量,这样当主服务器只能在代理服务器写入内存后才能读取,依此类推。我在互联网上搜索过代码,但它们似乎都在为一个文件工作。如何在两个文件之间共享?请告知一些教程或任何让我入门的内容。