posix

Designing a Queue to be a shared memory

拈花ヽ惹草 提交于 2020-01-12 01:43:26
问题 I'm attempting to design/implement a (circular) queue (in C) as a shared memory so that it can be shared between multiple threads/processes. The queue structure is as follows: typedef struct _q { int q_size; int q_front; int q_rear; int *q_data; }queue; Which supports the following functions: int empty_q(queue *q); int display_q(queue *q); int create_q(queue **q, int size); int delete_q(queue **q); int enqueue(queue *q, int data); int dequeue(queue *q, int *data); As per the queue size

How do I create a global variable that is thread-specific in C using POSIX threads?

为君一笑 提交于 2020-01-11 17:07:11
问题 I am learning about POSIX threads and I have come to the section on Thread Specific Data. The book does an excellent example using a file descriptor. However, I wanted to do the same example on my own, except this time using a global variable. However, I am having some difficulty fully grasping this concept. What I want to do is the following: Create a global integer Declare a key for the global int in main: set global integer to be a value eg. 10 create a key for it without any clean up

Difference between EACCES and EPERM

懵懂的女人 提交于 2020-01-11 01:48:16
问题 What is the difference between EACCES and EPERM exactly? EPERM is described here as "not super user", but I would usually associate that with EACCES. In fact, I can't recall ever seeing an EPERM in real life. 回答1: EACCES is almost always used when the system call was passed a path that was inaccessible by the current user. EPERM is used in various other situations where you need to be root to perform an action, e.g. kill() on a process that you don't own link() on a directory reboot() 回答2:

shmat() is returning a different “shmaddr” for same “shmkey”

匆匆过客 提交于 2020-01-10 05:18:05
问题 Here's my setup... /* Bounded Buffer item structure */ struct item { int id; /* string index value */ char str[80]; /* string value */ }; /* Structure for the shared memory region */ typedef struct { int debug; /* debug flag */ int in; /* index of next empty slot */ int out; /* index of next full slot */ char MUTEXname[32]; /* name of the MUTEX semaphore */ char EMPTYname[32]; /* name of the EMPTY semaphore */ char FULLname[32]; /* name of the FULL semaphore */ struct item buff[BUFFSIZE]; /*

Is my shell broken?

℡╲_俬逩灬. 提交于 2020-01-07 11:29:05
问题 #!/bin/sh count=0 foo=0 echo "foo is $foo" while [ "$foo" -eq 0 ] && [ "$count" -lt 10 ]; do echo "inside while: foo is $foo" count=$((count+1)) foo=1 done echo "after while" You'd expect the script above to output the following, right? foo is 0 inside while: foo is 0 after while And it does on many other machines, like your own. But not mine ... foo is 0 inside while: foo is 0 inside while: foo is 1 inside while: foo is 1 ... (infinite loop) Am I doing something wrong, or am I missing

Is my shell broken?

℡╲_俬逩灬. 提交于 2020-01-07 11:27:41
问题 #!/bin/sh count=0 foo=0 echo "foo is $foo" while [ "$foo" -eq 0 ] && [ "$count" -lt 10 ]; do echo "inside while: foo is $foo" count=$((count+1)) foo=1 done echo "after while" You'd expect the script above to output the following, right? foo is 0 inside while: foo is 0 after while And it does on many other machines, like your own. But not mine ... foo is 0 inside while: foo is 0 inside while: foo is 1 inside while: foo is 1 ... (infinite loop) Am I doing something wrong, or am I missing

run a program in background with execvp system call in c

六月ゝ 毕业季﹏ 提交于 2020-01-07 05:45:13
问题 i'm writing a program that recieves a command name and arguments and optionally the string "bg" at the end , if the "bg" string is passed my program should execute the command with its arguments in background if not in foreground, here's my code: #include<sys/types.h> #include<sys/wait.h> #include<unistd.h> #include<stdio.h> #include<errno.h> #include <stdlib.h> #include <string.h> int main(int argc, char* argv[]) { pid_t pid; int state; if( (pid=fork())<0) { perror("\nError in fork"); exit(

usage of Unix getaddrinfo C function to start set the server

拟墨画扇 提交于 2020-01-06 12:37:47
问题 I am building a client-server application in C with the source code taken from the book Advanced Programming in Unix Environment. In the server it is doing the following: struct addrinfo hint; memset(&hint, 0, sizeof(hint)); hint.ai_flags = AI_CANONNAME; hint.ai_socktype = SOCK_STREAM; hint.ai_addr = NULL; hint.ai_next = NULL; .... if ((n = sysconf(_SC_HOST_NAME_MAX))<0) { n = HOST_NAME_MAX; } if((host = malloc(n)) == NULL) { printf("malloc error\n"); exit(1); } if (gethostname(host, n)<0) {

Can I with PTEs from one process which indicate to fragments of physical memory to create appropriate PTEs in other process?

末鹿安然 提交于 2020-01-06 03:14:48
问题 When we in Linux use function mmap (,,, MAP_ANON | MAP_SHARED); , then for the same region of fragmented physically memory (which allocated) between processes are allocating virtual memory pages (PTEs). Ie these PTEs are copied from page table of one process to the page table of another process (with the same sequence of fragments of physical addresses allocated memory), is this true? But mmap () needs to be done before fork () . And if we already have two working process (ie after fork () ),

Infinite pipe insanity

落花浮王杯 提交于 2020-01-05 08:58:38
问题 I'm trying to create an infinte set of pipes to traverse from the left process to the right process. I'm using a fd to keep the previous out fd and input it to the new process. Can anyone see where I'm going wrong. It should be pretty simple to see at this point. I documented well. //Keep the previous out fd for the in of the subsequent process int prev_out_fd; for (x = 0; x < prog_count; ++x) { //Create a pipe for both processes to share int pipefd[2]; if (x != prog_count -1) { pipe(pipefd);