pipe() read() and write()

I can't seem to get my pipe to work. It tells me invalid file descriptor when I use perror() to output an error. I can't seem to get it to work

What I'm trying to do is emulate a CPU and Memory of a computer. Each entity resides in its own process. Now I'm trying to get values from one side to the other. But I can't seem to get it to work.

I've used close() and dup2() not sure how to go about it.

Here is my main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
pid_t pid;
    int pipeArr[2];
    int i = 0;
    
    //open file
        ifstream inFile( "program.txt" , ios::in);
        
        if (inFile.fail()){
                perror("File not good");
        }
    
        while(inFile.good()){
                inFile >> memory[i];
                i++;
        }
    
        inFile.close();
    
        i = 0;
        while ( memory[i] != (int)NULL ){
            cout<< memory[i] << endl;
            i++;
        }
        cout<<"reached end of printing memory array"<<endl;
        
    // create/error check process and pipe creation
    
    if ( pipe(pipeArr) ){
        perror("invalid pipe");
        exit(10);
    }
    
    if (pid = fork() < 0){  //error
        
        perror("Invalid process id");
        exit(11);
        
    }
    
    
    //---------------------------------------------------------start parent/child processes
    else if ( pid == 0 ) { //child process
        //CPU
        cout << "\n-Starting parent (CPU) process-\n";
        
        int PC = 0;//Program Counter
        int SP = MAX_MEMORY;//Stack Pointer starts at end of memory
        int IR = 0;//Instruction Register 
        int AC = 0;//Accumulator
        int X = 0;
        int Y = 0;
        bool end = false;
        
        cout << "\nstarting while loop\n";
        //while ( !end ){
            int value; //used to receive from pipe
            //reads next PC
            
            dup2(pipeArr[0],pipeArr[1]);
            //close(pipeArr[0]);
            //writes address to pipe
            //if (write(pipeArr[1], &PC, sizeof(int)) == -1)
            //    perror("write() failed. CPU process");
            
            write(pipeArr[1], &PC, sizeof(int));
            cout << "PC: " << PC << endl;
            
            dup2(pipeArr[1],pipeArr[0]);
            
            
            //close(pipeArr[1]);
          if ( read(pipeArr[0], &value, sizeof(int)) == -1 ) //reads value from pipe
                perror("read() failed. CPU process");
            cout << "Value: " << value << endl;
            
            IR = value;
            end = execute(IR, PC, AC, X, Y, SP ); //execute
            
            cout << "end: " << end << endl;
            
            dup2(pipeArr[0],pipeArr[1]);
            //close(pipeArr[0]);
            if ( write(pipeArr[1], &end, sizeof(bool)) == -1 )
                perror("write() failed. CPU process");
            close(pipeArr[1]);
            
            PC++;
            
            cout << "\nending while loop\n";
        //}
        cout << "\n-ending parent (CPU) process-\n";
    }
    
    else {//(pid > 0) parent process
        //Memory
        cout << "\n-Starting child (Memory) process-\n";
        
        bool end = false;
        cout << end << endl;
        cout << "\nstarting while loop\n";
        
        //while ( !end ){
            int tempPC;
            dup2(pipeArr[1],pipeArr[0]);
            //close(pipeArr[1]);
            if ( read(pipeArr[0],  &tempPC, sizeof(int) ) == -1 ) //reads address from pipe
                perror("read() failed. Memory process");
            cout<<tempPC<<endl;
            
            dup2(pipeArr[0],pipeArr[1]);
            //close(pipeArr[0]);
            //if (write(pipeArr[1], &memory[tempPC], sizeof(int) ) == -1 ) //writes data from address to pipe
            //    perror("write() failed. Memory process");
              
            write(pipeArr[1], &memory[tempPC], sizeof(int));
            
            dup2(pipeArr[1],pipeArr[0]);
            //close(pipeArr[1]);
            if ( read(pipeArr[0],  &end, sizeof(bool) ) == -1 )
                perror("read() failed. Memory process");
            close(pipeArr[0]);
        //}
        
        cout << "\n-Ending child (Memory) process-\n";
        
    }
    
    return 0;


I really REALLY need help!
Last edited on
Topic archived. No new replies allowed.