system-calls

System call fork() and execv function

浪尽此生 提交于 2019-12-17 19:05:54
问题 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.

How to pass arguments to processes created by fork()

a 夏天 提交于 2019-12-17 18:51:59
问题 I want to create copies of a process using fork() in C. I cant figure out how to pass arguments to the copies of my process. For example,I want to pass an integer to the process copies. Or I what to do, if I have a loop in which I call fork() and want to pass a unique value to processes (e.g. 0...N) for (int i = 0; i < 4; ++i) { fork(); // pass a unique value to new processes. } 回答1: The nice part about fork() is that each process you spawn automatically gets a copy of everything the parent

Loading raw code from C program

匆匆过客 提交于 2019-12-17 16:28:00
问题 I'm writing a program that loads and executes code from file. But i got a problem: "write" syscall does not work. Code successfully loads and executes, but does not display any text on the screen. Program that loads code: #include < stdio.h > #include < stdlib.h > int main(int argc,char* argv[]) { unsigned int f_size = 0; unsigned char* code_buf = NULL; void (*func_call)(void) = NULL; if(argc < 2) { printf("Usage: %s <FILE>\n",argv[0]); return 1; } FILE* fp = fopen(argv[1],"rb"); if(!fp) {

How to control which core a process runs on?

可紊 提交于 2019-12-17 15:15:37
问题 I can understand how one can write a program that uses multiple processes or threads: fork() a new process and use IPC, or create multiple threads and use those sorts of communication mechanisms. I also understand context switching. That is, with only once CPU, the operating system schedules time for each process (and there are tons of scheduling algorithms out there) and thereby we achieve running multiple processes simultaneously. And now that we have multi-core processors (or multi

Any benefit in using WEXITSTATUS macro in C over division by 256 on exit() status?

为君一笑 提交于 2019-12-17 10:54:51
问题 I was doing an exercise for university where I had to return a value with exit, that value was actually a count of something. This could be above 255 (which exit() can't handle) but the teacher suggested to use test data where the count could never go above that value. After all this, I needed to handle this count value, the exit status, I got this value in the main process by using waitpid(). To my surprise, if the child process returned 1, the "real" value in the main process was 256, 2 was

basic assembly not working on Mac (x86_64+Lion)?

冷暖自知 提交于 2019-12-17 09:54:14
问题 here is the code(exit.s): .section .data, .section .text, .globl _start _start: movl $1, %eax movl $32, %ebx syscall when I execute " as exit.s -o exit.o && ld exit.o -o exit -e _start && ./exit" the return is "Bus error: 10" and the output of " echo $? " is 138 I also tried the example of the correct answer in this question: Process command line in Linux 64 bit stil get "bus error"... 回答1: First, you are using old 32-bit Linux kernel calling convention on Mac OS X - this absolutely doesn't

Is there a better way than parsing /proc/self/maps to figure out memory protection?

坚强是说给别人听的谎言 提交于 2019-12-17 09:34:20
问题 On Linux (or Solaris) is there a better way than hand parsing /proc/self/maps repeatedly to figure out whether or not you can read, write or execute whatever is stored at one or more addresses in memory? For instance, in Windows you have VirtualQuery . In Linux, I can mprotect to change those values, but I can't read them back. Furthermore, is there any way to know when those permissions change (e.g. when someone uses mmap on a file behind my back) other than doing something terribly invasive

How do I reimplement (or wrap) a syscall function on Linux?

▼魔方 西西 提交于 2019-12-17 08:21:23
问题 Suppose I want to completely take over the open() system call, maybe to wrap the actual syscall and perform some logging. One way to do this is to use LD_PRELOAD to load a (user-made) shared object library that takes over the open() entry point. The user-made open() routine then obtains the pointer to the glibc function open() by dlsym() ing it, and calling it. The solution proposed above is a dynamic solution, however. Suppose I want to link my own open() wrapper statically. How would I do

64-bit syscall documentation for MacOS assembly

倾然丶 夕夏残阳落幕 提交于 2019-12-17 07:55:23
问题 I'm having trouble finding the good documentation for writing 64-bit assembly on MacOS. The 64-bit SysV ABI says the following in section A.2.1 and this SO post quotes it: A system-call is done via the syscall instruction. The kernel destroys registers %rcx and %r11. Returning from the syscall, register %rax contains the result of the system-call. A value in the range between -4095 and -1 indicates an error, it is -errno. Those two sentences are ok on Linux but are wrong on macOS Sierra with

practical examples use dup or dup2

自闭症网瘾萝莉.ら 提交于 2019-12-17 07:01:39
问题 I know what dup / dup2 does, but I have no idea when it would be used. Any practical examples? Thanks. 回答1: One example use would be I/O redirection. For this you fork a child process and close the stdin or stdout file descriptors (0 and 1) and then you do a dup() on another filedescriptor of your choice which will now be mapped to the lowest available file descriptor, which is in this case 0 or 1. Using this you can now exec any child process which is possibly unaware of your application and