重定向运算符在 unix 中不起作用?
Redirection operators are not working in unix?
我正在 运行 开发一个程序,它正在控制台上打印所有 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 > b.txt
我在控制台和文件中都没有得到任何输出
我会给予一些指点。阅读 Linux Programming Book,了解实际情况。
我在你的代码的相关 printf()
语句后添加了一个 fflush(stdout)
,即 line 32,65,85
.
完成此操作后,您将获得所需的输出。现在尝试理解为什么会出现这种行为以及为什么需要明确的 fflush()
?我相信问题 poster 应该做出一些努力来理解问题并 post a 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);
}
}
当检测到输出未定向到终端时,在后台将缓冲设置为块缓冲。
如果您等待的时间足够长(可能生成 4096 字节的输出),那么整个输出将成批出现。
要解决这个问题,您可以在每个 printf();
之后使用 fflush(stdout);
或强制缓冲模式在开头 行缓冲 12=].
查看 setlinebuf() 的联机帮助页了解更多信息。
我正在 运行 开发一个程序,它正在控制台上打印所有 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 > b.txt 我在控制台和文件中都没有得到任何输出
我会给予一些指点。阅读 Linux Programming Book,了解实际情况。
我在你的代码的相关 printf()
语句后添加了一个 fflush(stdout)
,即 line 32,65,85
.
完成此操作后,您将获得所需的输出。现在尝试理解为什么会出现这种行为以及为什么需要明确的 fflush()
?我相信问题 poster 应该做出一些努力来理解问题并 post a 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);
}
}
当检测到输出未定向到终端时,在后台将缓冲设置为块缓冲。 如果您等待的时间足够长(可能生成 4096 字节的输出),那么整个输出将成批出现。
要解决这个问题,您可以在每个 printf();
之后使用 fflush(stdout);
或强制缓冲模式在开头 行缓冲 12=].
查看 setlinebuf() 的联机帮助页了解更多信息。