posix

How to configure CMakeList in Clion ide for using POSIX pthread functions?

二次信任 提交于 2020-01-05 05:28:13
问题 I tried to compile a simple POSIX example in CLIon ide, but it doesn`t know about pthread library, I think... Here is the code: void *func1() { int i; for (i=0;i<10;i++) { printf("Thread 1 is running\n"); sleep(1); } } void *func2() { int i; for (i=0;i<10;i++) { printf("Thread 2 is running\n"); sleep(1); } } int result, status1, status2; pthread_t thread1, thread2; int main() { result = pthread_create(&thread1, NULL, func1, NULL); result = pthread_create(&thread2, NULL, func2, NULL); pthread

Regarding checking for file or directory

霸气de小男生 提交于 2020-01-05 05:12:06
问题 I have a very simple program here, but it seems to be returning a "true" value to the query S_ISDIR() even when the directory entry is not a directory. Can any one pleeas help me. I am using QNX Neurtion RTOS #include <stdio.h> #include <dirent.h> #include <sys/stat.h> #include <errno.h> int main(int argc, char *argv[]) { DIR *dir; struct dirent *entry; struct stat eStat; char *root; int i; root = argv[1]; while((entry = readdir(dir)) != NULL) { lstat(entry->d_name, &eStat); if(S_ISDIR(eStat

Regarding checking for file or directory

好久不见. 提交于 2020-01-05 05:11:55
问题 I have a very simple program here, but it seems to be returning a "true" value to the query S_ISDIR() even when the directory entry is not a directory. Can any one pleeas help me. I am using QNX Neurtion RTOS #include <stdio.h> #include <dirent.h> #include <sys/stat.h> #include <errno.h> int main(int argc, char *argv[]) { DIR *dir; struct dirent *entry; struct stat eStat; char *root; int i; root = argv[1]; while((entry = readdir(dir)) != NULL) { lstat(entry->d_name, &eStat); if(S_ISDIR(eStat

synchronization between processes using unnamed semaphores

雨燕双飞 提交于 2020-01-04 05:55:44
问题 In process-1 I am trying to write the data into shared memory. At the same time in process-2 I am reading the data from the same shared memory. in this case I need to provide synchronization between these two processes. if I will go through unnamed semaphores (using shm_init(),mmap() ),will it work or not? I have written code like this will it work or not? fd = shm_open("shm_name", O_CREAT| O_RDWR, S_IRUSR | S_IWUSR); sema = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE,MAP_SHARED , fd, 0)

Posix threads:Signal a thread that is running in while loop

為{幸葍}努か 提交于 2020-01-04 05:26:25
问题 I have a thread that is a very simple: it sends keep-alive packets to a server, then sleeps. I would like my main thread to signal the thread to quit, but whenever I call pthread_kill() it seems to crash my process. I tried to call sigpending() followed by sigismember() at the beginning of my while loop, but I think it is crashing because it doesn't know of any handle to take care of the signal (I was using sigaddset() before the while loop), as follow: void* foo(void* arg) { sigset_t set,

How to fork process without inheriting handles?

我的未来我决定 提交于 2020-01-04 04:43:26
问题 In my C/C++ server application which runs on Mac (Darwin Kernel Version 10.4.0) I'm forking child processes and want theses childes to not inherit file handles (files, sockets, pipes, ...) of the server. Seems that by default all handles are being inherited, even more, netstat shows that child processes are listening to the server's port. How can I do such kind of fork? 回答1: Normally, after fork() but before exec() one does getrlimit(RLIMIT_NOFILE, fds); and then closes all file descriptors

Why does access(2) check for real and not effective UID?

梦想与她 提交于 2020-01-04 04:13:32
问题 I noticed the hard way that the access(2) system call uses the real and not effective user ID for the access control check. While this is in line with what the access(2) man page says on Linux, it still makes little sense to me... For example, a setuid root program would run under the effective UID of root and real UID of whoever ran the program..now, the program would be able to open /etc/shadow with open(2), but the call access("/etc/shadow", R_OK); would fail with EACCESS Can anyone

pthread mutex locking variables used in statements

[亡魂溺海] 提交于 2020-01-03 20:16:53
问题 First of all, I have a gut feeling that says, inside an if statement, if I am using the variable, it counts as reading the variable so I should lock it with mutex there also (if another pthread might be doing stuff with it). Am I right that I should lock it? The example situation in simplified manner is given below. in one thread I am using the below statement: if(event){ // Should I or should I not lock event here to use it // inside if statement? pthread_mutex_lock(&mutex_event); event = 0;

Unable to Read From Serial Device After Unplugging and Replugging Connector

主宰稳场 提交于 2020-01-03 20:05:35
问题 I'm have a Linux application that is supposed to read from serial device /dev/ttyS0 . The serial device is opened in the following manner: // Open the serial port if((serial_device = open("/dev/ttyS0", O_RDWR | O_NOCTTY)) < 0){ fprintf(stderr, "ERROR: Open\n"); exit(EXIT_FAILURE); } // Get serial device attributes if(tcgetattr(serial_device,&options)){ fprintf(stderr, "ERROR: Terminal Get Attributes\n"); exit(EXIT_FAILURE); } cfsetspeed(&options,speed); // Set I/O baud rates cfmakeraw(

Unable to Read From Serial Device After Unplugging and Replugging Connector

筅森魡賤 提交于 2020-01-03 20:05:34
问题 I'm have a Linux application that is supposed to read from serial device /dev/ttyS0 . The serial device is opened in the following manner: // Open the serial port if((serial_device = open("/dev/ttyS0", O_RDWR | O_NOCTTY)) < 0){ fprintf(stderr, "ERROR: Open\n"); exit(EXIT_FAILURE); } // Get serial device attributes if(tcgetattr(serial_device,&options)){ fprintf(stderr, "ERROR: Terminal Get Attributes\n"); exit(EXIT_FAILURE); } cfsetspeed(&options,speed); // Set I/O baud rates cfmakeraw(