system-calls

C/Unix Strange behaviour while using system calls and printf

天涯浪子 提交于 2019-12-02 16:43:22
问题 I'm a newbie, trying to really understand systems programming. In the following program, I'm reading a file called 'temp1' (containing 1 2 3 4) and printing its contents to stdout. However, I also wanted to check the value of file descriptor returned by open. If I include the '\n' in printf call on line 5, the output prints value filep first and then contents of file. But if I remove the newline, the contents of file get printed first and then the value of filep. Why would this happen ? int

How to interpret strace output?

▼魔方 西西 提交于 2019-12-02 15:51:59
I need to profile the performance of an application for which I am using strace. However, I do not really know how to interpret the various system calls the strace emits. Examples of a few of them are below: (A) lseek(3, 1600, SEEK_SET) = 1600 (B) write(3, "G_DATA 300 0 "..., 800) = 800 (C) close(3) = 0 (D) mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b600b179000 (E) munmap(0x2b600b179000, 4096) = 0 (F) fstat(3, {st_mode=S_IFREG|0644, st_size=1600, ...}) = 0 I would be grateful if someone could briefly explain in plain English what these lines from (A) to (F)

Why read system call stops reading when less than block is missing?

送分小仙女□ 提交于 2019-12-02 15:32:28
问题 Introduction and general objective I am trying to send an image from a child process (generated by calling popen from the parent) to the parent process. The image is a grayscale png image. It is opened with the OpenCV library and encoded using imencode function of the same library. So the resulting encoded data is stored into a std::vector structure of type uchar , namely the buf vector in the code below. No error in sending preliminary image information First the child sends the following

How do system calls work?

浪子不回头ぞ 提交于 2019-12-02 13:57:57
I understand that a user can own a process and each process has an address space (which contains valid memory locations, this process can reference). I know that a process can call a system call and pass parameters to it, just like any other library function. This seems to suggest that all system calls are in a process address space by sharing memory, etc. But perhaps, this is only an illusion created by the fact that in high level programming language, system calls look like any other function, when a process calls it. But, now let me take a step deeper and analyze more closely on what

How to make system call from another system call in kernel space

≯℡__Kan透↙ 提交于 2019-12-02 13:35:10
问题 I am new in Linux kernel development. I have implemented a system call say my_pid in linux kernel 2.6. I want to call getpid system call from my system call. How can I do it? I want something like: pid_t my_pid(){ return getpid(); } Also from C in user-space I can call any system call using: syscall(); What is the generic way to do this in kernel mode? 回答1: There is no generic way of doing this. If you are in kernel space, you should invoke kernel functions that implement the system call

Launch one C++ application from another, and communicate with it

廉价感情. 提交于 2019-12-02 12:02:17
问题 I have a C++ (technically MATLAB mex) program, which I am planning to use to launch a stand-alone pure C++ slave program on my system. The master calling program may look something like the following: void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]){ system ("path/to/slave/program"); } Once the slave is launched, I would then like to have a second mex program which will communicate with and send data to the slave program. The data sent will be in the form of large-ish

how to measure pipe syscall time in milliseconds?

本小妞迷上赌 提交于 2019-12-02 11:29:15
问题 I want to see the time of my pipe program system call. I need to measure it for analyzing results. How can i measure the time of system call in milliseconds? For example, this is simple pipe program: #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/timeb.h> #include <time.h> void main() { FILE *read_fp; char buffer[10]; int cnt; memset(buffer,'\0',sizeof(buffer)); read_fp=popen("uname -a","|"); if (read_fp!=NULL) { cnt=fread(buffer,sizeof(char),9

Why read system call stops reading when less than block is missing?

一个人想着一个人 提交于 2019-12-02 11:16:28
Introduction and general objective I am trying to send an image from a child process (generated by calling popen from the parent) to the parent process. The image is a grayscale png image. It is opened with the OpenCV library and encoded using imencode function of the same library. So the resulting encoded data is stored into a std::vector structure of type uchar , namely the buf vector in the code below. No error in sending preliminary image information First the child sends the following image information needed by the parent: size of the buf vector containing the encoded data: this piece of

PHP system calls and $PATH in OS X

随声附和 提交于 2019-12-02 10:38:18
I'm trying to get PHP to make system calls on OS X. However, it doesn't seem to be able to find anything that's included in the system path. When I run... putenv("PATH={$_SERVER["PATH"]}:/usr/local/bin"); ... just before the system call, it works. This is not a practical solution, since the code that executes the system call is a plugin, so I'd rather not touch source code that'll make it incompatible come an update. Apache2 is running as the same user as I'm logged in, so theoretically it has access to the same commands as me. Also, the same code works fine on my Ubuntu machine. Environment

Duplicate stdout and stderr from fork process to files

可紊 提交于 2019-12-02 10:02:34
I need to duplicate stdout and stderr of a child proccess to multiple files. I understand that i can use tee() , but I haven't found examples for that. Now, it's only to print all to stdout and stderr. How to do it? pid_t childId = fork(); switch(childId){ case -1: perror("fork() error!\n"); return; case 0: mysignal(SIGTTOU, SIG_DFL); mysignal(SIGTTIN, SIG_DFL); mysignal(SIGCHLD, SIG_DFL); if(!background) tcsetpgrp(cterm, getpid()); setpgid(0, 0); if (isWriter) close(pipefd[0]); if (isReader!=-1) { close(0); dup(oldChannelOut); } if(isWriter){ close(1); dup(pipefd[1]); } //exec, if program is