天道酬勤,学无止境

c

How to open a file with it's relative path in Linux?

I have a program which opens a file by using a relative path (for instance '..'). Now the problem is, when I execute the program from another directory, the relative path is not relative to the program but relative to the working directory. Thus, if I start the program with '/path/to/program/myprog' it fails to find the file. Is there a way to execute the program independently of the working directory? Id est, as If the working directory were the directory where the program is located? Or am I just thinking in a too complicated way and there is a much easier way to refer to a file, which

2021-06-14 16:24:30    分类:问答    c++   c   linux   path

Recursive Function

Given the following recursive function: // Pre-condition: y is non-negative. int mysterious(int x, int y) { if (y == 0) return x; return 2*mysterious(x, y-1); } What is the return value of mysterious(3, 2)? Here is my call stack: return 2*mysterious(3, 2-1) => 2*3 => 6, 2*1 => mysterious(6,2) return 2*mysterious(6, 2-1) => 6*2 => 12, 2*2 => mysterious(12, 2) But it seems like y will never reach 0. What am I doing wrong?

2021-06-14 16:16:56    分类:问答    c   recursion

Is there a bit-wise trick for checking the divisibility of a number by 2 or 3?

I am looking for a bit-wise test equivalent to (num%2) == 0 || (num%3) == 0. I can replace num%2 with num&1, but I'm still stuck with num%3 and with the logical-or. This expression is also equivalent to (num%2)*(num%3) == 0, but I'm not sure how that helps.

2021-06-14 16:03:40    分类:问答    java   python   c++   c   bit-manipulation

Cythonizing fails because of unknown type name 'uint64_t'

This may be a newbie problem. I can't cythonize a simple helloworld.pyx tutorial script while the exact same code works on linux: print("hello world") Here is the setup.py script: from distutils.core import setup from Cython.Build import cythonize setup(ext_modules = cythonize('helloworld.pyx')) But I get this after running python setup.py build_ext --inplace: running build_ext building 'helloworld' extension creating build creating build/temp.macosx-10.7-x86_64-3.6 gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/haotian

2021-06-14 16:02:42    分类:问答    python   c   cython

How does “get_user_pages” work (For linux driver)

Working on a Linux PCI driver, now I'm trying to write codes for DMA using scatter/gather. For now, I've learned that to access to DMA datas directly from User space, we need to pin user space pages to kernel space. And to do this, we have get_user_pages, its full definition is like: int get_user_pages(struct task_struct * tsk, struct mm_struct * mm, unsigned long start, int nr_pages, int write, int force, struct page ** pages, struct vm_area_struct ** vmas); My first question is about the struct page ** pages. Here do we need to allocate memory(using kcalloc for ex.) for the pages before

2021-06-14 15:51:15    分类:问答    c   linux   linux-kernel   linux-device-driver   kernel-module

best way in linux to get usb device added notifications in c

what Linux library should i use to get asynchronous notifications of added usb devices? I'd like my program to work on any Linux device so I'll need to use a library that isn't specific to one Linux version

2021-06-14 15:30:19    分类:问答    c   linux   usb

Are bitwise operations still practical?

Wikipedia, the one true source of knowledge, states: On most older microprocessors, bitwise operations are slightly faster than addition and subtraction operations and usually significantly faster than multiplication and division operations. On modern architectures, this is not the case: bitwise operations are generally the same speed as addition (though still faster than multiplication). Is there a practical reason to learn bitwise operation hacks or it is now just something you learn for theory and curiosity?

2021-06-14 15:29:47    分类:问答    c   bit-manipulation   bitwise-operators

Using open() to create a file in C

This is the first time I am using open() from: #include <fcntl.h> I am trying to create two files: int fd; int fd2; char *tmpname = "./TMPFILE"; printf( "Temporary file created\n "); char *tmpname2 = "./TMPFILE2"; printf( "Temporary file two created\n "); fd = open(tmpname, O_WRONLY | O_APPEND); fd2 = open(tmpname2, O_WRONLY | O_APPEND); I am trying to create the files in the current working directory that can be written and appended to. This compiles and runs, but my concern is that when I check my directory to see if the files were created, they aren't listed. My question is does open() only

2021-06-14 15:22:27    分类:问答    c

Interactive console programming for c/c++?

So I've written a small program which takes in commands by the users, and then displays the output (after connecting to a server). It's an interactive console of sorts. However, after using the mongodb and redis command-line clients (which work interactively on the console/terminal), it seems that there must be a library somewhere which provides functionalities such as recording user inputs, accepting up/down keypresses to browse through command history, as well as tab completion framework (not sure how that one would work, but yeah). What's an ideal library to use for such a thing?

2021-06-14 15:12:50    分类:问答    c++   c   linux   console-application

Write/Read to/from FIFO files - linux

I've been trying to wrap my head around FIFO, and came up with a simple program of server and client. I'm not trying to do anything fancy, just to have one process that will play a role of 'server', this process will 'listen' to any messages delivered by another process; the client. Here's what I wrote: server.c #include<stdio.h> #include <fcntl.h> #include <stdlib.h> #define INGOING "clientToServer.fifo" #define BUFFER 200 int main(int argc, char *argv[]) { char in[BUFFER]; mkfifo(INGOING, 0666); printf("Welcome to server.\n"); printf("channel for sending messages to server is %s\n", INGOING)

2021-06-14 15:12:37    分类:问答    c   linux   posix   fifo