首页 \ 问答 \ 管道无法通过pthread_t工作(pipe not working through pthread_t)

管道无法通过pthread_t工作(pipe not working through pthread_t)

我现在正试图通过两个线程(主线程和第二个线程)之间的管道发送一些数据,但我得到关于文件描述符的错误9。 我认为在涉及线程时文件描述符是重复的,但似乎并非如此。 它通常会返回“从bf hi读取”,但它不会。 你能帮我解决这个问题吗? 谢谢。 这是代码

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

pthread_mutex_t mutex;
int tube[2];

void * fonction(){
    if(close(tube[0])==-1){
        perror("close error \n");
        exit(EXIT_FAILURE);
    }
    printf("son \n");
    if(write(tube[1],"hi",2)<0){
        perror("write error \n");
        exit(EXIT_FAILURE);
    }
    printf("errno %d \n",errno);
    pthread_exit(NULL);
}

int main(){
    pthread_t a;
    if(pipe(tube)==-1){
        perror("pipe error \n");
        exit(EXIT_FAILURE);
    }
    char buffer[2];
    pthread_mutex_init(&mutex,NULL);
    close(tube[1]);
    pthread_create(&a,NULL,fonction,&tube[1]);
    pthread_join(a,NULL);
    read(tube[0],buffer,2);
    printf("read from bf %s \n",buffer);
    return EXIT_SUCCESS;
}

Ps:正如timrau在这里建议的那样(Pthread_t 没有启动 )我使用pthread_join,但我想这不是正确的方法


I'm now trying to send some data through a pipe between two thread (the main thread and a second one) but I get the errno 9 about the file descriptor. I thought file descriptor were duplicated when thread are involved but it seems it's not the case here. It would normally return "read from bf hi" but it won't. Can you help me to resolve this issue ? Thanks. Here is the code

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

pthread_mutex_t mutex;
int tube[2];

void * fonction(){
    if(close(tube[0])==-1){
        perror("close error \n");
        exit(EXIT_FAILURE);
    }
    printf("son \n");
    if(write(tube[1],"hi",2)<0){
        perror("write error \n");
        exit(EXIT_FAILURE);
    }
    printf("errno %d \n",errno);
    pthread_exit(NULL);
}

int main(){
    pthread_t a;
    if(pipe(tube)==-1){
        perror("pipe error \n");
        exit(EXIT_FAILURE);
    }
    char buffer[2];
    pthread_mutex_init(&mutex,NULL);
    close(tube[1]);
    pthread_create(&a,NULL,fonction,&tube[1]);
    pthread_join(a,NULL);
    read(tube[0],buffer,2);
    printf("read from bf %s \n",buffer);
    return EXIT_SUCCESS;
}

Ps : as timrau suggested here (Pthread_t not starting) I use pthread_join but I guess it's not the right way to do it


原文:https://stackoverflow.com/questions/37170117
更新时间:2023-06-27 13:06

最满意答案

最终,您将无法绕过您无法控制的硬编码路径,例如网络共享路径。 您可以而且应该避免的是在文档中对这些路径进行硬编码。

相反,将它们转移到配置文件和/或环境变量(同样,它们将由配置文件控制,与.bashrc和类似文件一起控制)。 然后使用最简单的方法

network_share_path = Sys.getenv('NETWORK_SHARE_PATH',
                                stop('no network share path configured'))
file.copy(from = network_share_path, to = 'subfolder/draft.docx', overwrite = TRUE)

Ultimately you won’t get around hard-coding paths that are outside your control, such as paths to network shares. What you can and should avoid is hard-coding these paths in your documents.

Instead, relegate them to configuration files and/or environment variables (which, again, will be controlle by configuration files, to with .bashrc and similar). The simplest approach is then to use

network_share_path = Sys.getenv('NETWORK_SHARE_PATH',
                                stop('no network share path configured'))
file.copy(from = network_share_path, to = 'subfolder/draft.docx', overwrite = TRUE)

相关问答

