操作系统之随便贴点东西

#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)// father send
    {
        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) //child rev
    {
        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)// father send
        {
            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) //child rev
        {
            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();

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值