systems-programming

How to list first level directories only in C?

最后都变了- 提交于 2020-01-10 05:16:10
问题 In a terminal I can call ls -d */ . Now I want a c program to do that for me, like this: #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <unistd.h> int main( void ) { int status; char *args[] = { "/bin/ls", "-l", NULL }; if ( fork() == 0 ) execv( args[0], args ); else wait( &status ); return 0; } This will ls -l everything. However, when I am trying: char *args[] = { "/bin/ls", "-d", "*/", NULL }; I will get a runtime error: ls: */: No such file or directory 回答1:

Is there any way for ioctl() in linux to specify submission queue ID for a nvme IO request

戏子无情 提交于 2020-01-03 05:07:46
问题 I am working on a testing tool for nvme-cli(written in c and can run on linux). For SSD validation purpose, we are actually looking for sending I/O commands to a particular Submission queue(IO Queue pair). We needed this because we wanted threading, but for threading to happen we need to send I/O requests to different queues else the I/O requests would be processed serially. So is there any way in ioctl() where we can specify the Submission queue IDs? Here is how a nvme IO is requested with

How to avoid race condition when checking if file exists and then creating it?

十年热恋 提交于 2020-01-01 17:09:25
问题 I'm thinking of corner cases in my code and I can't figure out how to avoid problem when you check if file exists, and if it does not, you create a file with that filename. The code approximately looks like this: // 1 status = stat(filename); if (!status) { // 2 create_file(filename); } Between the call to 1 and 2 another process could create the filename. How to avoid this problem and is there a general solution to this type of problems? They happen often in systems programming. 回答1: You're

How to implement the c malloc/realloc functions properly?

旧城冷巷雨未停 提交于 2019-12-25 19:09:21
问题 I am writing my own OS and had to implement my own malloc realloc functions. However I think that what I have written may not be safe and may also cause a memory leak because the variable isn't really destroyed, its memory is set to zero, but the variable name still exists. Could someone tell me if there are any vulnerabilities in this code? The project will be added to github soon as its finished under user subado512. Code: void * malloc(int nbytes) { char variable[nbytes]; return &variable;

How does a parent process read a FIFO after the child process finished the writing that FIFO?

寵の児 提交于 2019-12-24 20:30:09
问题 I have a very simple basic program that has two process first one is parent and second one is child . Child process should write some stuff to the FIFO . After all writing jobs finished(after the child is terminated). Then parent process should read all the FIFO file and print to the stdout . So I think, I need a wait(NULL); for parent . So the parent will wait until the child is terminated. But child is also blocked because of the writing and blocked for reading this writes. So both process

Does user space program(runc) regulate the size of physical address space of a docker container process?

时光毁灭记忆、已成空白 提交于 2019-12-24 20:15:18
问题 Below is the CloudFormation template to configure ECS task containers on AWS EC2 instance(Linux): TodobackendTaskDefinition: Type: "AWS::ECS::TaskDefinition" Properties: ContainerDefinitions: - Name: todobackend Image: someacct/todobackend Memory: 450 MountPoints: - ContainerPath: /var/www/todobackend SourceVolume: webroot - Name: nginx Image: someacct/todobackend-nginx Memory: 300 PortMappings: - ContainerPort: "8000" HostPort: "8000" MountPoints: - ContainerPath: /var/www/todobackend

opendir will not accept string variable but will accept plain string

扶醉桌前 提交于 2019-12-24 04:08:15
问题 I cannot get this function to work, because for some reason opendir will not take buffer2 (declared as char buffer2[128]) as an argument properly. If I replace the variable with something like "." or "sample", it works perfectly. But doing it like this, I get a segmentation fault every time. Please help. system("clear"); DIR *dirp; struct dirent *dp; printf("Enter directory name: "); fgets(buffer2, 100, stdin); if((dirp = opendir(buffer2)) == NULL) printf("Could not open directory\n"); while(

vfork() system call

自作多情 提交于 2019-12-22 06:41:44
问题 I read that the new process created using vfork() system call executes as a thread in the parent's address space and until the child thread doesnot calls exit() or exec() system call, the parent is blocked. So I wrote a program using vfork() system call #include <stdio.h> #include <unistd.h> int main() { pid_t pid; printf("Parent\n"); pid = vfork(); if(pid==0) { printf("Child\n"); } return 0; } I got the output as follows: Parent Child Parent Child Parent Child .... .... .... I was assuming

Can I invoke ioctl() (system call) for nvme with different threads using pthreads

一笑奈何 提交于 2019-12-22 01:36:57
问题 I am working on a testing tool for nvme-cli(written in c and can run on linux). I am interested in repeating a nvme command 'r' number of times with 't' number of threads. The below code does the repeat of a command along with threading, but the issue here is the parallel execution time is very much high compared to serial execution. As per my observation the reason is the invocation of ioctl() system call from err = nvme_identify(fd, 0, 1, data); i.e nvme_identify() inturn calls ioctl() . So

What's the shortest code to write directly to a memory address in C/C++?

爱⌒轻易说出口 提交于 2019-12-21 03:21:08
问题 I'm writing system-level code for an embedded system without memory protection (on an ARM Cortex-M1, compiling with gcc 4.3) and need to read/write directly to a memory-mapped register. So far, my code looks like this: #define UART0 0x4000C000 #define UART0CTL (UART0 + 0x30) volatile unsigned int *p; p = UART0CTL; *p &= ~1; Is there any shorter way (shorter in code, I mean) that does not use a pointer? I looking for a way to write the actual assignment code as short as this (it would be okay