How is system() function of stdlib.h file implemented in linux? [duplicate] - c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How does system() exactly work in linux?
For the sake of my curiosity,
I want to know that how does system() function spawn the process and its internal implemenation ?
Is is using fork , exec internally ?

On my Debian box it uses clone() that itself calls sys_clone(). You can use strace to look at system calls.

Related

How sleep() works? [duplicate]

This question already has answers here:
How does sleep(), wait() and pause() work?
(4 answers)
Closed 2 years ago.
In C, how does the sleep function work? At the background is a while loop created? Or for loop? I would like to know exactly what lib does, how could I recreate a sleep in a simple way without having to use lib?
It's not implemented with a loop of any kind (that would waste energy occupying a core when you're doing nothing); it's a system call in which you tell the OS to suspend the current thread and wake it after the interval has elapsed (the exact mechanism used varies by OS). Reimplementing it yourself is ultimately going to depend on a system call and/or signals in some way; don't bother, just use sleep (or nanosleep, or Sleep, depending on OS).

Using system call in c [duplicate]

This question already has answers here:
replace system() with non-blocking function
(5 answers)
Closed 8 years ago.
Please tell me how to open two files simultaneouly using system();
I want to open two file
and I am doing it like
system("C:\temp\file1.doc");
system("C:\temp\file2.doc");
But here till the file 1 is open file 2 is not opening as the control is not able to reach the second system call , Is there a way to do open them simultaneously.
Thanks
Shashank
I would suggest two ways.
Run two threads. Call system() from each thread.
Create a child process using fork() and run system() from both parent and child processes.

Find number of threads spawned by a process in Linux [duplicate]

This question already has answers here:
POSIX API call to list all the pthreads running in a process
(3 answers)
Closed 9 years ago.
I want to write a c function , which when called by the process returns number of threads created by that process.
I want to get the value not by counting but from the kernel structure ?
Which structure has this information ?
You can get a lot of information about your process by looking in /proc/$$ where $$ is your process ID.The number of threads is available atomically through /proc/$$/status.
My solution: You need write a function to analyse the file /proc/$$/status to get the number of threads.

How to call unix commands from C program [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Running a shell command in a c program
I want to call echo in a C program and then redirect its output to a file. But I am not sure how to call unix commands in C program. Any help is much appreciated.
Thanks,
Aashish
#include <stdlib.h>
int main() {
system("echo test > filename");
return 0;
}
is the trivial way to do this. If you want more control over the process then you should investigate fork/exec.
However you can write files trivially using the C stdlib file functions. That would be much more lightweight than spawning a new process.
An alternative to using system to execute a UNIX command in C is execlp.
It all depends on the way you want to use them. If you want user input, then you might want to use execlp / execve. Otherwise, system is a fast, easy way to get UNIX working with your C program.
For redirection, you can use pipes.
It will help you look at data you want to redirect to a file you opened. With system you just set it but have no real options to set it or optimise it the way you would like.
If you specifically want to execute this program in order to grab its output, you rather want popen() instead of system().
You can do this using the system function:
#include <stdlib.h>
int main() {
system("echo 'words' > file");
return 0;
}
or using fopen, printf, fclose.

Is it thread safe to call printf in threads that run simultaneously? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
stdout thread-safe in C on Linux?
Say thread1 and thread2 are similar and at the end of their jobs they both printf. Is it thread safe or do they have to lock printf somehow?
Is it related to stdout? What if one does fflush(stdout) after each printf? Does it change anything?
The POSIX.1 and C-language functions
that operate on character streams
(represented by pointers to objects of
type FILE) are required by POSIX.1c to
be implemented in such a way that
reentrancy is achieved (see ISO/IEC
9945:1-1996, ยง8.2).
refer to Thread-safety and POSIX.1
Note: Some functions can be reentrant or non-reentrant, depending on their arguments.

Resources