Why program prints 4 times 'do' instead of just one 'do'?
Code:
#include<stdio.h>
#include<unistd.h>
int main()
{
printf(" do ");
if(fork()!=0) printf(" ma ");
if(fork()==0) printf(" to \n ");
else printf("\n");
}
Program prints
do ma
do
do ma to
do to
You call fork twice in your "if" statements:
if(fork()!=0) printf(" ma ");
if(fork()==0) printf(" to \n ");
On the first fork, the parent A spawns a child B, then both the parent and child will invoke the fork a second time. The parent will spawn child C and the child will spawn child D. The result are 4 processes: A,B,C,D.
A ---- B
| |
C D
Since your prints are buffered until flushed to stdout and each forked process gets a copy of this buffer, four "do" are printed (check #ilkkachu answer).
If you intend to have a single "do", you should do this instead:
pid_t pid = fork();
if (pid > 0){
printf(" do ");
printf(" ma ");
} else {
printf(" to \n");
}
Basically store the return of fork() in a variable instead of invoking fork twice in your "if" statements.
Because standard output is line-buffered by default (or fully buffered, if you redirect it to a file or a pipe).
The first printf doesn't hit a newline, so it only adds the string do to a buffer internal to the C library. On the first fork, the whole process, including that buffer, is duplicated. Then one of the copies adds ma to its buffer, and both copies are duplicated (since both processes call fork again, not just the parent or the child.)
Finally, either printf(" to \n ") or printf("\n") is called, producing a newline, which triggers the actual writing of whatever was in the buffer.
You can either use fflush(stdout) to force the C library to output any buffered data before you fork, or use setbuf(stdout, NULL) to disable buffering completely.
Related
I would like to write a small program which will analyize my current input on the command line and generate some suggesstions like those search engines do.
The problems is how can an external program get the content on command line? For example
# an external program started and got passed in the PID of the shell below.
# the user typed something in the shell like this...
<PROMPT> $ echo "grab this command"
# the external program now get 'echo "grab this command"'
# and ideally the this could be done in realtime.
More over, can I just modify the content of current command line?
EDIT
bash uses libreadline to manage the command line, but still I can not imagine how to make use of this.
You could write your own shell wrapper using c. Open bash in a process using popen and use fgetc and fputc to write the data to the process and the output file.
A quick dirty hack could look like this (bash isn't started in interactive mode, but otherwise should work fine. --> no prompt):
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
pid_t pid;
void kill_ch(int sig) {
kill(pid, SIGKILL);
}
/**
*
*/
int main(int argc, char** argv) {
int b;
FILE *cmd = NULL;
FILE *log = NULL;
signal(SIGALRM, (void (*)(int))kill_ch);
cmd = popen("/bin/bash -s", "r+");
if (cmd == NULL) {
fprintf(stderr, "Error: Failed to open process");
return EXIT_FAILURE;
}
setvbuf(cmd, NULL, _IOLBF, 0);
log = fopen("out.txt", "a");
if (log == NULL) {
fprintf(stderr, "Error: Failed to open logfile");
return EXIT_FAILURE;
}
setvbuf(log, NULL, _IONBF, 0);
pid = fork();
if (pid != 0)
goto EXEC_WRITE;
else
goto EXEC_READ;
EXEC_READ:
while (1) {
b = fgetc(stdin);
if (b != EOF) {
fputc((char) b, cmd);
fputc((char) b, log);
}
}
EXEC_WRITE:
while (1) {
b = fgetc(cmd);
if (b == EOF) {
return EXIT_SUCCESS;
}
fputc(b, stdout);
fputc(b, log);
}
return EXIT_SUCCESS;
}
I might not fully understand your question but I think you'd basically have two options.
The first option would be to explicitly call your "magic" program by prefixing your call with it like so
<PROMPT> $ magic echo "grab this command"
(magic analyzes $* and says...)
Your input would print "grab this command" to stdout
<PROMPT> $
In this case the arguments to "magic" would be handled as positional parameters ($*, $1 ...)
The second option would be to wrap an interpreter-like something around your typing. E.g. the Python interpreter does so if called without arguments. You start the interpreter, which will basically read anything you type (stdin) in an endless loop, interpret it, and produce some output (typically on stdout).
<PROMPT> $ magic
<MAGIC_PROMPT> $ echo "grab this command"
(your magic interpreter processes the input and says...)
Your input would print "grab this command" to stdout
<MAGIC_PROMPT> $
Currently, I'm trying to create an orphaned process by having proc A create proc B and proc B create proc C. Then, I want to kill proc B so that proc C is no longer attached to the process tree of proc A. However, I also want to know the process id of proc C in proc A. So far as an example, I have tried:
use FileHandle;
pipe(READ, WRITE);
WRITE->autoflush();
my $pid1 = fork();
if ($pid1) {
#Proc A
waitpid($pid1, 0);
close WRITE;
my $msg = <READ>;
print "msg: $msg\n";
} elsif ($pid1 == 0) {
#Proc B
my $pid2 = fork();
if ($pid2) {
#Proc B
print WRITE $pid2;
exit 0;
} elsif ($pid2 == 0) {
#Proc C
print "child 2 proc $$\n";
sleep(5);
exit 0;
}
}
Unfortunately, running this makes proc A wait for proc C to finish (sleep 5) before it prints the id of proc C which is not what I want. Is there a way to have the waitpid function return once proc B exits?
Alternatively, is there a better way to create an orphan process?
EDIT: added info regarding read/write pipe.
When I run your script, it doesn't behave in the way you describe. This may be related to the mechanics of the READ and WRITE handles which you've omitted.
However I think the POSIX::setsid() function does what you want. It puts a process into its own process group which is disconnected from the controlling TTY of the parent process:
use POSIX qw();
# after forking, in the child process that you want to become an orphan
POSIX::setsid();
I ended up using something like below to implement this:
use FileHandle;
pipe(READ, WRITE);
WRITE->autoflush();
my $cmd = "nohup ./test.sh";
my $pid1 = fork();
if ($pid1) {
#parent
my $kid;
$kid = waitpid($pid1, 0);
close WRITE;
my $msg = <READ>;
print "msg: $msg\n";
sleep(10);
} elsif ($pid1 == 0) {
#1st child
my $pid2 = fork();
if ($pid2) {
#1st child
print WRITE $pid2;
exit 0;
} elsif ($pid2 == 0) {
exec $cmd;
exit 0;
}
}
I understand how I/O redirection works in Unix/Linux, and I know Shell uses this feature to pipeline programs with a special type of file - anonymous pipe. But I'd like to know the details of how Shell implements it programmatically? I'm interested in not only the system calls involved, but also the whole picture.
For example ls | sort, how does Shell perform I/O redirection for ls and sort?
The whole picture is complex and the best way to understand is to study a small shell. For a limited picture, here goes. Before doing anything, the shell parses the whole command line so it knows exactly how to chain processes. Let's say it encounters proc1 | proc2.
It sets up a pipe. Long story short, writing into thepipe[0] ends up in thepipe[1]
int thepipe[2];
pipe(thepipe);
It forks the first process and changes the direction of its stdout before exec
dup2 (thepipe[1], STDOUT_FILENO);
It execs the new program which is blissfully unaware of redirections and just writes to stdout like a well-behaved process
It forks the second process and changes the source of its stdin before exec
dup2 (thepipe[0], STDIN_FILENO);
It execs the new program, which is unaware its input comes from another program
Like I said, this is a limited picture. In a real picture the shell daisy-chains these in a loop and also remembers to close pipe ends at opportune moments.
This is a sample program from the book operating system concepts by silberschatz
Program is self-explanatory if you know the concepts of fork() and related things..hope this helps! (If you still want an explanation then I can explain it!)
Obviously some changes(such as change in fork() etc) should be made in this program if you want it to make it work like
ls | sort
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#define BUFFER SIZE 25
#define READ END 0
#define WRITE END 1
int main(void)
{
char write msg[BUFFER SIZE] = "Greetings";
char read msg[BUFFER SIZE];
int fd[2];
pid t pid;
/* create the pipe */
if (pipe(fd) == -1) {
fprintf(stderr,"Pipe failed");
return 1;
}
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
if (pid > 0) { /* parent process */
/* close the unused end of the pipe */
close(fd[READ END]);
/* write to the pipe */
write(fd[WRITE END], write msg, strlen(write msg)+1);
/* close the write end of the pipe */
close(fd[WRITE END]);
}
else { /* child process */
/* close the unused end of the pipe */
close(fd[WRITE END]);
/* read from the pipe */
read(fd[READ END], read msg, BUFFER SIZE);
printf("read %s",read msg);
}
}
/* close the write end of the pipe */
close(fd[READ END]);
return 0;
}
We can detect if some is a zombie process via shell command line
ps ef -o pid,stat | grep <pid> | grep Z
To get that info in our C/C++ programs we use popen(), but we would like to avoid using popen(). Is there a way to get the same result without spawning additional processes?
We are using Linux 2.6.32-279.5.2.el6.x86_64.
You need to use the proc(5) filesystem. Access to files inside it (e.g. /proc/1234/stat ...) is really fast (it does not involve any physical I/O).
You probably want the third field from /proc/1234/stat (which is readable by everyone, but you should read it sequentially, since it is unseekable.). If that field is Z then process of pid 1234 is zombie.
No need to fork a process (e.g. withpopen or system), in C you might code
pid_t somepid;
// put the process pid you are interested in into somepid
bool iszombie = false;
// open the /proc/*/stat file
char pbuf[32];
snprintf(pbuf, sizeof(pbuf), "/proc/%d/stat", (int) somepid);
FILE* fpstat = fopen(pbuf, "r");
if (!fpstat) { perror(pbuf); exit(EXIT_FAILURE); };
{
int rpid =0; char rcmd[32]; char rstatc = 0;
fscanf(fpstat, "%d %30s %c", &rpid, rcmd, &rstatc);
iszombie = rstatc == 'Z';
}
fclose(fpstat);
Consider also procps and libproc so see this answer.
(You could also read the second line of /proc/1234/status but this is probably harder to parse in C or C++ code)
BTW, I find that the stat file in /proc/ has a weird format: if your executable happens to contain both spaces and parenthesis in its name (which is disgusting, but permitted) parsing the /proc/*/stat file becomes tricky.
Can you help me understand the following code?
void errorexit(char *pchar) {
// display an error to the standard err.
fprintf(stderr, pchar);
fprintf(stderr, "\n");
exit(1);
}
Calling errorexit("Error Message") will print "Error Message" to the standard error stream (often in a terminal) and exit the program. Any programs (such as the shell) that called your program will know that the there was an error since your program exited with a non-zero status.
It is printing out the string pointed to by pchar to the standard error output via fprintf and then forcing the application to exit with a return code of 1. This would be used for critical errors when the application can't continue running.
That function prints the provided string and a newline to stderr and then terminates the current running program, providing 1 as the return value.
fprintf is like printf in that it outputs characters, but fprintf is a little different in that it takes a file handle as an argument. I this case stderr is the file handle for standard error. This handle is already defined for you by stdio.h, and corresponds to the error output stream. stdout is what printf outputs to, so fprintf(stdout, "hello") is equivalent to printf("hello").
exit is a function that terminates the execution of the current process and returns whatever value was its argument as the return code to the parent process (usually the shell). A non-zero return code usually indicates failure, the specific value indicating the type of failure.
If you ran this program from the shell:
#include <stdio.h>
#include "errorexit.h"
int main(int argc, char* argv[])
{
printf("Hello world!\n");
errorexit("Goodbye :(");
printf("Just kidding!\n");
return 0;
}
You'd see this output:
Hello world!
Goodbye :(
And your shell would show "1" as the return value (in bash, you can view the last return code with echo $?).
Note that "Just kidding!" would not be printed, as errorexit calls exit, ending the program before main finishes.