C 两个线程-从STDIN读取文本并通过管道发送
我想创建两个线程-一个将从stdin读取字符串,另一个将在屏幕上显示它(通过管道进行通信)。我该怎么做?这是我到目前为止写的:C 两个线程-从STDIN读取文本并通过管道发送,c,linux,multithreading,C,Linux,Multithreading,我想创建两个线程-一个将从stdin读取字符串,另一个将在屏幕上显示它(通过管道进行通信)。我该怎么做?这是我到目前为止写的: #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> #include <linux/stat.h> #include <pthread.h> #include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/stat.h>
#include <pthread.h>
#include <string.h>
int first[2];
void *input(void *ptr)
{
close(first[1]);
while(1)
{
char str[10];
int result;
read(STDIN_FILENO, str, 10);
result = read(first[0], &str, 1);
if(result != 1)
{
perror("read");
exit(3);
}
printf("Reader: %s\n", str);
}
}
void *output(void *ptr)
{
close(first[0]);
int result;
char str[10];
while(1)
{
result = write(first[1], &str, 1);
if(result != 1)
{
perror("write");
exit(2);
}
printf("Writer: %s\n", str);
}
}
int main()
{
pthread_t t1, t2;
pthread_create(&t1, NULL, input, NULL);
pthread_create(&t2, NULL, output, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int-first[2];
无效*输入(无效*ptr)
{
关闭(第一[1]);
而(1)
{
char-str[10];
int结果;
读取(标准文件号,str,10);
结果=读取(第一个[0],&str,1);
如果(结果!=1)
{
佩罗(“阅读”);
出口(3);
}
printf(“读取器:%s\n”,str);
}
}
void*输出(void*ptr)
{
关闭(第一个[0]);
int结果;
char-str[10];
而(1)
{
结果=写入(第一个[1],&str,1);
如果(结果!=1)
{
佩罗(“书面”);
出口(2);
}
printf(“写入程序:%s\n”,str);
}
}
int main()
{
pthread_t t1,t2;
pthread_create(&t1,NULL,input,NULL);
pthread_create(&t2,NULL,output,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
返回0;
}
我可以编译它,但尝试运行它时会显示一个错误“write:Bad file descriptor”。实际上我找不到任何关于管道的教程,只有几段简短的代码。我应该怎么做?- 你对管道的读/写方向混淆了,你正在关闭线程中的管道
- 您需要检查从stdin读取并发送到管道的字节数
- 不能直接对从管道读取的数据使用printf(),该数据可能不是字符串(可能不是以nul结尾的,也可能是二进制数据)
- 您需要实际创建管道
void *input(void *ptr)
{
while(1)
{
char str[10];
int result;
result = read(STDIN_FILENO, str, sizeof str);
if (result <= 0)
{
if(result == -1)
perror("read");
close(first[1]);
return NULL;
}
if(write(first[1], str, result) != result)
{
perror("write");
exit(3);
}
printf("Reader: %s\n", str);
}
}
void *output(void *ptr)
{
int result;
char str[10];
while(1)
{
result = read(first[0], str, sizeof str);
if(result <= 0)
{
if(result == -1)
perror("read");
close(first[0]);
return NULL;
}
if (write(STDOUT_FILENO, str, result) != result) {
perror("write");
exit(4);
}
}
}
int main()
{
pthread_t t1, t2;
pipe(first);
...
}
void*输入(void*ptr)
{
而(1)
{
char-str[10];
int结果;
结果=读取(标准文件号、str、str大小);
如果(结果
- 你对管道的读/写方向混淆了,你正在关闭线程中的管道
- 您需要检查从stdin读取并发送到管道的字节数
- 不能直接对从管道读取的数据使用printf(),该数据可能不是字符串(可能不是以nul结尾的,也可能是二进制数据)
- 您需要实际创建管道
以下是您希望“输入”线程执行的操作:
从标准文本中读取
将步骤1中读取的数据写入管道
和“输出”线程:
从管子里读出
将步骤1中读取的数据写入标准输出
所以这应该是:
void *input(void *ptr)
{
while(1)
{
char str[10];
int result;
result = read(STDIN_FILENO, str, sizeof str);
if (result <= 0)
{
if(result == -1)
perror("read");
close(first[1]);
return NULL;
}
if(write(first[1], str, result) != result)
{
perror("write");
exit(3);
}
printf("Reader: %s\n", str);
}
}
void *output(void *ptr)
{
int result;
char str[10];
while(1)
{
result = read(first[0], str, sizeof str);
if(result <= 0)
{
if(result == -1)
perror("read");
close(first[0]);
return NULL;
}
if (write(STDOUT_FILENO, str, result) != result) {
perror("write");
exit(4);
}
}
}
int main()
{
pthread_t t1, t2;
pipe(first);
...
}
void*输入(void*ptr)
{
而(1)
{
char-str[10];
int结果;
结果=读取(标准文件号、str、str大小);
如果(结果
- 你对管道的读/写方向混淆了,你正在关闭线程中的管道
- 您需要检查从stdin读取并发送到管道的字节数
- 不能直接对从管道读取的数据使用printf(),该数据可能不是字符串(可能不是以nul结尾的,也可能是二进制数据)
- 您需要实际创建管道
以下是您希望“输入”线程执行的操作:
从标准文本中读取
将步骤1中读取的数据写入管道
和“输出”线程:
从管子里读出
将步骤1中读取的数据写入标准输出
所以这应该是:
void *input(void *ptr)
{
while(1)
{
char str[10];
int result;
result = read(STDIN_FILENO, str, sizeof str);
if (result <= 0)
{
if(result == -1)
perror("read");
close(first[1]);
return NULL;
}
if(write(first[1], str, result) != result)
{
perror("write");
exit(3);
}
printf("Reader: %s\n", str);
}
}
void *output(void *ptr)
{
int result;
char str[10];
while(1)
{
result = read(first[0], str, sizeof str);
if(result <= 0)
{
if(result == -1)
perror("read");
close(first[0]);
return NULL;
}
if (write(STDOUT_FILENO, str, result) != result) {
perror("write");
exit(4);
}
}
}
int main()
{
pthread_t t1, t2;
pipe(first);
...
}
void*输入(void*ptr)
{
而(1)
{
char-str[10];
int结果;
结果=读取(标准文件号、str、str大小);
如果(结果
- 你对管道的读/写方向混淆了,你正在关闭线程中的管道
- 您需要检查从stdin读取并发送到管道的字节数
- 不能直接对从管道读取的数据使用printf(),该数据可能不是字符串(可能不是以nul结尾的,也可能是二进制数据)
- 您需要实际创建管道
以下是您希望“输入”线程执行的操作:
从标准文本中读取
将步骤1中读取的数据写入管道
和“输出”线程:
从管子里读出
将步骤1中读取的数据写入标准输出
所以这应该是:
void *input(void *ptr)
{
while(1)
{
char str[10];
int result;
result = read(STDIN_FILENO, str, sizeof str);
if (result <= 0)
{
if(result == -1)
perror("read");
close(first[1]);
return NULL;
}
if(write(first[1], str, result) != result)
{
perror("write");
exit(3);
}
printf("Reader: %s\n", str);
}
}
void *output(void *ptr)
{
int result;
char str[10];
while(1)
{
result = read(first[0], str, sizeof str);
if(result <= 0)
{
if(result == -1)
perror("read");
close(first[0]);
return NULL;
}
if (write(STDOUT_FILENO, str, result) != result) {
perror("write");
exit(4);
}
}
}
int main()
{
pthread_t t1, t2;
pipe(first);
...
}
void*输入(void*ptr)
{
而(1)
{
char-str[10];
int结果;
结果=读取(标准文件号、str、str大小);
如果(结果)没有创建管道的代码。请参阅man 3管道
。还应同步对共享资源的并发访问(first
此处)。没有创建管道的代码。请参阅man 3管道
。还应同步对共享资源的并发访问(first
此处)应同步。没有创建管道的代码。请参阅man 3管道
。还应同步对共享资源的并发访问(first
此处)。没有创建管道的代码。请参阅man 3管道
。还应同步对共享资源的并发访问(first
此处)。