小编Naw*_*sef的帖子

为什么来自管道的消息在时间显示为char?用C语言

我试图使用管道读取从子进程发送到父进程的消息.我在这里问了问题并得到了一些帮助,我得到了消息.但问题是消息一次显示一个字符.我不知道为什么.这是我的代码:

int main(int argc, char *argv[]){

    printf("\nWritten by Nawar Youssef\n");

    int i, x, fd_log[2], fd_A_B[2], pipe_size=500;
    char ch, message_from_A[pipe_size], message_from_B[pipe_size],
                        msg_to_log[pipe_size], msg_to_B[pipe_size];

    pipe(fd_log);
    pipe(fd_A_B);

    //fork process A
    if (fork()==0) { //child
        printf("Inside process A\n");
        for (i=0; i < 10; i++) {
            //creat new a record (C or D 0 to/or 9)
            x = (rand() % 2);
            if (x == 1)
                ch='C';
            else
                ch='D';

            //write msg to log pipe
            sprintf(msg_to_log, "A sent to B: %c %d\n", ch, i);
            printf("wirtten to log …
Run Code Online (Sandbox Code Playgroud)

c linux parent-child

0
推荐指数
1
解决办法
97
查看次数

标签 统计

c ×1

linux ×1

parent-child ×1