posix

What does #define _POSIX_SOURCE mean?

你离开我真会死。 提交于 2020-01-03 17:17:12
问题 If I use #define _POSIX_SOURCE in C so then what advantage do i get in my program. Can I access more libraries in my program or I can call some functions which are present directly in C. I mean what does this mysterious macros means after all? 回答1: It allows you to use function that are not part of the standard C library but are part of the POSIX.1 (IEEE Standard 1003.1) standard. Using the macros described in feature_test_macros allows you to control the definitions exposed by the system

Does sleep/nanosleep work by utilizing a busy wait scheme?

故事扮演 提交于 2020-01-03 09:56:40
问题 I am wondering how is sleep/nanosleep internally implemented? Consider this code: { // on a thread other than main() thread while(1) { //do something sleep(1); } } would the CPU be doing constant context switching to check if sleep of 1 sec is done (i.e. an internal busy wait). I doubt it works this way, too much inefficiency. But then how does it work? Same question applies to nanosleep. Note: If this is implementation/OS specific, then how can I possibly implement a more efficient scheme

Open file for read/write, create if needed

杀马特。学长 韩版系。学妹 提交于 2020-01-03 08:15:13
问题 What is the most elegant way to open a file such that the file gets created if it does not exist, the file won't be truncated if it does exist and it is possible to write any part of the file (after seeking), not just the end? As far as I can tell, the open builtin doesn't seem up to the task: it provides various modes, but every one I tried fails to satisfy at least one of my requirements: r+ fails if the file does not exist. w+ will truncate the file, losing any existing content. a+ will

how to use settimeofday(2)?

隐身守侯 提交于 2020-01-03 06:04:13
问题 What am I doing wrong here? I expect settimeofday() to change the system time, not return EINVAL . $ uname -a Linux io 4.3.5-300.fc23.x86_64 #1 SMP Mon Feb 1 03:18:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux $ cat settimeofday.c #include <sys/time.h> #include <stdio.h> int main() { struct timeval tv = {0, 0}; if (settimeofday(&tv, 0) == -1) perror("settimeofday"); } $ gcc settimeofday.c $ sudo ./a.out settimeofday: Invalid argument The error is coming from a Thinkpad T450 running Fedora 23.

Unexpected output in pthread

怎甘沉沦 提交于 2020-01-03 05:13:51
问题 Hello for above code in thread it is displaying 0 (tid = 0) instead of 8... what may be the reason ? In PrintHello function I am printing threadid but I am sending value 8 but it is printing 0 as output #include <pthread.h> #include <stdio.h> #include <stdlib.h> void *PrintHello(void *threadid) { int *tid; tid = threadid; printf("Hello World! It's me, thread #%d!\n", *tid); pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t thread1,thread2; int rc; int value = 8; int *t; t =

Unexpected output in pthread

邮差的信 提交于 2020-01-03 05:13:18
问题 Hello for above code in thread it is displaying 0 (tid = 0) instead of 8... what may be the reason ? In PrintHello function I am printing threadid but I am sending value 8 but it is printing 0 as output #include <pthread.h> #include <stdio.h> #include <stdlib.h> void *PrintHello(void *threadid) { int *tid; tid = threadid; printf("Hello World! It's me, thread #%d!\n", *tid); pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t thread1,thread2; int rc; int value = 8; int *t; t =

Should I delete pointers that come from other functions or class methods?

杀马特。学长 韩版系。学妹 提交于 2020-01-03 03:27:07
问题 I have a background in Java and I'm still not fully used to the concept of pointers and scope, so sorry if the question seems silly. I read somewhere that I should delete pointers that I have allocated on the Heap. I understand that but should I also delete a pointer that is given to me like this: #include<dirent.h> DIR* dir; struct dirent* entries; dir= opendir("D:/DIR") entries= readdir(entries) // Should I delete the pointers after I'm done with them? delete entries; delete dir; Should I

What is the official standard for pthreads?

穿精又带淫゛_ 提交于 2020-01-02 20:22:19
问题 I am trying to find the document that specifies the standard for pthreads. I've seen various links which point to IEEE 1003.1c-1995 (i.e. Wikipedia or OpenGroup). However when I searched for this document on the IEEE standards site I eventually found this page which said "Superseded Standard." The IEEE page for 1003.1c-1995 did have a note that said: "Abstract not available. See ISO/IEC 9945-1." Searching for that on Google led me to a page for ISO/IEC 9945-1:1996 but the status said

What non-Linux unixes support openat()?

喜你入骨 提交于 2020-01-02 08:02:53
问题 openat() was added to POSIX in the POSIX.1-2008 revision, and has been supported by Linux since 2.6.16. How is support on non-Linux UNIXes? eg, Darwin, the *BSDs, and proprietary UNIXes. 回答1: Dragonfly BSD supports it, the rest of the BSDs don't. Solaris of course does. 回答2: Unlike the top answer is saying, all major BSDs and Apple's OS X seem to support it as of today: DragonFly since DragonFly 2.3. FreeBSD since FreeBSD 8.0. Linux since Linux 2.6.16 (for completeness). NetBSD since NetBSD 7

Persistent execvp on pipe?

此生再无相见时 提交于 2020-01-02 07:08:53
问题 I am working on an assignment for my Operating System class (Posix & C), building a mini-shell, and I don't know how to solve the following problem: My mini-shell has to accept two commands, for example ls | grep a . For that I create a pipe of size two and a child. The child closes all that it has to close and opens all that it has to open (standard/pipe's in & out). It then executes "ls," using execvp. I am sure this works well. After that, the parent shuts and opens inputs and outputs (I