Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ shmat()工作不正常_C++ - Fatal编程技术网

C++ shmat()工作不正常

C++ shmat()工作不正常,c++,C++,有人能告诉我为什么我的shmat不能正常工作吗?我不知道为什么int fam没有正确存储数据并返回到0 在我尝试在子进程中递增它之后。我需要使用存储的内存,以便更新我的其他类。有人能帮我弄清楚发生了什么事吗?谢谢 void Execute::terminal(const vector<Command*>& tokens) //runs all the commands { key_t key; int shmid; // int* fam = (in

有人能告诉我为什么我的shmat不能正常工作吗?我不知道为什么int fam没有正确存储数据并返回到0 在我尝试在子进程中递增它之后。我需要使用存储的内存,以便更新我的其他类。有人能帮我弄清楚发生了什么事吗?谢谢

void Execute::terminal(const vector<Command*>& tokens) //runs all the commands
{

    key_t key;
    int shmid;
    // int* fam = (int*)shmat(shm_id, NULL, 0);

    //key = ftok("/home/beej/somefile3", 'R');
    //shmid = shmget(key, 1024, 0644 | IPC_CREAT);
    shmid = shmget(key, 10*sizeof(int), 0644 | IPC_CREAT);
    // fam = static_cast<int*>(shmat(shmid, (void *) 0, 0));
    int *fam = /*static_cast<int*>*/(int*)(shmat(shmid, NULL, 0));
    fam = 0;
    cout << fam << endl;
    int termstat;
    bool temp;
    vector<char*>convert;
    vector<string> parse;
    string text;
    // ; = next command is always executed
    // && = next command only executes if previous command succesfully executed
    // || = next command only executes if previous command didnt
    // # = comment 
    cout << "terminal is working - " << tokens.size() << endl;
    for(int x = 0; x < tokens.size(); x++)
    {

        convert.resize(0);
        text = tokens.at(x)->tokenCheck();
        parsecmd(text,parse);

        for(int i = 0; i < parse.size(); i++)
        {
            convert.push_back(to_char(parse[i]));
        }
        char** arg = &convert.at(0);

        pid_t parent = getpid();
        pid_t pid = fork();

        if(pid < 0)
        {
            perror("Error: fork failed");
            _exit(-127);
        }

        if(pid > 0)
        {
            waitpid(pid, &termstat, 0);
        }
        else
        {
            if(tokens.at(index)->signCheck() == 0)  //comment - delete this token
            {
                tokens.at(index)->addstatus(true);
            }
            if(tokens.at(index)->signCheck() == 1)  // ; always runs
            {
                tokens.at(index)->addstatus(true);
                int check = execvp(arg[0],arg);
                if(check < 0)
                {
                    cout << fam << endl;
                    fam ++;
                    shmdt(fam);
                    cout << fam << endl;
                    cout << "Error: " << arg[0] <<" command not found (;)" << endl;
                    tokens.at(index)->addstatus(false);
                    _exit(-127);
                }
                else
                {
                 tokens.at(index)->addstatus(true);
                 _exit(5);
                }

            }
            if(tokens.at(index)->signCheck() == 2)  // || runs only if first fails
            {
                if(tokens.at(index-1)->statusCheck() == false)
                {

                    int check = execvp(arg[0],arg);
                    if(check < 0)
                    {
                        fam ++;
                        cout << "Error: " << arg[0] <<" command not found (||)" << endl;
                        tokens.at(index)->addstatus(false);
                        _exit(-127);
                    }
                    else
                    {
                    tokens.at(index)->addstatus(true);
                    _exit(5);
                    }
                }
            }
            if(tokens.at(index)->signCheck() == 3)  // && runs only if first passes
            {
                if(tokens.at(index-1)->statusCheck() == true)
                {

                    int check = execvp(arg[0],arg);
                    if(check < 0)
                    {
                        fam ++;
                        cout << "Error: " << arg[0] <<" command not found (&&)" << endl;
                        tokens.at(index)->addstatus(false);
                        _exit(-127);
                    }
                    else
                    {
                    tokens.at(index)->addstatus(true);
                    _exit(5);
                    }
                }
            }



        }
        cout << "fam at end: " <<fam << endl;
        if(fam == 0)
        {
            tokens.at(index)->addstatus(true);
        }

        this->index++;      // keeps track of which command is being executed
        cout << "index: " << index << endl;
    }
   for(int i = 0; i < tokens.size(); i++){ 
            cout << tokens[i]->statusCheck() << endl;
        }
void Execute::terminal(const vector&tokens)//运行所有命令
{
钥匙(t)钥匙;;
int shmid;
//int*fam=(int*)shmat(shm_id,NULL,0);
//key=ftok(“/home/beej/somefile3”,“R”);
//shmid=shmget(键10240644 | IPC|u CREAT);
shmid=shmget(键,10*sizeof(int),0644 | IPC|u创建);
//fam=静态铸造(shmat(shmid,(void*)0,0));
int*fam=/*static_cast*/(int*)(shmat(shmid,NULL,0));
fam=0;
cout signCheck()==1)/;始终运行
{
标记.at(索引)->addstatus(true);
int check=execvp(arg[0],arg);
如果(检查<0)
{

为什么在使用
shmat
设置后立即设置
fam=0
?从
shmget
返回的值是否有效?是否也不应该初始化
key
呢?如果只设置为
IPC\u PRIVATE
?为什么在使用
shmat
设置后立即设置
fam=0
?是值从
shmget
valid?转到
key
也不应该被初始化——如果只转到
IPC\u PRIVATE