#include <pthread.h>
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
string filename1,filename2;
int number1 , number2 ;
string from,to;
string string_replace(string strBig,string strsrc,string strdst,int flag)
{
int pos = 0;
int srclen = strsrc.size();
int dstlen = strdst.size();
while( (pos=strBig.find(strsrc, pos)) != std::string::npos )
{
strBig.replace( pos, srclen, strdst );
if(flag == 1)number1++;
else number2++;
pos += dstlen;
}
return strBig;
}
void * rep1(void * m) {
ofstream out;
out.open(filename1.c_str(),ios::app);
out.close();
ifstream inf;
inf.open(filename1.c_str());
string sline;
vector<string> txt;
while(getline(inf,sline))
{
txt.push_back(sline);
}
inf.close();
for(int i=0;i<txt.size();i++)
{
txt[i] = string_replace(txt[i],from,to,1);
}
ofstream in;
in.open(filename1.c_str(),ios::trunc);
for(int i =0;i<txt.size();i++)
{
in<<txt[i]<<endl;
}
in.close();
return NULL;
}
void * rep2(void * m) {
ofstream out;
out.open(filename2.c_str(),ios::app);
out.close();
ifstream inf;
inf.open(filename2.c_str());
string sline;
vector<string> txt;
while(getline(inf,sline))
{
txt.push_back(sline);
}
inf.close();
for(int i=0;i<txt.size();i++)
{
txt[i] = string_replace(txt[i],from,to,2);
}
ofstream in;
in.open(filename2.c_str(),ios::trunc);
for(int i =0;i<txt.size();i++)
{
in<<txt[i]<<endl;
}
in.close();
return NULL;
}
int main() {
pthread_t t1,t2;
cout<<"please input file 1 path:";
cin>>filename1;
cout<<"please input file 2 paht:";
cin>>filename2;
cout<<"please input string replace from:";
cin>>from;
cout<<"please input string replace to:";
cin>>to;
pthread_create(&t1,NULL,rep1,(void *)NULL);
pthread_create(&t2,NULL,rep2,(void *)NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
cout<<"replace number in file 1 :"<<number1<<endl;
cout<<"replace number in file 2 :"<<number2<<endl;
cout<<"replace total number :"<<number1+number2<<endl;
}
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#define MSGKEY1 75
#define MSGKEY2 76
char msg1[1024],msg2[1024];
void client()
{
pid_t a;
a = fork();
if(a == 0)
{
pid_t pid;
pid = fork();
if(pid>0)
{
sleep(0.1);
int msgqid1 = msgget(MSGKEY2,0777);
do
{
scanf("%s",msg1);
printf("PB send: %s\n",msg1);
msgsnd(msgqid1,&msg1,sizeof(msg1),0);
} while(strcmp(msg1,"quit") != 0 &&strcmp(msg2,"quit") != 0);
kill(pid,SIGKILL);
exit(0);
}
else if (pid == 0)
{
int msgqid2 = msgget(MSGKEY1,0777|IPC_CREAT);
do
{
msgrcv(msgqid2,&msg2,sizeof(msg2),0,0);
printf("PB rev: %s\n",msg2);
} while(strcmp(msg1,"quit") != 0 &&strcmp(msg2,"quit") != 0);
kill(getppid(),SIGKILL);
exit(0);
}
}
int status;
pid_t ret;
ret = wait(&status);
return ;
}
int main()
{
client();
return 0;
}
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#define MSGKEY1 75
#define MSGKEY2 76
char msg1[1024],msg2[1024];
void client()
{
pid_t a;
a = fork();
if(a == 0)
{
pid_t pid;
pid = fork();
if(pid>0)
{
sleep(0.1);
int msgqid1 = msgget(MSGKEY1,0777);
do
{
scanf("%s",msg1);
printf("PA send: %s\n",msg1);
msgsnd(msgqid1,&msg1,sizeof(msg1),0);
} while(strcmp(msg1,"quit") != 0 &&strcmp(msg2,"quit") != 0);
kill(pid,SIGKILL);
exit(0);
}
else if (pid == 0)
{
int msgqid2 = msgget(MSGKEY2,0777|IPC_CREAT);
do
{
msgrcv(msgqid2,&msg2,sizeof(msg2),0,0);
printf("PA rev: %s\n",msg2);
} while(strcmp(msg1,"quit") != 0 &&strcmp(msg2,"quit") != 0);
kill(getppid(),SIGKILL);
exit(0);
}
}
int status;
pid_t ret;
ret = wait(&status);
return ;
}
int main()
{
client();
}