更多
  • 有一个“:”前缀,允许您为库指定不同的名称。 如果你使用 g++ -o build/bin/myapp -l:_mylib.so other_source_files 应搜索_mylib.so的路径。 There is the ":" prefix that allows you to give different names to your libraries. If you use g++ -o build/bin/myapp -l:_mylib.so other_source_files shou ...
  • 首先,你的时间是以高开销的方式完成的。 你应该使用它: def hardcoded(): m = [0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,6,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,5,0,1,0,2,0, ...
  • 最终,您将无法绕过您无法控制的硬编码路径,例如网络共享路径。 您可以而且应该避免的是在文档中对这些路径进行硬编码。 相反,将它们转移到配置文件和/或环境变量(同样,它们将由配置文件控制,与.bashrc和类似文件一起控制)。 然后使用最简单的方法 network_share_path = Sys.getenv('NETWORK_SHARE_PATH', stop('no network share path configured')) file ...
  • 它是否硬编码? 是。 也就是说,你需要权衡一些因素。 首先,考虑声明和跟踪各种常量所需的附加代码的“默认”属性名称的可能性。 另一件要考虑的是一致性。 如果你有其他地方的属性名称可能会改变,那么你会想要使用所有的常量。 另一方面,对于XML或“编码”使用常量是浪费时间,因为这些是众所周知的/明确定义的项目...... 另一方面,是错别字的可能性。 当你使用一个常量时,你有编译时支持来确保你所说的任何地方“DEFAULTPROPERTY”在任何地方或任何地方都是正确的。 而使用字符串处理方式意味着问题可能不会 ...
  • 只需对您的代码进行一次小调整或2次修复就可以解决问题。 我评论了你的旧代码,这样你就可以看到我改变了什么。 您不想像保存那样在保存时指定文件格式,因为如果您这样做,它将始终提示您更改版本时的兼容性问题。 保持空白,它将默认为工作表已经存在的版本。您可以在NewFile =之后编辑C:\,无论您需要什么,只需将其保留在引号中即可。 或者,您可以更改excel的默认保存位置,但这不是VBA修复。 Option Explicit Sub SaveWorkbookAsNewFile() Dim ActShe ...
  • 这听起来像你的DLL依赖于你的开发机器上存在的其他DLL,但不在目标机器上。 当操作系统加载您的DLL但无法找到依赖的DLL时,它会报告相同的“无法找到文件”错误,从而使您认为无法找到您的DLL。 如果是这种情况,那么您需要分发这些额外的DLL或删除它们的依赖关系。 It sounds like your DLL has dependancies on other DLLs that exist on your development machine but not on the target machin ...
  • 简短的回答,没有。 多头回答,你可能会让它像你想要的那样工作。 您可以创建自动前置文件以使用$_SERVER['DOCUMENT_ROOT'] var来确定使用switch / if-else构造包含哪个文件。 或者您可以将__FILE__ var与realpath结合使用以包含/要求所需的“prepend”文件。 自动prepend.php require_once(realpath(__FILE__) . '/prepend.php'); //编辑: 再考虑一下, __FILE__可能会引用前置文件而不 ...
  • 您可以通过使用使用Objective C概念而不是原始C的帮助程序库来删除样板代码; 在SQLite的情况下,像FMDB 。 当您发现重复编写相同的代码时,您应该将公共代码分解为辅助函数(/ class / etc.); 要像这样调用的东西: update_table_record(database, @"tableA", @{ @"att0" : input0 }, // record ID @{ @"attA1" : i ...
  • 一个解决方案是在你的jar中包含.exe(不要忘记将它包含在你的构建路径中)。 然后,在您的应用程序中,您将使用getResourceAsStream("path_to_your_exe_file")从jar中读取.exe的内容。 然后在临时目录中创建exe文件的副本。 请参阅Apache commons IO 。 使用FileUtils.copy(in, out) 。 然后你可以使用旧的System.setProperty("webdriver.chrome.driver",C:\\Users\\kday ...
  • NLua可以完成您想要的任何操作,并且将其实施到您的项目中非常容易。 Lua lua = new Lua(); lua.DoString("return 'Hello World!'"); 您还可以注册DotNet函数以在脚本文件中使用。 lua.RegisterFunction("print", this, typeof(Program).Print("Print")); 之后,你可以在你的.lua文件中使用它: function Run() print("Hello World!") end ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。