#include <iostream>
#include <hiredis/hiredis.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main(){
pid_t pid;
pid = fork();
if(pid <0 ){
std::cout<< "fork error!"<<std::endl;
}
//父子进程都执行
redisContext* conn = redisConnect("10.1.175.130", 6379);
if(NULL==conn || conn->err){
if(conn){
std::cout<<"Error:"<<conn->errstr<<std::endl;
redisFree(conn);
} else {
std::cout<<"Error:Can't allocate redis context"<<std::endl;
}
return 0;
}
if(0==pid){
const char* command = "set name yuanzaixin";
redisReply* r = (redisReply*)redisCommand(conn, command);
if(NULL==r){
redisFree(conn);
return 0;
}
std::cout<<"fork child"<<std::endl;
freeReplyObject(r);
}
if(pid >0){
//父进程执行不一样的命令
const char* command = "set name xinzaiyuan";
redisReply* r = (redisReply*)redisCommand(conn, command);
if(NULL==r){
redisFree(conn);
return 0;
}
std::cout<<"fork parent"<<std::endl;
freeReplyObject(r);
}
//父子进程都执行
if (NULL!=conn){
redisFree(conn);
}
}