I write a inter process program which start sub processes in main program. These processes communicate as they are running independently in shell. I use pipe(), dup2() to connect stdin, stdout of sub process.
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <string>
#include <sstream>
#define READ 0
#define WRITE 1
int popen3(const char* command, int & pid, FILE*& fdr, FILE*& fdw)
{
pid_t child_pid;
int pip1[2]={-1,-1}; // [parent-read, clild-write]
int pip2[2]={-1,-1}; // [clild-read, parent-write]
pipe2(pip1, 0);
pipe2(pip2, 0);
if((child_pid = fork()) == -1)
{
perror("fork");
//exit(1);
return errno;
}
/* child process */
if (child_pid == 0)
{
close(pip1[READ]);
close(pip2[WRITE]);
dup2(pip1[WRITE], STDOUT_FILENO); // child-write
dup2(pip2[READ], STDIN_FILENO); // child-read
close(pip1[WRITE]);
close(pip2[READ]);
setpgid(child_pid, child_pid); //Needed so negative PIDs can kill children of /bin/sh
execl("/bin/sh", "/bin/sh", "-c", command, NULL);
exit(0);
}
else // parent process
{
close(pip1[WRITE]);
close(pip2[READ]);
}
pid = child_pid;
fdr = fdopen(pip1[READ], "r"); // parent-read
fdw = fdopen(pip2[WRITE], "w"); // parent-write
return 0;
}
int main()
{
int pid;
string command = "python3";
FILE *fpw = nullptr, *fpr = nullptr;
if (popen3(command.c_str(), pid, fpr, fpw) !=0) {
printf("popen3 error\n");
exit(1);
}
char command_out[10] = {0};
if (fpr!=nullptr)
fcntl(fileno(fpr), F_SETPIPE_SZ, 0);
if (fpw!=nullptr)
fcntl(fileno(fpw), F_SETPIPE_SZ, 0);
if (fpw!=nullptr){
//fcntl(fileno(fpw), F_SETPIPE_SZ, 100);
const char* py="import os; os.write(2, b'py out stderr\\n'); print('hello'+' world')\n";
fwrite(py, 1, strlen(py), fpw);
}
else {
printf("fpw null\n");
}
if (fpr!=nullptr){
sleep(1);
fcntl(fileno(fpr), F_SETFL, O_NONBLOCK);
while (fread(command_out, 1, 10, fpr) > 0)
printf("#### %s", command_out);
}
else {
printf("fpr null\n");
}
if (fpw!=nullptr){
const char* py="import os; os.write(2, b'py out stderr\\n'); print('hello'+' world'); exit()\n";
fwrite(py, 1, strlen(py), fpw);
}
else {
printf("fpw null\n");
}
pclose3(fpr, fpw, pid);
return 0;
}
OUTPUT:
py out stderr
py out stderr
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
BrokenPipeError: [Errno 32] Broken pipe
It seems that it closed child to parent pipe. I'm fused to fix this.
This may be the right output:
Python 3.8.10 (default, .... (from sub process stderr and directed to shell)
py out stderr
hello world (from sub process stdout and fread and printf to shell)
py out stderr
hello world
I have read the following questions but still not fix my problem.
Getting the PID from popen
Bi-directional inter-process communication using two pipes
And, here is a very clear tutorial:
http://unixwiz.net/techtips/remap-pipe-fds.html
Thanks!
Related
I am getting unusual results while writing and reading from a pipe. The runtime error is Program terminated by signal: 13. I searched about this error and found that this error is due to there are no readers to read from pipe while i am reading from the pipe in the child process. Here is my code:
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define BUFFER_SIZE 50
#define READ_END 0
#define WRITE_END 1
int main()
{
int pipe_fd[2];
pid_t pid;
pid = fork();
char message[BUFFER_SIZE] = "Greetings";
char read_message[BUFFER_SIZE];
if( pipe(pipe_fd) == -1)
{
perror("Error in creating the pipe \n");
exit(-1);
}
if(pid==-1)
{
perror("Error in creating the child! \n");
exit(-1);
}
if(pid==0) // Child process
{
close(pipe_fd[WRITE_END]);
read(pipe_fd[READ_END], read_message , BUFFER_SIZE);
printf("The message read by the child is: %s", read_message);
close(pipe_fd[READ_END]);
}
if(pid>0) // Parent process
{
close(pipe_fd[READ_END]); //Closing the read end of the pipe
write(pipe_fd[WRITE_END], message, BUFFER_SIZE); // Writing to pipe on write_end
close(pipe_fd[WRITE_END]);
}
return 0;
}
Any suggestions how to solve this runtime error?
You need to open your pipe before you fork a child process. Otherwise your processes aren't talking to the same pipe.
Thus move your pipe creation code above the fork() call as follows:
if( pipe(pipe_fd) == -1)
{
perror("Error in creating the pipe \n");
exit(-1);
}
pid_t pid;
pid = fork();
char message[BUFFER_SIZE] = "Greetings";
char read_message[BUFFER_SIZE];
if(pid==-1)
{
perror("Error in creating the child! \n");
exit(-1);
}
Trying to understand why this section of code using the cat command isn't working with execvp in C.
char *in[5] ={"cat", "file1.txt", ">>", "file2.txt", 0};
execvp(in[0], in);
When I run it displays the contents of file1.txt but then says:
cat: >> No such file or directory.
Then displays the contents of file2.txt
Why wouldn't it recognize the >> operator in this instance?
You can read the "man tee" command which it read from standard input and write to standard output and files. You could achieve this with below example.
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
/*
Implementation of below command:
cat file1.txt > file2.txt
*/
char *cmd1[] = { "/bin/cat", "file1.txt", 0 };
char *cmd2[] = { "tee", "file2.txt", 0 };
static void sigchld_hdl (int sig)
{
int status;
while (waitpid(-1, &status, 0) > 0) {
if(WIFEXITED(status))
printf("Child exited with code %d\n", WEXITSTATUS(status)); }
}
int runcmd(int pfd[])
{
int i=0;
switch (fork()) {
case -1:
perror ("fork");
return 1;
case 0:
dup2(pfd[0], 0);
close(pfd[1]); /* the child does not need this end of the pipe */
execvp(cmd2[0], cmd2);
perror(cmd2[0]);
exit(10);
default: /* parent */
dup2(pfd[1], 1);
close(pfd[0]); /* the parent does not need this end of the pipe */
execvp(cmd1[0], cmd1);
perror(cmd1[0]);
}
sleep(1);
}
int main (int argc, char *argv[])
{
struct sigaction act;
int fd[2];
pipe(fd);
memset (&act, 0, sizeof(act));
act.sa_handler = sigchld_hdl;
if (sigaction(SIGCHLD, &act, 0)) {
perror ("sigaction");
return 1;
}
runcmd(fd);
return 0;
}
I have the following program, when I run the program, I feel really confused that why my program didn't excute
int num=i;
printf("it is No.%d !",num);
printf("hello , I will excute execvp!");
My program basically create 6 child processes to excute executionbode() function, and then use execvp to overload original program. However, everytime when I run the program, the string "hello, I will execute execvp" never shows up! Also I think those three sentences above also didn't execute in the running program? can someone tell me why? Here is my program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include "makeargv.h"
#include "redirection.h"
#include <sys/wait.h>
int executionnode(int i);
int main(){
pid_t childpid;
int i;
int row=6;
for(i=0;i<row;i++)
{ childpid=fork();
if(childpid==0)
continue;
else if (childpid>0)
executionnode(i);
else {
perror("something wrong");
exit(1);
}
}
}
int executionnode(int i){
sleep(i);
printf("hello, I am process:%ld\n",(long)getpid());
wait(NULL);
char *execArgs[] = { "echo", "Hello, World!", NULL };
int num=i;
printf("it is No.%d !",num);
printf("hello , I will excute execvp!");
execvp("echo", execArgs);
}
Can someone tell me why? and how to fix it? I feel it is really strange? Is it because of execvp() functions? I just began to learn operating system,so I am really confused about it! Thank you for helping me!
As user3629249 said you have some confusion. You'll get many children of children of children... and that wait(NULL) is useless :).
I used this structure to got your goal in my OS subject excercises.
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#define N 5
int main(int argc, char const *argv[])
{
pid_t pid,pids[N];
int i, num_pids = 0;
int state = 0;
int prior[]={1,3,5,2,4};
pid_t parent_pid = getpid();
printf("Parent pid is %i\n",father_pid);
// This for loop is the key
for (i = 0; i < N && getppid() != parent_pid; i++)
{
if ((pid = fork()) < 0)
{
printf ("fork error\n");
exit(-1);
}
pids[num_pids++] = pid;
}
if (pid == 0) // Child processes
{
printf("I am the child %i\n",getpid());
}
else // Parent process
{
for (i = 0; i < N; i++)
{
int pid_index = prior[i]-1; // Array starts with 0
pid = waitpid(pids[pid_index]);
printf("Children %i ended\n",pids[indice_pid]);
printf("%i alive children\n",N-1-i);
}
}
return 0;
}
This structure works because you save the parent's pid in parent_pid variable and compare the parent of each process pid with getppid(). If this pid is different that parent_pid, this proccess is the parent. In another case the process is a child so it has to stop (these processes don't have to fork). With this way you can get only the forks you need.
The rest of the code is the same: Pid==0 is child process and any other is the parent. You can call executionnode(int i) in child processes block (remember, pid==0 !!! you have a mistake). i variable should have the right value in each call I think.
Good luck!
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
void handler(int sig)
{
pid_t pid;
int status;
while( (pid = waitpid(-1, &status, WNOHANG)) > 0 )
printf("%d\n", pid);
}
int main(void)
{
struct sigaction act;
pid_t pid;
int ch;
act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGCHLD, &act, 0);
pid = fork();
if( pid == 0 ) {
exit(0);
}
else {
if( (ch = fgetc(stdin)) == EOF )
printf("EOF\n");
}
}
Hello, I want to know about sigaction function. If I execute this program, the result is like below.
[process id]
EOF
Why EOF is in stdin buffer after processing SIGCHLD signal ? I don't know why this happen. or Maybe I don't know how to use sigaction function ?
fgetc() returns EOF if the file is at end-of-file or an error occurs while trying to read the character. In this case, read() being interrupted by a signal is an error, and the SA_RESTART option to sigaction() prevents this error.
To distinguish between EOF and error, use feof() or ferror(), or test the variable errno. errno will be 0 for the EOF case, non-zero for an error (EINTR in this case).
Parent receives SIGPIPE sending chars to aborted child process through FIFO pipe.
I am trying to avoid this, using select() function. In the attached sample code,
select() retruns OK even after the child at the other end of pipe having been terminated.
Tested in
RedHat EL5 (Linux 2.6.18-194.32.1.el5)
GNU C Library stable release version 2.5
Any help appreciated. Thnak you.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <unistd.h>
static void sigpipe_fct();
main()
{
struct stat st;
int i, fd_out, fd_in, child;
char buf[1024];
#define p_out "/tmp/pout"
signal(SIGPIPE, sigpipe_fct);
if (stat(p_out, &st) != 0) {
mknod(p_out, S_IFIFO, 0);
chmod(p_out, 0666);
}
/* start receiving process */
if ((child = fork()) == 0) {
if ((fd_in = open(p_out, O_RDONLY)) < 0) {
perror(p_out);
exit(1);
}
while(1) {
i = read(fd_in, buf, sizeof(buf));
fprintf(stderr, "child %d read %.*s\n", getpid(), i, buf);
lseek(fd_in, 0, 0);
}
}
else {
fprintf(stderr,
"reading from %s - exec \"kill -9 %d\" to test\n", p_out, child);
if ((fd_out = open(p_out, O_WRONLY + O_NDELAY)) < 0) { /* output */
perror(p_out);
exit(1);
}
while(1) {
if (SelectChkWrite(fd_out) == fd_out) {
fprintf(stderr, "SelectChkWrite() success write abc\n");
write(fd_out, "abc", 3);
}
else
fprintf(stderr, "SelectChkWrite() failed\n");
sleep(3);
}
}
}
static void sigpipe_fct()
{
fprintf(stderr, "SIGPIPE received\n");
exit(-1);
}
SelectChkWrite(ch)
int ch;
{
#include <sys/select.h>
fd_set writefds;
int i;
FD_ZERO(&writefds);
FD_SET (ch, &writefds);
i = select(ch + 1, NULL, &writefds, NULL, NULL);
if (i == -1)
return(-1);
else if (FD_ISSET(ch, &writefds))
return(ch);
else
return(-1);
}
From the Linux select(3) man page:
A descriptor shall be considered ready for writing when a call to an
output function with O_NONBLOCK clear would not block, whether or not
the function would transfer data successfully.
When the pipe is closed, it won't block, so it is considered "ready" by select.
BTW, having #include <sys/select.h> inside your SelectChkWrite() function is extremely bad form.
Although select() and poll() are both in the POSIX standard, select() is much older and more limited than poll(). In general, I recommend people use poll() by default and only use select() if they have a good reason. (See here for one example.)