posix

Why does start_routine for pthread_create return void* and take void*

我的未来我决定 提交于 2019-12-18 12:08:27
问题 The function header for pthread_create looks like this: int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg); I understand it all except that the function pointer for start_routine is of the form void* (*fpointer) (void*) which means it takes in a void pointer and returns a void pointer . The void pointer that it takes is just a way to pass in an argument to the start_routine, I get that part, but I don't understand why the function

How to convert a PCRE to a POSIX RE?

左心房为你撑大大i 提交于 2019-12-18 11:52:46
问题 This interesting question Regex to match anything (including the empty string) except a specific given string concerned how to do a negative look-ahead in MySQL. The poster wanted to get the effect of Kansas(?! State) because MySQL doesn't implement look-ahead assertions, a number of answers came up the equivalent Kansas($|[^ ]| ($|[^S])| S($|[^t])| St($|[^a])| Sta($|[^t])| Stat($|[^e])) The poster pointed out that's a PITA to do for potentially lots of expressions. Is there a script/utility

How to convert a PCRE to a POSIX RE?

。_饼干妹妹 提交于 2019-12-18 11:52:04
问题 This interesting question Regex to match anything (including the empty string) except a specific given string concerned how to do a negative look-ahead in MySQL. The poster wanted to get the effect of Kansas(?! State) because MySQL doesn't implement look-ahead assertions, a number of answers came up the equivalent Kansas($|[^ ]| ($|[^S])| S($|[^t])| St($|[^a])| Sta($|[^t])| Stat($|[^e])) The poster pointed out that's a PITA to do for potentially lots of expressions. Is there a script/utility

Cost of context switch between threads of same process, on Linux

三世轮回 提交于 2019-12-18 11:46:23
问题 Is there any good empirical data on the cost of context switching between threads of the same process on Linux (x86 and x86_64, mainly, are of interest)? I'm talking about the number of cycles or nanoseconds between the last instruction one thread executes in userspace before getting put to sleep voluntarily or involuntarily, and the first instruction a different thread of the same process executes after waking up on the same cpu/core. I wrote a quick test program that constantly performs

Is there a minimally POSIX.2 compliant shell?

六月ゝ 毕业季﹏ 提交于 2019-12-18 11:09:07
问题 Is there a minimally POSIX.2 compliant shell (let's call it mpcsh ) in the following sense: if mpcsh myscript.sh behaves correctly on my (compliant) system then xsh myscript.sh will behave identically for any POSIX.2 compliant shell xsh on any compliant system. ("Identically" up to less relevant things like the wording of error messages etc.) Does dash qualify? If not, is there any way to verify compliance of myscript.sh ? 回答1: The sad answer in advance It won't help you (not as much and

Correct use of Stat on C

断了今生、忘了曾经 提交于 2019-12-18 10:54:17
问题 Why does this work : char *fd = "myfile.txt"; struct stat buf; stat(fd, &buf); int size = buf.st_size; printf("%d",size); But this does not work: char *fd = "myfile.txt"; struct stat *buf; stat(fd, buf); int size = buf->st_size; printf("%d",size); 回答1: The reason for it not working is that buf in the first example is allocated on the stack. In the Second example you only have a pointer to a struct stat, pointing to anywhere (probably pointing to address 0x0, i.e. a NULL pointer), you need to

Detecting a chroot jail from within

吃可爱长大的小学妹 提交于 2019-12-18 10:36:08
问题 How can one detect being in a chroot jail without root privileges? Assume a standard BSD or Linux system. The best I came up with was to look at the inode value for "/" and to consider whether it is reasonably low, but I would like a more accurate method for detection. [edit 20080916 142430 EST] Simply looking around the filesystem isn't sufficient, as it's not difficult to duplicate things like /boot and /dev to fool the jailed user. [edit 20080916 142950 EST] For Linux systems, checking for

pthread_cond_wait and mutex requirement

穿精又带淫゛_ 提交于 2019-12-18 10:14:26
问题 Why it is required to lock a mutex before calling pthread_cond_wait ? Also, is it required to take a lock (on the same mutex) before calling pthread_cond_signal ? thanks for your help. 回答1: Why it is required to lock a mutex before calling pthread_cond_wait? Because otherwise there is an unavoidable race condition. A mutex protects shared state. A condition variable is associated with some predicate ("condition") on the state. The basic idea is that you want to: 1) check the predicate 2) if

Wrong implementation of Peterson's algorithm?

久未见 提交于 2019-12-18 09:06:00
问题 I was trying to learn something about parallel programming, so I tried to implement Peterson's algorithm for an easy example where one shared counter is incremented by 2 threads. I know that Peterson's isn't optimal due to busy waiting but I tried it only for study reasons. I supposed that the critical section of this code is in the thread function add where the shared counter is incremented. So i call the enter_section function before the counter incrementation and after it I called the

Close all File Handles when Calling posix_spawn

China☆狼群 提交于 2019-12-18 05:08:10
问题 I'd like to spawn a collection of processes using posix_spawn(...) (or something very similar). This function accept an argument of type posix_spawn_file_actions_t, which allows me specify how the open file handles should be treated. From what I can ascertain from the documentation, all files are inherited from the calling processes and modified according the information in the posix_spawn_file_actions_t structure. I want all files to be unopened by the spawned process (except for stdin,