system-calls

Anyone can understand how gettimeofday works?

耗尽温柔 提交于 2019-11-29 06:53:36
gettimeofday is a syscall of x86-86 according to this page (just search gettimeofday in the box): int gettimeofday(struct timeval *tv, struct timezone *tz); I thought the disas should be easy anough, just prepare the two pointers and call the related syscall . But its disas is doing much more: (gdb) disas gettimeofday Dump of assembler code for function gettimeofday: 0x00000034f408c2d0 <gettimeofday+0>: sub $0x8,%rsp 0x00000034f408c2d4 <gettimeofday+4>: mov $0xffffffffff600000,%rax 0x00000034f408c2db <gettimeofday+11>: callq *%rax 0x00000034f408c2dd <gettimeofday+13>: cmp $0xfffff001,%eax

Suppress console when calling “system” in C++

China☆狼群 提交于 2019-11-29 06:31:35
I'm using the system command in C++ to call some external program, and whenever I use it, a console window opens and closes after the command finishes. How can I avoid the opening of a console window? I would be happy if the solution could be platform-independent. I would also like for my program to wait until the command is finished. It sounds like you're using windows. On Linux (and *nix in general), I'd replace the call to system with calls to fork and exec , respectively. On windows, I think there is some kind of spawn-a-new-process function in the Windows API—consult the documentation.

How does strace work?

余生颓废 提交于 2019-11-29 06:13:03
问题 It can trace all system calls used. But what differs a sys_call from a normal call?? 回答1: As Matthew said, strace uses the ptrace(2) system call to work its magic. ptrace is used to implement debuggers and other tools which need to inspect what another program is doing. Essentially, strace will call ptrace and attach to a target process. Whenever the target process makes a system call, it will stop, and strace will be notified. strace will then inspect the registers and stack of the target

How do I read the results of a system() call in C++?

回眸只為那壹抹淺笑 提交于 2019-11-29 05:21:00
I'm using the following code to try to read the results of a df command in Linux using popen . #include <iostream> // file and std I/O functions int main(int argc, char** argv) { FILE* fp; char * buffer; long bufSize; size_t ret_code; fp = popen("df", "r"); if(fp == NULL) { // head off errors reading the results std::cerr << "Could not execute command: df" << std::endl; exit(1); } // get the size of the results fseek(fp, 0, SEEK_END); bufSize = ftell(fp); rewind(fp); // allocate the memory to contain the results buffer = (char*)malloc( sizeof(char) * bufSize ); if(buffer == NULL) { std::cerr <

What happens if a write system call is called on same file by 2 different processes simultaneously

眉间皱痕 提交于 2019-11-29 05:11:17
Does the OS handle it correctly? Or will I have to call flock()? Although the OS won't crash, and the filesystem won't be corrupted, calls to write() are NOT guarenteed to be atomic, unless the file descriptor in question is a pipe, and the amount of data to be written is PIPE_MAX bytes or less. The relevant part of the standard : An attempt to write to a pipe or FIFO has several major characteristics: Atomic/non-atomic: A write is atomic if the whole amount written in one operation is not interleaved with data from any other process. This is useful when there are multiple writers sending data

Make a system call to get list of processes

大城市里の小女人 提交于 2019-11-29 04:53:23
I'm new on modules programming and I need to make a system call to retrieve the system processes and show how much CPU they are consuming. How can I make this call? Why would you implement a system call for this? You don't want to add a syscall to the existing Linux API. This is the primary Linux interface to userspace and nobody touches syscalls except top kernel developers who know what they do. If you want to get a list of processes and their parameters and real-time statuses, use /proc . Every directory that's an integer in there is an existing process ID and contains a bunch of useful

How to implement system call in ARM64?

我怕爱的太早我们不能终老 提交于 2019-11-29 04:21:20
I am working with arm64 assembly coding and I want to implement system calls using svc instruction . I can't find any working arm64 system call implementation online.Also, I can't find the system call list for arm64. Also explain the implementation . You can pass six arguments in x0 to x5 , return value is saved in x0 . To give an assembler snippet, this is write syscall from Android Bionic's libc implementation . write 's three arguments would already be in x0-x2 . Syscall number is passed in x8 . /* Generated by gensyscalls.py. Do not edit. */ #include <private/bionic_asm.h> .hidden __set

How to check the value of errno?

↘锁芯ラ 提交于 2019-11-29 02:35:48
I am using a system call and in case it fails, I need to do different things for different errnos. I need to write code that looks something like this: int res; res = systemCall(); if (res == -1) { if (errno == ENOMSG) { doSomething(); } else { doSomethingElse(); } } perror doesn't help, because it only prints the value. As for strerro - if it is what I need, I am not suer how to use it, because here it says that the actual string is not the same as the error. Quote from the man page: "(For example, if errnum is EINVAL, the returned description will be "Invalid argument")". I am using Linux.

how does open works for normal file and device drivers

社会主义新天地 提交于 2019-11-29 02:33:12
Currently, I am learning Linux device drivers. And got stuck over how opening a device file works ? What I got until now... Consider the a simple code that opens a normal file.. #incldue<stdio.h> int main() { FILE fp; char buffer[20]; fp = fopen(/home/yoggi/foo.txt, "r"); fread(buffer, 5, 1, fp); } In above program, The fopen(), c-library function, is a wrapper function to the system call open() , which intern calls sys_open() or file_open() in VFS layer function. As linux supports a number of file system, virtual file system then transfer the control to actual file system handler to the

System Call fork() and execv function

南笙酒味 提交于 2019-11-29 02:26:01
I'm trying to run two executables consecutively using this c code: #include <stdio.h> #include <unistd.h> int main (int argc, char *argv[]) { fork(); execv("./prcs1", &argv[1]); // GIVE ADDRESS OF 2nd element as starting point to skip source.txt fork(); execv("./prcs2", argv); printf("EXECV Failed\n"); } The program exits after the first execv() call despite the fork, it never gets to the second execv(). I've tried calling wait() after the first fork but I'm not sure that's what it's missing. Any ideas why control doesn't return to the parent after the child exits? Carl Norum You have a couple