Sometimes I see strange usage of dup2() function. For example: It doesn’t even check the return value. What exactly does it do with such arguments? The man dup2 says that if newfd (second argument of dup2) was previously open, it is silently closed before being reused. But the 0 is stdin and why do we even need to do that?
Tag: posix
Wait for signal from parent and does job and blocks again
Write a program that can display a text string (consisting only of the 26 alphabets and spaces). The program should fork 27 worker processes. Each worker process runs an infinite loop in which it waits for a signal from the controller (i.e., parent), sleeps for 1 sec, prints its pre-defined character, signals the controller, then blocks again. The controller reads
Strange POSIX semaphore behavior (stuck on sem_wait on Linux)
I’m trying to solve a school problem envolving POSIX semaphores. I’ve run into an issue that I’ve narrowed down to this simplified code: When compiling (using gcc -Wall -pthread sems.c -o sems) and running this program in Linux, I get the following output (the program doesn’t finish execution): Because I call sem_post(&sem) in the child process, I would expect the
Stop grep message from posting
I am working on a script that take 1 string argument and a file. I want it so that if a file is put in that doesn’t exist, then it will display the “filename cannot be read” message. That part does work however it also displays a “grep: grep.txt: No such file or directory” message. Is there any way to
How do I find out which header/include declared what variables in source? [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations. Closed 3 years ago. Improve this question If I see a struct, typedef, const, or any other variable
POSIX threads vs parallelism
Do POSIX threads (managing by pthread library) works parallelly? My lecturer has told us that this library creates only user threads, that are not managed by Linux kernel. I wrote some code that fill large matrix and compared times of execution for threads and whole program and it seems to works parallelly. Can I use this library to creating parallel
Where is ssize_t defined in Linux?
OS: Debian 9 (Linux 4.9) Compiler: GCC 8.2 Currently I am including <stddef.h> (where size_t is defined) and <stdint.h> (where most integral types are defined), but I still don’t have ssize_t. Where is it defined? Answer ssize_t is defined in sys/types.h. Per the POSIX documentation: NAME sys/types.h – data types SYNOPSIS DESCRIPTION The header shall define at least the following
Why is stat::st_size 0 for devices but at the same time lseek defines the device size correctly?
I noticed that when I query the size of a device using open + lseek, everything is OK, but when I stat the device, I get zero instead of the real device size. The device is clean without any file system and the first bytes of device start with some text like “1234567890ABC”. What is wrong? The code: Output: If
Only accept 4 character long, decimal numbers as user input for read
I am writing a small script to easily capture weights, which are taken from user input with read -p. I want all captured weights to be 4 characters long, decimal numbers in the format XX.X. For example 30.2 kg. How can I make sure users can only input decimal numbers in that format? Thank you for your help. Answer One
How to trap CTRL+Z in Linux POSIX shell script; possible or not?
I am a Linux shell scripter, I would like to know if it is possible to trap signal for script sleep: That is Ctrl+z? I do it at the moment like this and would like to trap that sleep signal too. Where the print_error_and_exit function just does as its name says. Goal: I would like to ignore this signal, if