2 me1171115772 me1171115772 于 2016.09.18 21:17 提问

关于linux下fork创建进程的问题

本意是希望通过父进程创建两个进程,但是不知道为什么不停创建client,代码如下,希望有时间的帮忙看一下,十分感谢!
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PERM S_IRUSR|S_IWUSR
#define MSG_FILE "/home/allen/myfiles/server.c"
using namespace std;

class msgtype{
public:
long msgtyoe;
char buffer[256];
};
//a judge function for clien son process
bool judge(char *);
int main()

{
int msgid;//message queue id.
//use global variable ,evety child thread can get the message queue key.
//msgtype msg;
key_t key;
if(pid_t fpid=fork()>=0)
{
// server child thread code block
if(fpid==0)
{
//server code block
//it is the child thread

        cout<<"this is server thread"<<endl;
        //create file stream to save the key or save msgid.
        ofstream msgfile(MSG_FILE);
        msgtype *msg=new msgtype;
        if ((msgid = msgget(key, PERM | IPC_CREAT | IPC_EXCL)) == -1) {
            fprintf(stderr, "Creat Message Error:%s\a\n", strerror(errno));
            exit(1);
        }
        else
        {
            msgfile<<msgid;
            msgfile.close();
            //output the msgid for debug.
            cout<<msgid<<endl;
            while(1)
            {
                int x;
                cin >> x;
                //do not check the type and return the oldest msg autolly.
                msgrcv(msgid,msg,sizeof(msgtype),1,0);
                if(msg->buffer[0]!='j')
                {
                    cout<<"Server Receive :"<<msg->buffer<<endl;

                }
                else if (msg->buffer[0]=='s')
                {
                    exit(0);
                }
                else
                {

                    msg->msgtyoe=2;
                    string h="j"+110;
                    memcpy(msg->buffer,h.c_str(),sizeof(h.c_str()));
                    msgsnd(msgid,msg,sizeof(msgtype),0);
                }

            }
        }

    }
    //parent thread block
    else
    {

        pid_t client_thread;
        msgtype *msg=new  msgtype;
        if(client_thread=fork()==-1)
        {
            fprintf(stderr,"Create client thread error: %s\a\n",strerror(errno));

            exit(1);

        }
        else{


            //client  child thread code block

            if(client_thread==0)
            {
                ifstream inmsgfile("MSG_FILE");
                cout<<"this is client thread"<<endl;
                if(msgid=msgget(key,PERM)!=-1)
                {
                    msg->msgtyoe=1;
                    inmsgfile >> msgid;
                    cout<<"msgid == "<<msgid<<endl;
                    msgsnd(msgid,msg,sizeof(msgtype),0);
                    msgrcv(msgid,msg,sizeof(msgtype),2,0);
                    if(judge(msg->buffer))
                    {
                        string h="it is a prime   "+msg->buffer[1];
                        memcpy(msg->buffer,h.c_str(),sizeof(h.c_str()));
                        msgsnd(msgid,msg,sizeof(msgtype),0);

                    }

                    exit(0);

                }

            }
        }





    }





}





cout << "Hello world!" << endl;
return 0;

}
bool judge(char *s)
{
if(s[0]=='j')//first j means to judge
{
int n=s[1];
if ( n < 2 )
{ // 小于2的数即不是合数也不是素数
throw 0;
}
static unsigned aPrimeList[] = { // 素数表
1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 113,
193, 241, 257, 337, 353, 401, 433, 449, 577, 593, 641,
673, 769, 881, 929, 977, 1009, 1153, 1201, 1217, 1249,
1297,1361, 1409, 1489, 1553, 1601, 1697, 1777, 1873,
1889, 2017, 2081, 2113, 2129, 2161, 2273, 2417, 2593,
2609, 2657, 2689, 2753, 2801, 2833, 2897, 3041, 3089,
3121, 3137, 3169, 3217, 3313, 3329, 3361, 3457, 3617,
3697, 3761, 3793, 3889, 4001, 4049, 4129, 4177, 4241,
4273, 4289, 4337, 4481, 4513, 4561, 4657, 4673, 4721,
4801, 4817, 4993, 5009, 5153, 5233, 5281, 5297, 5393,
5441, 5521, 5569, 5857, 5953, 6113, 6257, 6337, 6353,
6449, 6481, 6529, 6577, 6673, 6689, 6737, 6833, 6961,
6977, 7057, 7121, 7297, 7393, 7457, 7489, 7537, 7649,
7681, 7793, 7841, 7873, 7937, 8017, 8081, 8161, 8209,
8273, 8353, 8369, 8513, 8609, 8641, 8689, 8737, 8753,
8849, 8929, 9041, 9137, 9281, 9377, 9473, 9521, 9601,
9649, 9697, 9857
};

        const int nListNum = sizeof(aPrimeList)/sizeof(unsigned);//计算素数表里元素的个数
        for (unsigned i=2;i<nListNum;++i )
        {
            if(n/2+1<aPrimeList[i])
            {
                return true;
            }
            if(0==n%aPrimeList[i])
            {
                return false;
            }
        }
        /*由于素数表中元素个数是有限的,那么对于用素数表判断不到的数,就只有用笨蛋办法了*/
        for (unsigned i=aPrimeList[nListNum-1];i<n/2+1;i++ )
        {
            if (0==n%i)
            {
                // 除尽了,合数
                return false;
            }
        }
        return true;


}

}

输出如下:
/home/allen/.CLion2016.2/system/cmake/generated/process_msg_clion-b927b03f/b927b03f/Debug/proess_msg
this is client thread
this is client thread
Hello world!
this is client thread
Hello world!
Hello world!
this is client thread
Hello world!

Process finished with exit code 0

IDE用的是CLION

1个回答

cangyingzhijia
cangyingzhijia   2016.09.18 21:42

if(pid_t fpid=fork()>=0) 好像赋值语句优先级低于大于等于吧

Csdn user default icon
上传中...
上传图片
插入图片