posix

How to delete a directory and its contents in (POSIX) C? [duplicate]

心已入冬 提交于 2019-12-17 15:39:57
问题 This question already has answers here : Removing a non empty directory programmatically in C or C++ (9 answers) Closed 2 years ago . I am most interested in the non-recursive case, but I am guessing others who might track this question would prefer seeing the recursive case. Basically, we are aiming to accomplish: rm -rf <target> However, a system call would be an immature answer. 回答1: You need to use nftw() (or possibly ftw()) to traverse the hierarchy. You need to use unlink() to remove

What is SEGV_MAPERR?

让人想犯罪 __ 提交于 2019-12-17 15:22:06
问题 What is SEGV_MAPERR , why does it always come up with SIGSEGV ? 回答1: There are two common kinds of SEGV, which is an error that results from an invalid memory access: A page was accessed which had the wrong permissions. E.g., it was read-only but your code tried to write to it. This will be reported as SEGV_ACCERR. A page was accessed that is not even mapped into the address space of the application at all. This will often result from dereferencing a null pointer or a pointer that was

why pthread causes a memory leak

我是研究僧i 提交于 2019-12-17 13:45:35
问题 Whenever I create a pthread, valgrind outputs a memory leak, For example the below code: #include <stdio.h> #include <unistd.h> #include <pthread.h> void *timer1_function (void *eit){ (void) eit; printf("hello world\n"); pthread_exit(NULL); } int main(void){ pthread_t timer1; pthread_create( &timer1, NULL, timer1_function, NULL); ///////line13 int i=0; for(i=0;i<2;i++){usleep(1);} return 0; } valgrind outputs ==1395== HEAP SUMMARY: ==1395== in use at exit: 136 bytes in 1 blocks ==1395== total

why pthread causes a memory leak

ぃ、小莉子 提交于 2019-12-17 13:45:23
问题 Whenever I create a pthread, valgrind outputs a memory leak, For example the below code: #include <stdio.h> #include <unistd.h> #include <pthread.h> void *timer1_function (void *eit){ (void) eit; printf("hello world\n"); pthread_exit(NULL); } int main(void){ pthread_t timer1; pthread_create( &timer1, NULL, timer1_function, NULL); ///////line13 int i=0; for(i=0;i<2;i++){usleep(1);} return 0; } valgrind outputs ==1395== HEAP SUMMARY: ==1395== in use at exit: 136 bytes in 1 blocks ==1395== total

What does the “s” mean in the structure?

三世轮回 提交于 2019-12-17 12:42:27
问题 Here's a simple question. What's the meaning of the leading letter " s " in the sin_family, sin_port, sin_addr and sin_zero? struct sockaddr_in { short int sin_family; // Address family, AF_INET unsigned short int sin_port; // Port number struct in_addr sin_addr; // Internet address unsigned char sin_zero[8]; // Same size as struct sockaddr }; Thanks. 回答1: This comes from Berkeley, back when LSD was still legal. So very obvious in their naming choices :/ All kidding aside, this dates back to

C++ error: undefined reference to 'clock_gettime' and 'clock_settime'

被刻印的时光 ゝ 提交于 2019-12-17 10:15:25
问题 I am pretty new to Ubuntu, but I can't seem to get this to work. It works fine on my school computers and I don't know what I am not doing. I have checked usr/include and time.h is there just fine. Here is the code: #include <iostream> #include <time.h> using namespace std; int main() { timespec time1, time2; int temp; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1); //do stuff here clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2); return 0; } I am using CodeBlocks as my IDE to build and run

OpenMP and CPU affinity

社会主义新天地 提交于 2019-12-17 09:43:09
问题 Will sched_setaffinity or pthread_attr_setaffinity_np work to set thread affinity under OpenMP? Related: CPU Affinity 回答1: Yes, named calls will work to set thread affinity. The only problem is to fix thread number and to set right affinity in right thread (you can try using static scheduling of for loop for known number of threads). As I know, almost every openmp allows to set affinity via environment. The name of environment variable varies (it was not standartized some time ago). I use

How do I find the current machine's full hostname in C (hostname and domain information)?

依然范特西╮ 提交于 2019-12-17 08:22:07
问题 In a C project (POSIX), how do I get the fully qualified name for the current system? For example, I can get just the hostname of my machine by doing gethostname() from unistd.h. This might give me machine3 in return, but I'm actually looking for machine3.somedomain.com for example. How do I go about getting this information? I do not want to use a call to system() to do this, if possible. 回答1: To get a fully qualified name for a machine, we must first get the local hostname, and then lookup

Recursive mkdir() system call on Unix

☆樱花仙子☆ 提交于 2019-12-17 07:11:13
问题 After reading the mkdir(2) man page for the Unix system call with that name, it appears that the call doesn't create intermediate directories in a path, only the last directory in the path. Is there any way (or other function) to create all the directories in the path without resorting to manually parsing my directory string and individually creating each directory ? 回答1: There is not a system call to do it for you, unfortunately. I'm guessing that's because there isn't a way to have really

How can I convert a file pointer ( FILE* fp ) to a file descriptor (int fd)?

徘徊边缘 提交于 2019-12-17 06:24:38
问题 I have a FILE * , returned by a call to fopen() . I need to get a file descriptor from it, to make calls like fsync(fd) on it. What's the function to get a file descriptor from a file pointer? 回答1: The proper function is int fileno(FILE *stream) . It can be found in <stdio.h> , and is a POSIX standard but not standard C. 回答2: Even if fileno(FILE *) may return a file descriptor, be VERY careful not to bypass stdio's buffer. If there is buffer data (either read or unflushed write), reads/writes