Tag: posix

从程序集调用C / C ++函数(OSX Mavericks x64)

这是一个奇怪的问题,我似乎无法找到答案。 这个: #include using namespace std; void show_number(int number) { cout << number << endl; // Shows '10' as expected } int main() { cout << endl; // Remove this and it fails __asm { mov rdi, 10 call show_number } } 实际上工作正常,除非你删除初始cout << endl ( main第一行)。 当你删除它时, show_number的cout似乎由于某种原因导致段错误。 是什么导致这个? (OSX Mavericks x64,但我认为应该在linux中工作)

仅列出常规文件(没有目录)问题

你知道为什么这个程序没有列出某些文件,即使它们是“常规”的吗?: #include #include #include #include #include int main(void) { DIR *dh = opendir(“./”); // directory handle struct dirent *file; // a ‘directory entity’ AKA file struct stat info; // info about the file. while (file = readdir(dh)) { stat(file->d_name, &info); printf(“note: file->d_name => %s\n”, file->d_name); printf(“note: info.st_mode => %i\n”, info.st_mode); if (S_ISREG(info.st_mode)) printf(“REGULAR FILE FOUND! […]

彼得森算法的错误实现?

我试图学习一些关于并行编程的东西,所以我尝试实现Peterson的算法,这是一个简单的例子,其中一个共享计数器增加2个线程。 我知道彼得森因等待繁忙而不是最佳,但我只是出于学习原因尝试过。 我认为这段代码的关键部分是在线程函数add中共享counter递增的地方。 所以我在计数器递增之前调用enter_section函数,之后调用leave_function 。 这部分错了吗? 我对关键部分的评估错了吗? 问题是当这两个线程完成时,计数器有时会给出一个不可预见的值。 它必须是线程之间的同步问题,但我只是看不到它…感谢您的帮助。 #include #include #include int counter; /* global shared counter */ int flag[2] = {0, 0}; /* Variables for Peterson’s algorithm */ int turn = 0; typedef struct threadArgs { pthread_t thr_ID; int num_of_repeats; int id; } THREADARGS; void enter_section (int thread) { int other = 1 […]

在C上正确使用Stat

为什么这样做: char *fd = “myfile.txt”; struct stat buf; stat(fd, &buf); int size = buf.st_size; printf(“%d”,size); 但这不起作用: char *fd = “myfile.txt”; struct stat *buf; stat(fd, buf); int size = buf->st_size; printf(“%d”,size);

套接字选项是否从侦听套接字inheritance到accept()?

假设传递给accept的侦听套接字使用setsockopt在其上设置了非默认选项。 这些选项(部分或全部?)是由接收连接的结果文件描述符inheritance的吗?

是否可以(并且安全)使接受套接字无阻塞?

我正在寻找一种方法来中断阻塞套接字上的accept()调用。 使用信号不是一种选择,因为这意味着在库中,我不想混淆用户信号。 使用select()是另一种选择,因为各种原因buf在我的情况下不是很吸引人。 如果可能,最好的方法是将套接字设置为非阻塞模式(使用fcntl()和O_NONBLOCK )来自另一个线程,而套接字在accept()调用时被阻塞。 预期的行为是accept()调用将在errno使用EAGAIN或EWOULDBLOCK返回。 它真的会这样吗? 安全吗? 便携式? 如果你知道这个方法对Windows的适用性(你需要使用WSAIoctl()和FONBIO ),我也很感兴趣。

realpath函数示例(C编程)

我正在寻找一个如何在C程序中使用realpath函数的示例。 我似乎无法在网上或我的任何C编程书中找到一个。

设置读取标准输入的超时时间

有没有办法超时stdin读取,以免程序挂起太长时间? read(0, var, numberofbytes);

如何干净地中断recv调用中的线程阻塞?

我有一个用C编写的multithreading服务器,每个客户端线程看起来像这样: ssize_t n; struct request request; // Main loop: receive requests from the client and send responses. while(running && (n = recv(sockfd, &request, sizeof(request), 0)) == sizeof(request)) { // Process request and send response. } if(n == -1) perror(“Error receiving request from client”); else if(n != sizeof(act)) fprintf(stderr, “Error receiving request from client: Incomplete data\n”); […]

posix regcomp和regexec线程是否安全? 具体来说,在GNU libc上?

这里有两个独立的问题:我可以在没有锁定的multithreading程序中使用正则表达式,如果是这样,我可以在多个线程中同时使用相同的regex_t吗? 我无法在Google或联机帮助页上找到答案。