使用管道和分支用C语言编程?

时间:2013-11-15 23:14:28

标签: c linux fork pipe

我有一个任务,我必须解决这个问题...我是一个完整的新手C和我仍然试图学习C。

所以这是问题

  

编写一个创建管道和子进程的程序。父母重复设置闹钟15秒。触发警报时,父级会计算自启动以来经过的秒数和微秒数,并通过管道将这些数据发送给子级。在获取信息时,孩子在屏幕上显示它们。整个过程持续了2分钟。

我试过这个问题,但我有很多错误..

这是我的解决方案..

#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <time.h>


int main(void)
{
        int     fd[2], nbytes;
    int fd2[2];
        pid_t   childpid;
        char    readbuffer1[80];
    char    readbuffer2[80];

         clock_t start, stop;
         long count;
         double time_sec, time_milli;
    const char* time1, time2;

        pipe(fd);
    pipe(fd2);
        childpid = fork();



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

        if(childpid == 0)
        {
                /* Child process closes up input side of pipe */
                close(fd[1]);


        /* Read in a string from the pipe */
        read(fd[0], readbuffer1, sizeof(readbuffer1));
        read(fd2[0], readbuffer2, sizeof(readbuffer2));
                printf("Received string 1: %s", readbuffer1);
        printf("Received string 2: %s", readbuffer2);

        }
        else
        {
                start = clock();
                /* Parent process closes up output side of pipe */

                alarm(15);
                /* Send "string" through the output side of pipe */


                stop = clock();



                time_sec = (double)(stop-start)/CLOCKS_PER_SEC;
        time_milli = time_sec*1000;
        sprintf(&time1,"%f",time_sec);
        sprintf(&time2,"%f",time_milli);
        close(fd[0]);
        write(fd[1], time1, (strlen(time1)+1));
        write(fd2[1], time2, (strlen(time2)+1));

        }

        return(0);
}

如何让这个运行2分钟?怎么能反复运行闹钟15秒?请帮帮....

3 个答案:

答案 0 :(得分:3)

确定。我已经更正了你的代码如下。您可以直接编译并运行它。它应该工作。

您的代码有几个问题:

  1. 你应该在sprintf之前为time1和time2分配内存空间。

  2. 您应该使用time()而不是clock()。看到: clock() function always returning 0

  3. 使用sleep()而不是alarm()

  4. 希望这有用!

    #include <string.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main(void){
    
    
    int     fd[2];
    int     nbytes;
    int     fd2[2];
    pid_t   childpid;
    char    readbuffer1[80];
    char    readbuffer2[80];
    
    time_t start, stop;
    long count;
    double time_sec, time_milli;
    char* time1;
    char* time2;
    
    pipe(fd);
    pipe(fd2);
    childpid = fork();
    
        if(childpid == -1)
        {
                perror("fork");
                exit(1);
        }
    
        if(childpid == 0)
        {
                /* Child process closes up input side of pipe */
                close(fd[1]);
    
          /* Read in a string from the pipe */
          read(fd[0], readbuffer1, sizeof(readbuffer1));
          read(fd2[0], readbuffer2, sizeof(readbuffer2));
    
              printf("Received string 1: %s", readbuffer1);
          printf("Received string 2: %s", readbuffer2);
        }
        else
        {
    
            close(fd[0]);
    
            time(&start);
    
              sleep(15);
    
              time(&stop);
    
          time_sec = stop-start;
          time_milli = time_sec*1000;
    
          time1 = malloc(80);
          time2 = malloc(80);
    
          sprintf(time1,"%f",time_sec);
          sprintf(time2,"%f",time_milli);
    
    
          write(fd[1], time1, (strlen(time1)+1));
    
          write(fd2[1], time2, (strlen(time2)+1));
    
        }
    }
    

答案 1 :(得分:1)

首先,如果您运行代码,会得到什么错误?

我在你的代码中看到的第一个问题是你正在创建两个管道,而不是问题中提到的那个!

这应该创建一个合适的管道,你可以在那里做你的东西

修改:我刚在代码中添加了一个简单的一次通信。您所要做的就是将所有内容包装在一个运行2分钟的循环中,然后在正确的时间发送警报。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
        int pipefd[2];
        int rt;
        pid_t child = 0;

        // An array with some values
        char charBuffer[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        // A buffer we use to communicate over the pipe
        char outputBuffer[10];

        if( pipe(pipefd) == -1 ) // Create new pipe
        {
                fprintf(stderr, "Failed to create pipe\n");
                exit(1);
        }

        child = fork();        // Create new Child

        if( !child )
        {
                printf("child created\n");
                close(pipefd[1]); // Child is read only

                // Wait for your alarm here
                while( read(pipefd[0], (void*)&outputBuffer, 10) > 0 )
                {
                    printf("Child received something over the pipe\n");
                    // Write output direct to stdout
                    rt = write(STDOUT_FILENO, &outputBuffer, 10);
                    rt = write(STDOUT_FILENO, "\n", 1);
                }
        }
        else if( child == -1 )
        {
                fprintf(stderr, "Error on fork - no child\n");
                exit(2);
        }
        else if( child > 0 )
        {
                close(pipefd[0]); // Parent is write only

                // Write our values to the pipe
                rt = write(pipefd[1], &charBuffer, 10);

                printf("Parent finished writing\n");
                close(pipefd[1]); // Signal writing is finished
        }

        return 0;
}

答案 2 :(得分:0)

您只需sleep 15秒并将其包装成循环

for (int i = 0; i < 8; ++i) {
    sleep(15);
    /* do something, talk to your child, ... */
}