重定向运算符在unix中不起作用?

时间:2015-08-18 06:04:50

标签: unix io-redirection

我正在运行一个程序,它正在控制台上打印所有printf语句。 但是当我尝试使用'>'将它们重定向到任何文件时,文件会被创建,但文件中没有该程序的输出。 请帮忙 当我在控制台中运行以下代码时:

#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>

double time_diff(struct timeval x , struct timeval y);

struct timeval initial;
long sno=0;

void *process1 (void *sleepTimeForP1);
void *process2 (void *sleepTimeForP2);

pthread_mutex_t lock;

int main()
{
    gettimeofday(&initial , NULL);
    pthread_t trd1,trd2;
    int thread1,thread2;
    int var1;
    int var2;
    int *sleepTimeForP1;
    int *sleepTimeForP2;
    var1=rand()%9+1;
    sleepTimeForP1=&var1;
    var2=rand()%9+1;
    sleepTimeForP2=&var2;
    printf("S No.\tThread Number\tItem\tTime(usec)\n");
    thread1=pthread_create(&trd1,NULL,process1,(void *)sleepTimeForP1);
    thread2=pthread_create(&trd2,NULL,process2,(void *)sleepTimeForP2);
    pthread_join(trd1, NULL);
    pthread_join(trd2, NULL);
    printf("pthread1 = %d\n",thread1);
    printf("pthread2 = %d\n",thread2);

    return 0;
}

double time_diff(struct timeval x , struct timeval y)
{
    double x_ms , y_ms , diff;
    x_ms = (double)x.tv_sec*1000000 + (double)x.tv_usec;
    y_ms = (double)y.tv_sec*1000000 + (double)y.tv_usec;
    diff = (double)y_ms - (double)x_ms;
    return diff;
}

void *process1 (void *sleepTimeForP1)
{
    int *tsleepTimeForP1 = (int *)sleepTimeForP1;
    struct timeval end;
    while(1)
    {
        pthread_mutex_lock(&lock);
        sno++;
        gettimeofday(&end , NULL);
        printf("%ld\t1\t\t1.1\t%.0lf\n",sno,time_diff(initial, end));
        sno++;
        gettimeofday(&end , NULL);
        printf("%ld\t1\t\t1.2\t%.0lf\n",sno,time_diff(initial, end));
        pthread_mutex_unlock(&lock);
        sleep(1);
    }

}

void *process2 (void *sleepTimeForP2)
{
    int *tsleepTimeForP2 = (int *)sleepTimeForP2;
    struct timeval end;
    while(1)
    {
        pthread_mutex_lock(&lock);
        sno++;
        gettimeofday(&end , NULL);
        printf("%ld\t2\t\t2.1\t%.0lf\n",sno,time_diff(initial, end));
        sno++;
        gettimeofday(&end , NULL);
        printf("%ld\t2\t\t2.2\t%.0lf\n",sno,time_diff(initial, end));
        pthread_mutex_unlock(&lock);
        sleep(1);
    }
}

它给我以下输出:

S No.   Thread Number   Item    Time(usec)
1   1       1.1 320
2   1       1.2 438
3   2       2.1 506
4   2       2.2 586
5   1       1.1 1000592
6   1       1.2 1000629
7   2       2.1 1000714
8   2       2.2 1000740
9   1       1.1 2000820
10  1       1.2 2000927
11  2       2.1 2000998
12  2       2.2 2001099
13  1       1.1 3001165
14  1       1.2 3001285
15  2       2.1 3001355
16  2       2.2 3001441
17  1       1.1 4001518
18  1       1.2 4001635
19  2       2.1 4001706
20  2       2.2 4001798
21  1       1.1 5001776

但是当我这样做时./a.out&gt; b.txt 我没有在控制台和文件

中获得任何输出

2 个答案:

答案 0 :(得分:2)

当检测到输出未定向到终端时,缓冲在幕后设置为阻止缓冲。 如果你已经等待了足够长的时间(可能要生成4096字节的输出),那么整个输出将以批量形式出现。

要解决此问题,您可以在fflush(stdout);之后使用printf();,或者在setlinebuf(stdout);开头强制将缓冲模式强制为行缓冲。< / p>

查看setlinebuf()的联机帮助页以获取更多信息。

答案 1 :(得分:0)

我会给出一些指示。阅读Linux Programming Book,了解实际情况。

我在您的代码的相关fflush(stdout)语句后添加了printf(),即line 32,65,85

完成后,您将获得所需的输出。现在试着理解为什么会出现这种行为以及为什么需要明确的fflush()?我相信问题海报应该有一些努力来理解问题并发布Minimal Complete Verifiable Example

完整代码:

#include <stdio.h>
#include <math.h>
#include <sys/time.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>

double time_diff(struct timeval x , struct timeval y);

struct timeval initial;
long sno=0;

void *process1 (void *sleepTimeForP1);
void *process2 (void *sleepTimeForP2);

pthread_mutex_t lock;

int main()
{
    gettimeofday(&initial , NULL);
    pthread_t trd1,trd2;
    int thread1,thread2;
    int var1;
    int var2;
    int *sleepTimeForP1;
    int *sleepTimeForP2;
    var1=rand()%9+1;
    sleepTimeForP1=&var1;
    var2=rand()%9+1;
    sleepTimeForP2=&var2;
    printf("S No...\tThread Number\tItem\tTime(usec)\n");
    fflush(stdout);
    thread1=pthread_create(&trd1,NULL,process1,(void *)sleepTimeForP1);
    thread2=pthread_create(&trd2,NULL,process2,(void *)sleepTimeForP2);
    pthread_join(trd1, NULL);
    pthread_join(trd2, NULL);
    printf("pthread1 = %d\n",thread1);
    printf("pthread2 = %d\n",thread2);

    return 0;
}

double time_diff(struct timeval x , struct timeval y)
{
    double x_ms , y_ms , diff;
    x_ms = (double)x.tv_sec*1000000 + (double)x.tv_usec;
    y_ms = (double)y.tv_sec*1000000 + (double)y.tv_usec;
    diff = (double)y_ms - (double)x_ms;
    return diff;
}

void *process1 (void *sleepTimeForP1)
{
    int *tsleepTimeForP1 = (int *)sleepTimeForP1;
    struct timeval end;
    while(1)
    {
        pthread_mutex_lock(&lock);
        sno++;
        gettimeofday(&end , NULL);
        printf("%ld\t1\t\t1.1\t%.0lf\n",sno,time_diff(initial, end));
        sno++;
        gettimeofday(&end , NULL);
        printf("%ld\t1\t\t1.2\t%.0lf\n",sno,time_diff(initial, end));
        fflush(stdout);
        pthread_mutex_unlock(&lock);
        sleep(1);
    }

}

void *process2 (void *sleepTimeForP2)
{
    int *tsleepTimeForP2 = (int *)sleepTimeForP2;
    struct timeval end;
    while(1)
    {
        pthread_mutex_lock(&lock);
        sno++;
        gettimeofday(&end , NULL);
        printf("%ld\t2\t\t2.1\t%.0lf\n",sno,time_diff(initial, end));
        sno++;
        gettimeofday(&end , NULL);
        printf("%ld\t2\t\t2.2\t%.0lf\n",sno,time_diff(initial, end));
           fflush(stdout); 
pthread_mutex_unlock(&lock);
        sleep(1);
    }
}