我们正在研究Linux red hat服务器来练习进程间通信。我们班的每个人都使用telnet作为不同的用户连接到服务器。
我编译并执行了我的server.c& client.c首先它工作没有错误,但对于我的同学,它在执行server.c时给出打开错误 (对我来说它仍然有效)当我的朋友执行client.c程序时,它会打印我共享的文件内容
供参考, https://users.cs.cf.ac.uk/Dave.Marshall/C/node27.html#SECTION002730000000000000000
SERVER.c
#include<stdio.h>
#include<sys/ipc.h>
#include<unistd.h>
#include<sys/shm.h>
#include<fcntl.h>
#include<string.h>
#define SHMSZ 40
int main()
{
char ch;
int shmid,fo,n,i;
char fname[SHMSZ];
char buff[600];
char *shm,*s;
int key=3600;
if((shmid=shmget(key,SHMSZ,IPC_CREAT|0666))<0)
printf("\nOpen Error");
if((shm=shmat(shmid,NULL,0))==(char*)-1)
printf("\nOpen Error");
s=shm;
system("clear");
printf("\n server is accepting the file name:");
printf("\n Enter the file name:");
scanf("%s",&fname);
fo=open(fname,O_RDWR);
if(fo<0) // here it fails
printf("\n Open error");
else
{
i=0;
while(read(fo,buff,sizeof(buff))!=0)
{
for(i=0;i<strlen(buff);i++)
*s++=buff[i-1];
}
*s='\0';
printf("\nClient is loading\n");\
while(*shm!='*')
sleep(1);
printf("\nClient finished into work");
}
}
client.c
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/shm.h>
#define SHMSZ 40
int main()
{
int shmid;
int key=3600;
char *shm,*s;
if((shmid=shmget(key,SHMSZ,0666))<0)
printf("\nShmat error");
if((shm=shmat(shmid,NULL,0))==(char*)-1)
printf("\nShmat error");
system("clear");
printf("\nThis is the client");
printf("\nReading the content in the memory");
*shm='*';
for(s=shm;*s!='\0';s++)
putchar(*s);
putchar('\n');
}
预期产量: 服务器端
[it@itserver it]$ cc ipcshms.c
[it@itserver it]$ ./a.out
Server is accepting the filename
Enter the filename:sum.c //shared file
Client is Loading
Client finished its work
客户端
[it@itserver it]$ cc ipcshmc.c
[it@itserver it]$ ./a.out
This is client
Reading the content in memory //shared file content
#include<stdio.h>
int main()
{
printf("Hi");
}
我试着向我的教授询问他拒绝借他的耳朵
什么可能导致if
条件失败并在server.c中给出打开错误?
P.S:
如果我更改分段会有效吗?
答案 0 :(得分:0)
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(
options => options.UseSqlServer(***);
}
因此它执行下一行,表示“打开错误”。
在打开文件时,他们选择了我没有的文件(文件在他们的目录中不存在)
因此导致另一个问题