posix

Race condition when using dup2

人盡茶涼 提交于 2019-12-20 10:47:09
问题 This manpage for the dup2 system call says: EBUSY (Linux only) This may be returned by dup2() or dup3() during a race condition with open(2) and dup(). What race condition does it talk about and what should I do if dup2 gives EBUSY error? Should I retry like in the case of EINTR ? 回答1: There is an explanation in fs/file.c , do_dup2(): /* * We need to detect attempts to do dup2() over allocated but still * not finished descriptor. NB: OpenBSD avoids that at the price of * extra work in their

What is the difference between NPTL and POSIX threads?

耗尽温柔 提交于 2019-12-20 10:21:15
问题 What is the basic difference between NPTL and POSIX threads? How have these two evolved? 回答1: POSIX threads (pthread) is not an implementation, it is a API specification (a standard, on paper, in english) of several functions whose name starts with pthread_ and which are defined in <pthread.h> header. POSIX is also a set of specifications. NPTL is now inside GNU Libc on Linux and is (or at least tries very hard to be) an implementation of POSIX threads. It is a bunch of source and binary code

When to use the POLLOUT event of the poll C function?

老子叫甜甜 提交于 2019-12-20 10:13:09
问题 I wrote a small TCP servers with socket() + POLLIN poll() + recv() + send() , but I don't know when to use POLLOUT poll or select writefds to poll on writable event. Can anyone give me an example of the real usage of POLLOUT ? 回答1: The usual pattern is to use non-blocking file descriptors with poll() like this: When getting ready to poll() , Always set POLLIN because you are always interested in reading what the other end of the socket has send you. Except if you have a large backlog of

POSIX Headers (from MinGW project) in Visual Studio 2013

感情迁移 提交于 2019-12-20 06:10:01
问题 (Continues from: Porting from Code::Blocks to Visual Studio 2010 - I'll remove this if it's against StackExchange's formatting rules) I'm trying to port an open source Code::Blocks project, which originally uses MinGW and GCC TDM-1 4.7.2 (does not support newer versions), to Visual Studio 2013 (NOTE: I switched from 2010 to 2013 for the question above). The code uses 1998 ISO C++ standard and uses various additional utilities like SDL2 , OpenGL , Zlib , Lua , and others. The main problem is,

If I have only the physical address of device buffer (PCIe), how can I map this buffer to user-space?

佐手、 提交于 2019-12-20 05:49:19
问题 If I have only the physical address of the memory buffer to which is mapped the device buffer via the PCI-Express BAR (Base Address Register), how can I map this buffer to user-space ? For example, how does usually the code should look like in Linux-kernel? unsigned long long phys_addr = ...; // get device phys addr unsigned long long size_buff = ...l // get device size buff // ... mmap(), remap_pfn_range(), Or what should I do now? On: Linux x86_64 From: https://stackoverflow.com/a/17278263

C, clock_gettime, returned incorrect nanosecond value?

穿精又带淫゛_ 提交于 2019-12-20 04:56:47
问题 I'm writing a simple program, which checks if elapsed time is more than 1 seconds. I take start time with a clock_gettime(), then I call sleep(5), take new time and I check if the difference is bigger than 1; I sleep 5 seconds, then it should be greater than 5, but my program prints a strange result. this is the code: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> int main() { struct timespec tv1,tv3,expected; struct timespec nano1; tv1.tv_nsec = 0; tv3.tv_nsec =

C, clock_gettime, returned incorrect nanosecond value?

筅森魡賤 提交于 2019-12-20 04:56:22
问题 I'm writing a simple program, which checks if elapsed time is more than 1 seconds. I take start time with a clock_gettime(), then I call sleep(5), take new time and I check if the difference is bigger than 1; I sleep 5 seconds, then it should be greater than 5, but my program prints a strange result. this is the code: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> int main() { struct timespec tv1,tv3,expected; struct timespec nano1; tv1.tv_nsec = 0; tv3.tv_nsec =

Go back to Previous Directory in Linux using a C program

旧街凉风 提交于 2019-12-20 04:38:28
问题 I am in the directory /home/destination I need to go back to the /home directory. Any ideas on how to implement this using a C-program? 回答1: A program can only change its own environment . Thus, the program can chdir but it will not change the current directory of the parent. That's why cd can't be implemented as an external command. 回答2: You can use the chdir function for this: chdir(".."); /* change current working directory, go one level up */ 回答3: If you'd like to go level up chdir("..");

Is there a way to ensure atomicity while having a multithreaded program with signal handlers?

拥有回忆 提交于 2019-12-20 04:29:04
问题 If I have a program like this (in pseudocode): mutex_lock; func() { lock(mutex_lock); // Some code (long enough to make a // race condition if no proper synchronisation // is available). We also going to call a signal, // say, SIGINT, through (ctrl-c), while we are in // the range of locking and unlocking the lock. unlock(mutex_lock); } sig_handler_func(sig) { // Say, we are handling SIGINT (ctrl-c) signal // And we need to call func from here too. if (sig == SIGINT) { func(); } } main() { //

Is it possible to rescue file descriptor from FILE*?

陌路散爱 提交于 2019-12-20 04:24:34
问题 I have to use a certain cross-platform library which passes FILE* objects around. I get a file descriptor from another source (inherited), I want to keep same fd across fork 'd processes. I currently use fdopen to convert a file descriptor to a FILE* object. My problem is that fclose used to clean up FILE* objects closes connected file descriptor. I would very much like to keep this file descriptor after it has been used. is there a way rescue file descriptor from FILE* ? Is there a way to