多个管道在C中不使用信号量

时间:2015-11-09 07:12:35

标签: c pthreads pipe fork semaphore

我正在尝试从父进程向子进程发送消息,然后子进程将发送回复。我用2个管道和3个信号量来控制整个情况。我的信号量是 sem_init(&my_sema, 0, 1); sem_init(&my_sema2, 0, 0); sem_init(&my_sema3, 0, 0); 这个想法是 父函数在sema下面,写入管道并向上移动sema2,这样子进程可以运行,父进程开始等待sema3,这将在子进程完成后递增。 但我没有得到任何方面的输出。 这是我的代码

#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

//define semaphore
sem_t my_sema;
sem_t my_sema2;
sem_t my_sema3;
sem_t my_sema4;

int value;
#define MAX 10

int     fd[2], fd2[2], nbytes;
pid_t   childpid;
char    string[] = "Hello child Process!\n";
char    string2[] = "Hi, parent Process!\n";
char    readbuffer[1024];

void* parentProcess(void *args){
    //wait on sema
    sem_wait(&my_sema); 

    /* close input side to write */
        close(fd[0]);

    /* Send "string" through the output side of pipe */
        write(fd[1], string, (strlen(string)+1));

    //increment the sema so child can read the message 
    sem_post(&my_sema2);


    sem_wait(&my_sema3);

    /* close output fd2[1] to read input */
        close(fd2[1]);


    //read message sent by the child
        nbytes = read(fd2[0], readbuffer, sizeof(readbuffer));
        printf("Child : %s", readbuffer);


    pthread_exit(0);


}
void* childProcess(void *args){

    //wait for sema to become positive
    sem_wait(&my_sema2);

    /* Child process closes up output side of pipe */
        close(fd[1]);

    /* read the parent message */
        nbytes = read(fd[0], readbuffer, sizeof(readbuffer));   
    printf("Parent : %s", readbuffer);
    printf("\n");

    /* Child process closes up input side of pipe */
        close(fd2[0]);

    //send reply to parent process
    /* Send "string" through the output side of pipe */
        write(fd2[1], string2, (strlen(string2)+1)); 

        pthread_exit(0);


}
//main thread starts here
int main(){
    printf("in the main\n");
    //we use fd[0] for reading and fd[1] for writing


    //creating the pipes
        pipe(fd);
    pipe(fd2);

    //initialize the semaphore, returns 0 on success
        sem_init(&my_sema, 0, 1);
        //initialize the sema2 to 0 so initially sema is 1 and only parent can send message 
        sem_init(&my_sema2, 0, 0);
    sem_init(&my_sema3, 0, 0);
    sem_init(&my_sema4, 0, 0);

    pthread_t child_thread; pthread_t parent_thread;

    if((childpid = fork()) == -1)
        {
                perror("fork");
        }

        if(childpid == 0)
        {
        printf("Child started\n");
        pthread_create(&child_thread, NULL, childProcess, NULL);
        }
        else
        {   
        printf("parent started\n");
        pthread_create(&parent_thread, NULL, parentProcess, NULL);
        }   

    printf("waiting for threads\n");

    //wait for the threads to finish
    pthread_join(parent_thread, NULL); pthread_join(child_thread, NULL);
    printf("Threads finished\n");

    sem_destroy(&my_sema); /* destroy semaphore */
        sem_destroy(&my_sema2);
    sem_destroy(&my_sema3);
        sem_destroy(&my_sema4);

    return 0; 
}

0 个答案:

没有答案