64,685
社区成员
发帖
与我相关
我的任务
分享
//test.c 本程序演示两个进程之间通过临时文本文件交换数据实现间接调用
//A将请求数据写到文件a.txt,写完后改名为aa.txt
//B发现aa.txt存在时,读取其内容,调用相应功能,将结果写到文件b.txt,写完后删除aa.txt,再将b.txt改名为bb.txt
//A发现bb.txt存在时,读取其内容,读完后删除bb.txt
#include <io.h>
#include <stdio.h>
#include <windows.h>
int main(int argc,char **argv) {
int d,r;
FILE *f;
char cmdstr[512];
if (argc<2) {
sprintf(cmdstr,"cmd /c \"%s\" B",argv[0]);
WinExec(cmdstr,SW_HIDE);
for (d=1;d<=5;d++) {//共使用1..5调用B 5次
//A将请求数据写到文件a.txt,写完后改名为aa.txt
f=fopen("a.txt","w");
fprintf(f,"%d\n",d);
fclose(f);
rename("a.txt","aa.txt");
printf("call with %d\n",d);
//A发现bb.txt存在时,读取其内容,读完后删除bb.txt
while (1) {
Sleep(100);
if (_access("bb.txt",0)!=-1) {
f=fopen("bb.txt","r");
fscanf(f,"%d",&r);
fclose(f);
remove("bb.txt");
break;
}
}
printf("result:%d\n",r);
}
return 0;
}
if (argv[1][0]=='a'||argv[1][0]=='A') {//A
for (d=1;d<=5;d++) {//共使用1..5调用B 5次
//A将请求数据写到文件a.txt,写完后改名为aa.txt
f=fopen("a.txt","w");
fprintf(f,"%d\n",d);
fclose(f);
rename("a.txt","aa.txt");
printf("call with %d\n",d);
//A发现bb.txt存在时,读取其内容,读完后删除bb.txt
while (1) {
Sleep(100);
if (_access("bb.txt",0)!=-1) {
f=fopen("bb.txt","r");
fscanf(f,"%d",&r);
fclose(f);
remove("bb.txt");
break;
}
}
printf("result:%d\n",r);
}
} else {//B
while (1) {
//B发现aa.txt存在时,读取其内容,调用相应功能,将结果写到文件b.txt,写完后删除aa.txt,再将b.txt改名为bb.txt
while (1) {
Sleep(100);
if (_access("aa.txt",0)!=-1) {
f=fopen("aa.txt","r");
fscanf(f,"%d",&r);
fclose(f);
r=r*10;
f=fopen("b.txt","w");
fprintf(f,"%d\n",r);
fclose(f);
remove("aa.txt");
rename("b.txt","bb.txt");
break;
}
}
if (r==50) break;
}
}
return 0;
}
//C:\tmp\test>test
//call with 1
//result:10
//call with 2
//result:20
//call with 3
//result:30
//call with 4
//result:40
//call with 5
//result:50
//
//C:\tmp\test>start /B test B
//
//C:\tmp\test>test A
//call with 1
//result:10
//call with 2
//result:20
//call with 3
//result:30
//call with 4
//result:40
//call with 5
//result:50
//
//C:\tmp\test>
#include<iostream>
using namespace std;
int main(int argc,char * argv[])
{
int m,n;
cout<<argv[0]<<endl;
cout<<argv[1]<<endl;
cout<<argv[2]<<endl;
cin>>m>>n;
cout<<m*n<<endl;
return 0;
}
#include<stdlib.h>
int main(int argc, char *argv[]){
system("ls -l");
return 0;
}