system-calls

To sleep in C, should I use while with a clock, or a system call?

为君一笑 提交于 2019-12-22 11:43:11
问题 I was checking out clock() on cplusplus.com. Their example involves making the process wait for a second and then output a line, in a loop until 10 seconds have ellapsed. I need to do something similar in the homework assignment I'm working on. What I was wondering was this: if I just send my program into a loop, isn't that using system resources to just spin me around? In this case, wouldn't a system call be better? Thanks for your help! 回答1: Yes, using a system call or library function like

LD_PRELOAD can not intercept syscalls, but only libcalls?

最后都变了- 提交于 2019-12-22 10:47:02
问题 My code works well with malloc , but not with mmap . The code is below: main.c #include <stdio.h> #include <stdlib.h> int main(){ int * p = (int*) malloc(sizeof(int)); printf("in main(): value p = %d\n", *p); free(p); } preload.c #define _GNU_SOURCE #include <time.h> #include <dlfcn.h> #include <stdio.h> #include <sys/types.h> void *(*orig_malloc)(size_t size); void *malloc(size_t size){ printf(" Hooked(preload)! malloc:size:%lu\n", size); return orig_malloc(size); } void * (*orig_mmap)(void

How to use clone() to make parent process and child process run at the same time?

╄→尐↘猪︶ㄣ 提交于 2019-12-22 09:45:43
问题 I'm new to linux. I want to make child process and parent process at the same time. But I have failed. Here is my code. Can anybody help me? #define _GNU_SOURCE #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sched.h> #include <signal.h> #define FIBER_STACK 8192 void * stack; int do_something(){ int a = 0; while (a<10){ printf("pid : %d, a = %d\n", getpid(), a++); } exit(1); } int main() { void * stack; stack = malloc(FIBER_STACK); if(!stack) { printf("The stack failed\n"

Linux Kernel programming: trying to get vm_area_struct->vm_start crashes kernel

China☆狼群 提交于 2019-12-22 09:18:42
问题 this is for an assignment at school, where I need to determine the size of the processes on the system using a system call. My code is as follows: ... struct task_struct *p; struct vm_area_struct *v; struct mm_struct *m; read_lock(&tasklist_lock); for_each_process(p) { printk("%ld\n", p->pid); m = p->mm; v = m->mmap; long start = v->vm_start; printk("vm_start is %ld\n", start); } read_unlock(&tasklist_lock); ... When I run a user level program that calls this system call, the output that I

Linux set end of file (shrink, truncate, cut out some data @ end)

强颜欢笑 提交于 2019-12-22 09:01:25
问题 In windows there is SetEndOfFile() API to cut out some data in the end. How do I do this in Linux? The pseudo-code sample of what I'm looking for (Linux-specific): int fd = open("/path/to/file",O_RDWR); // file contents: "0123456789ABCDEF", 16 bytes lseek(fd,10,SEEK_CUR); // what's in the next line? (imaginary code) syscall(what,fd,FD_SET_EOF); close(fd); //sync(); // now file on disk looks like "0123456789", 10 bytes 回答1: ftruncate(fd, 10); (The lseek call isn't needed.) man 2 ftruncate 来源:

Can't access the open /arch/x86/syscalls/syscall_32.tbl

孤者浪人 提交于 2019-12-22 08:13:05
问题 As i'm writing this command after i shift to the kernel. When i compile it, it wasn't showing any list.Is there any other command to open the list ? open /arch/x86/syscalls/syscall_32.tbl 回答1: Bug Remove first / character from your file path (as it should be relative path). Check file Now, check that this file exists, using file tool: $ file arch/x86/syscalls/syscall_32.tbl Print file If file exists, you can print it using cat or less commands. E.g.: $ less arch/x86/syscalls/syscall_32.tbl

Passing parameters to system calls

萝らか妹 提交于 2019-12-22 05:48:17
问题 I did a basic helloWorld system call example that had no parameters and was just: int main() { syscall(__NR_helloWorld); return 0; } But now I am trying to figure out how to pass actual arguments to the system call (ie. a long ). What is the format exactly, I tried: int main() { long input = 1; long result = syscall(__NR_someSysCall, long input, long); return 0; } Where it takes a long and returns a long , but it is not compiling correctly; what is the correct syntax? 回答1: Remove the type

Why does fseek use read() system call?

て烟熏妆下的殇ゞ 提交于 2019-12-22 05:47:27
问题 I'm trying to understand the glibc implementation of fseek . To do so, I downloaded the glibc source code and tried to understand its function execution order. I found the fseek implementation in libio/fseek.c . Basically, it calls the function (or rather the macro) _IO_fseek() using the same parameters. This macro is implemented in libio/iolibio.h . It is defined as _IO_seekoff_unlocked (__fp, __offset, __whence, _IOS_INPUT|_IOS_OUTPUT) (implemented in libio/ioseekoff.c ). The next step in

C and resource protection in memory

醉酒当歌 提交于 2019-12-21 17:46:49
问题 When we compile a C program, it just generates some machine-understandable code. This code can directly run on the hardware, telling from this question. So my questions are: If a C program can directly run on the hardware, how can the kernel handle the resource allocation for this program? If the executable generated from the compiler is in pure machine-understandable form, then how do the privileged and non-privileged modes work? How does the kernel manage the permission of hardware

do system calls execute inside a software interrupt handler in entirety?

我是研究僧i 提交于 2019-12-21 17:26:53
问题 Do system calls execute in the context of a software interrupt handler in entirety? I mean, some system calls like read() could take a long time to return, against the policy that ISR should be very short in execution time. Are system calls offloaded to other threads? How does that work? [A reference to any kernel is fine] 回答1: The syscalls run on most kernels inside an ISR . Take a quick glance at a former release of Linux and you will notice the int $Ox80 to invoke the kernel. This solution