Error in reading from the pipe - linux

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);
}

Related

Bi-directional communication inter-process using two pipes

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!

Sigaction doesnt work

I am learning about signals and wrote a simple programs that plays with them.
So i am inputting a number then using fork i create a process.The parent process is supposed to send the number as a signal to the child process,then the child_signal handler is supposed to send back the number squared as a signal.
This is the code.
#include <iostream>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>
using namespace std;
void child_handler(int sig_num){
cout<<"Child recieved a signal"<<endl;
pid_t ppid = getppid();
if(kill(ppid,sig_num*sig_num) == -1){
cout<<"Childs signal handler failed to send a signal "<<endl;
}
cout<<"Sent a sgnal to the parent"<<endl;
return;
}
void parent_handler(int sig_num){
cout<<"Parent recieved a signal "<<endl;
cout<<sig_num<<endl;
return;
}
int main(){
int n;
cin>>n;
pid_t pid = fork();
if(pid != 0){
struct sigaction sa2;
memset(&sa2,0,sizeof(sa2));
sa2.sa_handler = parent_handler;
if(sigaction(n,&sa2,NULL) == -1){
cout<<"Parents sigaction failed "<<endl;
}
if(kill(pid,n) == -1){
cout<<"Kill failed "<<endl;
}
cout<<"Sent a signal to the child"<<endl;
waitpid(pid,0,0);
}
else{
struct sigaction sa1;
memset(&sa1,0,sizeof(sa1));
sa1.sa_handler = child_handler;
if(sigaction(n,&sa1,NULL) == -1){
cout<<"Childs sigaction failed eerno:"<<errno<<endl;
}
sleep(20);
return 0;
}
return 0;
}
The output is this.
Sent a signal to the child.
And it doesn't say anything about sigaction.
In your code a child process can receive a signal before setting a handler.

I can't understand sigaction() result

#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).

Can not get proper response from select() using writefds

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.)

SIGUSR1 not received

This is my first Program....ctrlcsignal.c
enter code here
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
void signal_handler(int sigNo)
{
//if Ctrl+c signal
if(sigNo==SIGINT){
printf("value of SIGINT:-%d\t",SIGINT);
printf("received SIGINT\n");
}
// if some other signal , but this part wont get executed
// as the signal_handler function is registered with SIGINT only
else
{
printf("Some other signal found");
printf("value of other signal:-%d",sigNo);
}
}
int main(void)
{
//registering the signal handler function with a signal
kill(19574,SIGUSR1);
if(signal(SIGINT,signal_handler)==SIG_ERR)
{
printf("\n can't catch SIGINT\n");
}
while(1) //infinite loop
sleep(1); // 1s ,so that the CPU is not busy
return 0;
}
and this my second program....userdefinedsignals.c
enter code here
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void signal_handler(int sigNo)
{
printf("function entered...");
// check for userdefined Signal SIGUSR1
if (sigNo == SIGUSR1)
{
printf("received SIGUSR1 with value :- %d",SIGUSR1);
}
//checking for KILL Signal
else if (sigNo == SIGKILL)
{
printf("received SIGKILL with value :- %d",SIGKILL);
}
//checking for STOP Signal
else if (sigNo == SIGSTOP)
{
printf("received SIGSTOP with value :- %d",SIGSTOP);
}
// if some other signal , but this part wont get executed
// as the signal_handler function is registered with SIGINT only
else
{
printf("Some other signal found");
printf("value of other signal:-%d",sigNo);
}
}
int main(void)
{
int pid_t;
printf("process id is %d",getpid());
//registering the signal handler function with a signal
if(signal(SIGUSR1,signal_handler) == SIG_ERR)
{
printf("\n can't catch SIGSIGUSR1\n");
}
if(signal(SIGKILL,signal_handler)==SIG_ERR)
{
printf("\n can't catch SIGKILL\n");
}
if(signal(SIGSTOP,signal_handler)==SIG_ERR)
{
printf("\n can't catch SIGSTOP\n");
}
while(1) //infinite loop
sleep(1); // 1s ,so that the CPU is not busy
return 0;
}
I get the pid of the second process ... suppose xxxx
then i use the following command...
enter code here
kill -USR1 xxxx
but it shows nothing ....
also then i tried by calling the following function int the first program...but no use..
enter code herekill(xxxx,SIGUSR1);
HELP ME..!!!!
Works here.
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdarg.h> /* vsnprintf() */
#include <signal.h> /* signal */
void myprintf(FILE *fp, char *fmt, ...)
{
char buff[512];
int rc,fd;
va_list argh;
va_start (argh, fmt);
rc = vsnprintf(buff, sizeof buff, fmt, argh);
if (rc < 0 || rc >= sizeof buff) {
rc = sprintf(buff, "Argh!: %d:\n", rc);
}
if (!fp) fp = stderr;
fd = fileno(fp);
if (fd < 0) return;
if (rc > 0) write(fd, buff, rc);
return;
}
void signal_handler(int sigNo)
{
switch (sigNo ) {
case SIGUSR1:
myprintf(NULL, "received SIGUSR1 with value :- %d\n", SIGUSR1);
break;
case SIGKILL:
myprintf(NULL, "received SIGKILL with value :- %d\n", SIGKILL);
break;
case SIGSTOP:
myprintf(NULL, "received SIGSTOP with value :- %d\n", SIGSTOP);
break;
default:
myprintf(NULL, "Some other signal occured: %d\n", sigNo);
break;
}
return;
}
int main(void)
{
pid_t mypid;
mypid = getpid();
printf("process id is %d\n", (int) mypid);
if(signal(SIGUSR1,signal_handler) == SIG_ERR)
{ printf("\n can't catch SIGSIGUSR1\n"); }
if(signal(SIGKILL,signal_handler)==SIG_ERR)
{ printf("\n can't catch SIGKILL\n"); }
if(signal(SIGSTOP,signal_handler)==SIG_ERR)
{ printf("\n can't catch SIGSTOP\n"); }
if(signal(SIGCONT,signal_handler)==SIG_ERR)
{ printf("\n can't catch SIGCONT\n"); }
while(1) {
sleep(1);
}
return 0;
}
You're catching the signal all right, but not seeing the message because you don't terminate lines properly, and the standard output stream on your system is line buffered (assuming your program runs in a terminal).
Standard C defines three levels of buffering for output streams:
unbuffered, where output is transmitted immediately
line buffered, where output is transmitted when a newline character is encountered
fully buffered, where output is transmitted when an internal buffer fills
(This is a simplification - see a C reference or the Standard for details).
Consider:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("Hello");
pause();
}
This produces no output in a terminal. Fix it by terminating the line:
printf("Hello\n");
This will produce the expected output in a terminal.
If stdout is not connected to a terminal - for example, you redirect to a file - then the stream becomes fully buffered. This:
./a.out > foo
Ctrl-C
cat foo
produces no output, even with the newline character added. Here you need an explicit flush to transmit the output before the buffer is full.
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("Hello\n");
fflush(stdout);
pause();
}
This produces output even when redirected to a file.

Resources