-1

我正在尝试制作一个全双工管道,这是我的家庭作业(我事先告诉过)。作业的范围比这个管道大得多,我只需要完成它就可以继续工作。我在这里只粘贴重要的代码,我没有粘贴整个代码。

#define READ_END 0
#define WRITE_END 1

int main()
{
    Folder* curFolder = (Folder*) malloc (sizeof(Folder));
    assert(curFolder != NULL);

    initFolder(curFolder, NULL, "root");
    makeDir(curFolder, "test");

    return 0;
}

Pipe* createPipe()
{
    Pipe* p = (Pipe*) malloc (sizeof(Pipe));
    assert(p != NULL);

    pipe( p->readPipe );
    pipe( p->writePipe );

    //close( p->readPipe[WRITE_END] );
    //close( p->writePipe[READ_END] );

    return p;
}

char* readMsg(FolderRef* folderHandle, char* buffer)
{
    read(folderHandle->pipe->readPipe[READ_END], buffer, PIPE_BUF);

    return buffer;
}

void sendMsg(FolderRef* folderHandle, char* msg)
{
    write(folderHandle->pipe->writePipe[WRITE_END], msg, strlen(msg) + 1);
}

void makeDir(Folder* curFolder, char* name)
{
    pid_t childId;
    char msg[PIPE_BUF];
    Pipe* con = createPipe();

    childId = fork();

    switch ( childId )
    {
        case -1: // error occurred
        {
            printf( "Error at fork in function makeDir.\n" );
            exit( 0 );
        }
        case 0: // son process
        {
            initFolder( curFolder, getFolderHandle( getppid(), con, "" ), name );
            readMsg(curFolder->parentFolder, msg);
            printf("%s\n", msg);
            sendMsg(curFolder->parentFolder, "roger roger");
            break;
        }
        default:
        {
            insertFolder(&curFolder->folderList, getFolderHandle(childId, con, name));
            sendMsg(curFolder->folderList->folder, "test");
            readMsg(curFolder->folderList->folder, msg);
            printf("%s\n", msg);
            break;
        }
    }
}

结果(现在)应该在屏幕上输出:test roger roger

但是我无法完成它。我似乎错过了一些小细节。

4

1 回答 1

3

如果您写信,则pipe->writePipe[WRITE_END]需要从pipe->writePipe[READ_END]. 似乎您尝试从中读取,pipe->readPipe[READ_END]但这是另一个管道。

于 2012-12-08T20:48:48.617 回答