Running this snippet of code to experiment with piping and signals. I'm trying to learn how to properly utilize the select() function between pipes.
This process will fork. If there is something to be read from stdIn it is then written to the write end of the pipe. It is supposed to execute either a basic command entered via terminal, or it runs hard-coded commands in the code. (It's running hard code right now as "ls.")
When I run this snippet, it should quit and stop running completely when I press the letter "q" followed by ENTER, or it should quit after it runs its assigned process.
Instead, even after I hit "q" or run the process it won't stop the program completely. It is still waiting for input. It will stop running once I have hit ENTER, but it never even executes my process.
For example, if I compile and run this as "./test ls" or even just run "./test" (because ls is hard-coded in so that SHOULD just run I think), it will not run the command ls. And the program will continue to run until I've hit ENTER again.
I'm certain my rudimentary understanding of select() has to do with this issue. I'm pretty sure my select() statement needs to break at some point but I don't know what or how to check for this.
I was told that there is a method WIFEXITED() that might be able to help me but I'm just not sure how it applies in this context.
I also would like to know how to check if your pipes are empty!
I DO know that I want this to be able to both take input from the terminal and record it and also run built in functions.
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <stdio.h>
int main() {
int in[2]; // parent writes; child reads
pipe(in);
if (fork() == 0) {
// instantiate the values that will be execed
char *a[2];
//a[0] = "./test3";
a[0] = "ls";
a[1] = NULL;
// redirects what is being read from stdin to the pipe
//this redirection is for a separate test that is not included
close(in[1]);
close(0);
dup(in[0]);
close(in[0]); // close read
execvp(a[0], a);
}
else {
close(in[0]); // only want parent to write
// select() params
int nfds = 3;
int check = -1;
int done = 0;
fd_set readfds;
fd_set writefds;
FD_ZERO(&readfds); // set select params
FD_SET(0, &readfds);
FD_SET(in[1], &writefds);
while ((check = select(nfds, &readfds, &writefds, NULL, NULL)) > 0) {
int size = 0;
char buf[1000];
// write to pipe for child
if (FD_ISSET(0, &readfds) && FD_ISSET(in[1], &writefds)) {
while ((size = read(0, buf, sizeof(buf))) != 0) {
write(in[1], buf, size);
}
}
// reset
FD_ZERO(&readfds);
FD_SET(0, &readfds);
FD_SET(in[1], &writefds);
}
printf("%d --------------- %d\n", (FD_ISSET(in[1], &writefds)),
FD_ISSET(0, &readfds));
}
return 0;
}
Here is the potential set of test code that can be run with the above snipped if a[0] = ./test is uncommented and a[0] = ls is commented.
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
int main () {
int fd;
char buf[11];
int ret, sret;
int flag = 0;
fd = 0;
fd_set readfds;
struct timeval timeout;
while(1) {
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
timeout.tv_sec = 5;
timeout.tv_usec = 0;
sret = select(fd + 1, &readfds, NULL, NULL, NULL);
memset((void *) buf, 0, 11);
ret = read(fd, (void *) buf, 10);
flag = strcmp(buf, "q\n") == 0;
if (flag) {
return 0;
}
printf("ret = %d\n", ret);
if(ret != -1) {
printf(" buf = %s\n", buf);
}
}
return 0;
}
Related
Referring to following code example, I want the main thread to supply the number num that the child thread is expecting using scanf.
I tried this way to write the wordcount (9) to stdin which is to be read by child thread, but it is not working.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
void* child_thread_func(void* terminalflag)
{
int num=0;
printf("Trying to read num from stdin\n");
scanf("%d",&num);
/*expecting 9 to be printed here*/
printf("Entered number is %d\n", num);
}
int main () {
pthread_t tid;
if (pthread_create(&tid, NULL, &child_thread_func, NULL) != 0) {
printf("Failed to initialize thread\n");
exit(1);
}
sleep(2);
char buffer[50];
FILE *wfp = popen("wc -c", "w");
if (wfp != NULL) {
sprintf(buffer, "dummyword");
int save_stdin = dup(fileno(stdin));
dup2(fileno(wfp), fileno(stdin));
fwrite(buffer, sizeof(char), strlen(buffer), wfp);
dup2(save_stdin, fileno(stdin));
pclose(wfp);
}
pthread_join(tid, NULL);
}
Can someone suggest a correct way or any other alternative way to do this?
Thanks.
I don't think there is any good way for a process to write text to its own stdin; stdin is meant to be a way for the parent process (or the user, if the parent process is a Terminal window) to send data to your process, not for your process to send data to itself.
However, you could achieve a similar result by having your child thread use select() or similar to read input from both stdin and from the output end of a pipe; then your parent process can send data to the child process by writing to the input end of that same pipe.
Below is a modified version of your program demonstrating the technique. Note that the child thread will print out any text that you type into stdin; and also the main thread will send a line of text to the child thread once every 5 seconds, and the child thread will also print out that text. After the main thread has sent 5 messages to the child thread, the main thread will close its end of the pipe, causing the child thread to exit and then the process can exit cleanly as well.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/select.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
static int pipeReadFD = -1;
static int ReadTextFrom(const char * descriptionOfSender, int fd)
{
char buf[256];
const int numBytesRead = read(fd, buf, sizeof(buf)-1); // -1 so we always have room to place NUL terminator byte
if (numBytesRead > 0)
{
buf[numBytesRead] = '\0'; // make sure the string is NUL-terminated
printf("ReadTextFrom(): Read %i bytes from [%s]: [%s]\n", numBytesRead, descriptionOfSender, buf);
}
return numBytesRead;
}
void* init_on_sys_ready(void* terminalflag)
{
int num=0;
printf("Child thread: trying to read text from stdin\n");
while(1)
{
const int stdinFD = fileno(stdin);
const int maxFD = (pipeReadFD > stdinFD) ? pipeReadFD : stdinFD;
fd_set readFDSet;
FD_ZERO(&readFDSet);
FD_SET(stdinFD, &readFDSet);
FD_SET(pipeReadFD, &readFDSet);
const int selRet = select(maxFD+1, &readFDSet, NULL, NULL, NULL);
if (selRet >= 0)
{
if ((FD_ISSET(stdinFD, &readFDSet))&&(ReadTextFrom("stdin", stdinFD) <= 0)) break;
if ((FD_ISSET(pipeReadFD, &readFDSet))&&(ReadTextFrom("pipe", pipeReadFD) <= 0)) break;
}
else
{
perror("select");
break;
}
}
printf("Child thread exiting!\n");
return NULL;
}
int main(int argc, char ** argv)
{
int pipeFDs[2];
if (pipe(pipeFDs) < 0)
{
perror("pipe");
return -1;
}
pipeReadFD = pipeFDs[0];
int pipeWriteFD = pipeFDs[1];
pthread_t tid;
if (pthread_create(&tid, NULL, &init_on_sys_ready, NULL) != 0) {
printf("Failed to initialize CLI\n");
exit(1);
}
int count = 0;
for (int count=0; count < 5; count++)
{
char buf[512];
snprintf(buf, sizeof(buf), "Hello #%i from main thread", ++count);
const size_t slen = strlen(buf);
if (write(pipeWriteFD, buf, slen) == slen)
{
printf("main() sent [%s] to the child thread via the pipe.\n", buf);
}
else
{
perror("write");
break;
}
sleep(5);
}
close(pipeWriteFD); // this will cause the child thread to exit ASAP
pthread_join(tid, NULL);
return 0;
}
popen's man states:
[...] the command's standard output is the same as that of the process that called popen()
So you just need a way to redirect stdout to stdin.
Which is exactly what pipe is for. It links an output fd with an input fd.
As pipe creates new fds, we need to use dup2 to replace stdin and stdout, as you've already did in your example code. Threads share the same memory, so you don't have to worry about any child/parent differences in fds.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
void* child_thread_func(void* terminalflag)
{
int num=0;
printf("Trying to read num from stdin\n");
scanf("%d",&num);
/*expecting 9 to be printed here*/
printf("Entered number is %d\n", num);
}
int main () {
setbuf(stdin, NULL);
pthread_t tid;
if (pthread_create(&tid, NULL, &child_thread_func, NULL) != 0) {
printf("Failed to initialize thread\n");
exit(1);
}
int save_stdin = dup(STDIN_FILENO);
int save_stdout = dup(STDOUT_FILENO);
int tube[2];
pipe(tube);
dup2(tube[0], STDIN_FILENO);
dup2(tube[1], STDOUT_FILENO);
char buffer[50] = {0};
FILE *wfp = popen("wc -c", "w");
if (wfp != NULL) {
sprintf(buffer, "dummyword");
fwrite(buffer, sizeof(char), strlen(buffer), wfp);
pclose(wfp);
}
dup2(save_stdin, STDIN_FILENO);
dup2(save_stdout, STDOUT_FILENO);
pthread_join(tid, NULL);
}
FINAL UPDATE:
Code updated with final working version, got everything working thanks to the code found on: How to flush stdin without requiring user input?
Im programming C and having some trouble redirecting output from a parent process to a child process using file descriptors.
The idea of all of this, is for program A to be the middleman between all the data that goes from two programs.
I have a process A that creates a child process B which executes a execlp to a program C. There is also a program D that communicates with process A by named pipes and the idea is to redirect this communication to program C using unnamed pipes.
Right now my programs redirect the inicial communication from C to A to D correctly and from D to A correctly but fails when the redirection from A to C is supposed to happen.
I think the problem is that fgets() does not seem to retrieve the input unless two enters are given. I've tried using scanf, fscanf, getchar() and others aswell as flushing in multiple ways, nothing worked.
The only problem really is the fact that two inputs seem to be required for the communication of A to C to occur.
There are a million posts about this, and i've tried a lot of them to no sucess.
Can anyone help? Sorry if it sounds confusing.
Process A:(middleman)
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
int main(void)
{ //0->read 1->write
int fd_out[2];
int fd_in[2];
pipe(fd_out);
pipe(fd_in);
pid_t pid = fork(); //Create process B
char s[BUFSIZ];
char s2[BUFSIZ];
if ( pid == 0 )
{
close(STDOUT_FILENO);
dup(fd_out[1]);
close(fd_out[0]);
close(STDIN_FILENO);
dup(fd_in[0]);
close(fd_in[1]);
if(execlp("./test_pipe_game","./test_pipe_game",(char*)NULL) == -1){
printf("Error EXECL\n"); //Program C
}
}else{
int fifo;
char * myfifo = "/tmp/fifo123";
mkfifo(myfifo, 0666);
close (fd_out[1]);
close(fd_in[0]);
while(1){
read(fd_out[0], s, BUFSIZ);
printf("Game said:\n%s \nsending to client...\n", s);
fifo = open(myfifo, O_WRONLY);
write(fifo, s, BUFSIZ);
close(fifo);
fifo = open(myfifo, O_RDONLY);
read(fifo, s2, BUFSIZ);
close(fifo);
printf("Client said:\n%s \nsending to game...\n", s2);
write(fd_in[1], s2, BUFSIZ);
fflush(stdout);
printf("Sent!\n");
}
}
int status;
waitpid(pid, &status, 0);
if ( WIFEXITED(status) )
{
int exit_status = WEXITSTATUS(status);
printf("Exit status of the child was %d, my pid = %d\n",
exit_status, getpid());
}
return 0;
}
Program C:(game)
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
//https://stackoverflow.com/questions/54299405/how-to-flush-stdin-without-requiring-user-input
int flush_in(FILE *file)
{
int ch;
int flags;
int fd;
fd = fileno(file);
flags = fcntl(fd, F_GETFL, 0);
if (flags < 0) {
return -1;
}
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
return -1;
}
do {
ch = fgetc(file);
} while (ch != EOF);
clearerr(file);
if (fcntl(fd, F_SETFL, flags)) {
return -1;
}
return 0;
}
int main(){
char s[BUFSIZ];
char aux;
int c;
int i = 0;
while(1){
printf("Say something client!\n");
fflush(stdout);
//fgets(s, BUFSIZ, stdin);
scanf("%s",s);
printf("I received %s from the client!\n", s);
flush_in(stdin);
}
}
Program D(client)
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
int main()
{
int fd1;
char * myfifo = "/tmp/fifo123";
char str1[BUFSIZ];
char str2[BUFSIZ];
while (1)
{
system("clear");
fd1 = open(myfifo,O_RDONLY);
read(fd1, str1, BUFSIZ);
close(fd1);
printf("The game said:\n%s\n", str1);
printf("Say something:\n");
//fgets(str2, BUFSIZ, stdin);
scanf("%s",str2);
fd1 = open(myfifo,O_WRONLY);
write(fd1, str2, BUFSIZ);
close(fd1);
}
return 0;
}
UPDATE:
Like #CraigEstey and #thebusybee said my problem was using only one pipe, when i added the second pipe it solved the issue. I now have another problem, which seems to be related to reading from stdin without getting trash, i get no trash from the first 2 or 3 communications but after that fgets only reads trash. Flushing stdin does not seem to solve much. I've updated the codes if anyone wants to help!
My project is to fork and then use the parent process to read data from a file line by line and then send each line to the child process, which has to use execve to send the line as an argument for bc, and the output has to go back to the parent process. Right now, I'm just trying to send the data to the child and receive it properly, but it doesn't work. I have to use select to figure out if the child has output for the parent to get.
I have a file with 5 lines on it, and I use a while loop to go through the file. For each line I thought I would get the line back from the child, but it only does one line or two and stops. Then I get the same line twice for some reason.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <errno.h>
#include <wait.h>
int main(int argc, char *argv[])
{
alarm(60);
fd_set rfds;
fd_set r2fds;
struct timeval tv;
int retval;
int retval2;
int i = argc;
int rc;
FILE *fp;
fp = fopen (argv[1],"r");
char *args[3];
int j = 0;
while (j < i)
{
args[j] = argv[j+2];
j++;
}
int stdin_pipe_fds[2], stdout_pipe_fds[2], stderr_pipe_fds[2];
pipe(stdin_pipe_fds);
pipe(stdout_pipe_fds);
pipe(stderr_pipe_fds);
rc = fork();
if (rc == -1)
{
while (rc == -1)
{
rc = fork();
}
}
pid_t child;
pid_t parent;
if (rc == 0)
{
child = getpid();
close(stdin_pipe_fds[1]);
close(stdout_pipe_fds[0]);
close(stdout_pipe_fds[0]);
close(0);
dup(stdin_pipe_fds[0]);//, 0);
close(stdin_pipe_fds[0]);
close(1);
dup(stdout_pipe_fds[1]);//,1);
close(stdout_pipe_fds[1]);
close(2);
dup(stderr_pipe_fds[1]);//,2);
close(stderr_pipe_fds[1]);
}
if (rc > 0)
{
parent = getpid();
close(stdin_pipe_fds[0]);
close(stdout_pipe_fds[1]);
close(stderr_pipe_fds[1]);
}
char str[100];
char buf2[100];
char buf[100];
FD_ZERO(&rfds);
FD_SET(stdout_pipe_fds[0], &rfds);
tv.tv_sec = 1;
tv.tv_usec = 0;
while ((fgets(str,100,fp)) != NULL)
{
if (rc > 0)
{
int wstatus;
int wsta;
int status;
wsta = write(stdin_pipe_fds[1],str,strlen(str));
retval = select(FD_SETSIZE, &rfds, NULL, NULL, &tv);
if (FD_ISSET(stdout_pipe_fds[0], &rfds))
{
wstatus = read(stdout_pipe_fds[0], buf2, 100);
printf("From child: %s\n",buf2);
if (wstatus == -1)
{
printf("read failed\n");
//continue;
}
//wsta = write(stdin_pipe_fds[1],str,strlen(str));
}
}
if (rc == 0)
{
alarm(60);
scanf("%s",buf);
printf("%s", buf);
}
}
fclose(fp);
}
In the child
you close stdout_pipe_fds[0] twice, and not stderr_pipe_fds[0].
The scanf("%s", buf) will strip the newline from the string; you might not want that. Stdout, if you started from a terminal, will be line buffered; that is, it will take a newline to trigger an actual write. Since you snuck in and replaced the underlying FD, it doesn't know that a new buffering strategy may be appropriate.
In the parent:
you treat read and write as if they return a status; they return the number of bytes read or written. If it reads 0 bytes, the buf will contain the values from the previous read. I am not sure of the purpose of the read(), I would assume it just messes things up. I think you need to draw a picture of how the file descriptors are linked.
Suggestions: There is a generally accepted idiom when using fork:
if ((rc = fork()) == 0) {
/* do child stuff */
} else if (rc != -1) {
/* do parent stuff */
} else {
/* do error stuff */
}
Which is mainly followed to avoid warping peoples brains. It is really hard to read when it is interleaved. The "K" in K&R once quipped that "... if you're as clever as you can be when you write it, how will you ever debug it?"
The close(0); dup(); is much better expressed as dup2(fd, 0). The mere fact that you can compile code on your machine assures that dup2 functions correctly.
I'm having a strange problem. I'm currently creating two programs, one will execute on background and the other one will read its output and also allow to make inputs to the other one running on background.
The problem is, when I put the line dup2(in, STDIN_FILENO) the program executing on background stops printing to the files.
The Reader:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
int main()
{
int out, err, in;
size_t i = 0, j = 0;
int offErr = 0, offOut = 0;
char bufErr[1024];
char bufOut[1024];
char *cFifo = "/tmp/out";
char *cFifoErr = "/tmp/err";
char *cFifoIn = "/tmp/in";
out = open(cFifo, O_RDONLY|O_CREAT, 0600);
err = open(cFifoErr, O_RDONLY|O_CREAT, 0600);
in = open(cFifoIn, O_WRONLY|O_CREAT, 0600);
while(1)
{
memset(bufOut, 0, 1024);
memset(bufErr, 0, 1024);
i=0;
j=0;
while(!i && !j)
{
j = pread(err, bufErr, 1024, offErr);
i = pread(out, bufOut, 1024, offOut);
offOut +=i;
offErr +=j;
}
if(i)
write(STDOUT_FILENO, bufOut, 1024);
if(j)
write(STDOUT_FILENO, bufErr, 1024);
}
return 0;
}
and the Writer (background):
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
int main()
{
int out, in, err;
char *cFifo = "/tmp/out";
char *cInFifo = "/tmp/in";
char *cErrFifo = "/tmp/err";
out = open(cFifo, O_RDWR|O_CREAT|O_TRUNC, 0600);
in = open("/tmp/in", O_RDWR|O_CREAT|O_TRUNC, 0600);
err = open(cErrFifo, O_RDWR|O_CREAT|O_TRUNC, 0600);
dup2(out, STDOUT_FILENO);
dup2(err, STDERR_FILENO);
//dup2(in, STDIN_FILENO); //if I uncomment this it stops printing to out or err
system("python");
while(1);
return 0;
}
When you uncomment:
//dup2(in, STDIN_FILENO); //if I uncomment this it stops printing to out or err
you are making Python read from an empty file (it was created and/or truncated when you opened it), so Python gets nothing to do and exits.
Your reader code then sits in the while (1) ; loop doing nothing useful.
At the very least, add a printf() after the system() statement so you know when the Python command has completed.
I've to read program log file and to do that I wanted to use select() and read()
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd = -1;
fd_set fds;
struct timeval tv;
int rc;
char buffer[4096];
char * log_path = "/home/mich/a.txt";
if((fd = open(log_path,O_RDONLY) ) == -1 )
{
printf("error\n");
return -1;
}
while(1)
{
FD_ZERO(&fds);
FD_SET(fd,&fds);
tv.tv_sec = 2;
tv.tv_usec = 0;
rc = select(fd+1, &fds, NULL, NULL, &tv);
if (rc < 0) {
printf("failed\n");
continue;
} else if (rc > 0 && FD_ISSET(fd,&fds)) {
printf("read\n");
} else {
printf("timeout\n");
continue;
}
while ((my_getline(buffer,sizeof(buffer),fd)) > 0)
{
printf("%s",buffer);
}
}
close(fd);
}
my_getline is a function which uses read().
Output:
read
aaa
read
read
read
read
read
bbb
read
read
read
read
...
where aaa and bbb are lines from read file.
What is wrong in this program?
select() tells you that a read() won't block.
That includes the case where it will return 0 to indicate end-of-file, which presumably is what you're getting.
select tells you that there is something waiting on fd. This doesn't have to be a line, it could be a single byte, or the information that the end of the file has been reached. Add puts("####"); at the end of the outer while loop to see how your code processes the input file.
To see how to keep reading a file that other processes are appending to, look at the source code for one of the free implementations of the unix tail utility (specifically its -f option). The traditional implementation is to read the whole file, sleep for a small interval (1 second in tail), and repeat from the position where you left off after the last read. A more modern, more reactive, less resource-consuming, but less portable approach uses a file change notification API (inotify under Linux, kqueue under *BSD, ...).