I'm trying to create a piped multithreaded, multiprocess program where the parent process will create a thread that will read from a pipe from a sending child and then write to another pipe to the target child. But when the sending child process invokes piped write() to the parent process, the parent process won't read; it continues to block.
Pipe creation and the parent process creating the thread
int value1 = pipe(to_parent[index]);
int value2 = pipe(from_parent[index]);
if(value1 == -1) {
perror("pipe");
continue;
}
if(value2 == -1) {
perror("pipe");
continue;
}
/* fork for every connection to the server*/
int pid = fork();
/*go back to the beginning of loop if fork fails*/
if(pid == -1) {
perror("fork failed");
continue;
}
/* parent process will go back to the beginning of the loop */
if(pid != 0) {
close(from_parent[index][0]);
close(to_parent[index][1]);
pthread_create(&parent_listen_to_child[index], NULL, listen_to_child, &index);
continue;
}
/*PIPE
CHILD => PARENT*/
void * listen_to_child(void * ptr) {
int index = *(int*)ptr;
int index_to_kill;
printf("listen_to_child\n");
read(to_parent[index][0], &index_to_kill, sizeof(int));
printf("talk to child\n");
write(from_parent[index_to_kill][1], &index, sizeof(int));
}
Child processes create the thread that reads from parent through pipe and a thread which would soon write to the parent through the pipe
void respond(int index) {
char buffer[MAX];
int client_socket = clients[index].socket;
char name[MAX_NAME_SIZE];
strcpy(name, get_name(client_socket));
strcpy(clients[index].name, name);
printf("%s has connected to the server\n", name);
print_names();
printf("hello\n");
pthread_create(&command_processor, NULL, receive_command, &index);
/* close pipe ends in child*/
close(from_parent[index][1]);
close(to_parent[index][0]);
pthread_create(&child_listen_to_parent, NULL, listen_to_parent, &index);
pthread_join(child_listen_to_parent, NULL);
pthread_join(command_processor, NULL);
}
/* PIPE
PARENT => CHILD TO CANCEL*/
void * listen_to_parent(void * ptr) {
int index = *(int*)ptr;
int tattle;
printf("listen_to_parent\n");
read(from_parent[index][0], &tattle, sizeof(int));
printf("cancel %s\n", clients[index].name);
pthread_cancel(command_processor);
}
Where the write to the parent process from sending child is invoked
void send_talk(client sender, client target) {
printf("sender: %s\ntarget: %s\n", sender.name, target.name);
int sender_index = get_index(sender.socket);
int target_index = get_index(target.socket);
int write_value;
if(( write_value = write(to_parent[sender_index][1], &target_index, sizeof(int))) != -1) {
printf("write successful: wrote %d bytes\n", write_value);
}
else {
printf("write unsuccessful\n");
}
}
Related
I am new to pipes but how do I redirect the output from child_1 to the input for child_2?
I am trying to pass the value from the parent to child_1, adds 1 to the value, print the value, then use that output and pass it into child_2, add 1 again, and finally print the value.
The code below has the right output value for child_1, but not for child_2, how do I redirect the output from child_1 to the input for child_2?
Here is my code so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char * argv[]) {
int fd[2];
int PID;
pipe(fd); //fd1[0] = read | fd1[1] = write
PID = fork(); // spawn child_1
if (PID < 0){ // failed to fork
perror("Unable to fork child");
exit(1);
}
if (PID > 0) { // parent
int value = 100;
// since parent is only writing, close the reading end of pipe
close(fd[0]);
// write the data to the write end of the pipe
write(fd[1], &value, sizeof(int));
// then close the writing end of the pipe (parent)
close(fd[1]);
/**********************************************************/
} else { // child 1
int val = 0;
// read from the parent pipe
read(fd[0], &val, sizeof(int));
val += 1;
// is this how to redirect from one pipe to another?
dup2(fd[0], fd[1]);
// this prints the right value for val (val [101] = value [100] + 1)
printf("Child [%d] read value %d\n", getpid(), val);
// close the reading end of the pipe for child_1
close(fd[0]);
int PID2 = fork(); // make child 2
if(PID2 == 0) { // child 2
int val2 = 0;
close(0); // close stdin since we are trying to take the value from child_1
// read input from child_1 pipe (NOT WORKING?)
read(fd[0], &val2, sizeof(int));
val2 += 1;
printf("Child [%d] out %d\n", getpid(), val2);
close(fd[0]);
}
}
return EXIT_SUCCESS;
}
The way you have things set up, there's no need to use dup2() or any other I/O redirection.
Add #include <unistd.h> to the list of include files (and remove #include <string.h> — it seems to be unused)
Delete: dup2(fd[0], fd[1]);
Delete: close(fd[0]);
Delete: close(0);
Before the second fork(), add write(fd[1], &val, sizeof(val));
When you have close(fd[0]) in the first child, you effectively close fd[0] for the second child too.
You should check the status of the read and write operations before using the results.
Those changes lead to:
/* SO 7383-1815 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
int fd[2];
int PID;
pipe(fd);
PID = fork();
if (PID < 0)
{
perror("Unable to fork child");
exit(EXIT_FAILURE);
}
if (PID > 0)
{
int value = 100;
close(fd[0]);
write(fd[1], &value, sizeof(int));
close(fd[1]);
}
else
{
int val = 0;
if (read(fd[0], &val, sizeof(val)) != sizeof(val))
{
perror("read() failed in child 1");
exit(EXIT_FAILURE);
}
val += 1;
printf("Child [%d] read value %d\n", getpid(), val);
if (write(fd[1], &val, sizeof(val)) != sizeof(val))
{
perror("write() failed in child 1");
exit(EXIT_FAILURE);
}
int PID2 = fork();
if (PID2 == 0)
{
int val2 = 0;
if (read(fd[0], &val2, sizeof(val2)) != sizeof(val2))
{
perror("read() failed in child 2");
exit(EXIT_FAILURE);
}
val2 += 1;
printf("Child [%d] out %d\n", getpid(), val2);
close(fd[0]);
close(fd[1]);
}
}
return EXIT_SUCCESS;
}
When compiled (cleanly with options set fussy), it produces output such as:
Child [34520] read value 101
Child [34521] out 102
I believe this is what was wanted.
I have picked the following example from APUE :
void daemonize(const char* cmd)
{
int i,fd0,fd1,fd2;
pid_t pid;
struct rlimit r1;
struct sigaction sa;
//clear all file masks
umask(0);
//get max number of file descriptors
if(getrlimit(RLIMIT_NOFILE,&r1) < 0)
{
perror("error getting file descriptor size");
return;
}
//become a session leader
if((pid = fork()) < 0)
{
perror("error forking");
return;
}
else if(pid == 0)
{
setsid();
}
else
{
exit(0); //parent exits
}
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if(sigaction(SIGHUP,&sa,NULL) < 0)
{
return;
}
if((pid = fork()) < 0)
{
return;
}
else if(pid != 0)
{
exit(0); //parent
}
//child continues
syslog(LOG_ERR,"chile continuing with pid : %d",getpid());
//change the working directory
if(chdir("/") < 0)
{
return;
}
if(r1.rlim_max == RLIM_INFINITY)
r1.rlim_max = 1024;
for(i=0;i<r1.rlim_max;i++)
close(i);
//attach the file descriptors to /dev/null
fd0 = open("/dev/null",O_RDWR);
fd1 = dup(0);
fd2 = dup(0);
//initialize the log file
openlog(cmd, LOG_CONS,LOG_DAEMON);
if(fd0!=0 || fd1!=1 || fd2!=2)
{
syslog(LOG_ERR,"unexpected file descriptors %d %d %d\n",fd0,fd1,fd2);
exit(1);
}
}
int main()
{
daemonize("date");
pause(); //how is this working???
}
What I don't understand is how the pause() from the main function is working? What I was expecting is that since we have done exit(0) for the parent process in daemonize(), it should have exited and resulted in the normal termination of the main() process. It should have never returned to the main() and the call to pause() should not even happen. Why it did not terminate and why the pause() got called?
The code forks twice, producing a parent, a child, and a grandchild. The first two each exit(0); the last returns from daemonize.
I'm trying to communicate between two processes, a parent process that sends instructions and a child process that returns a response.
void create_player(Game *g, Player *p, int player, char* command) {
int send[2];
int receive[2];
int file;
if(pipe(send)!=0) {
_exit(99);
}
if(pipe(receive)!=0) {
_exit(99);
}
g->player[player].id = fork();
if(g->player[player].id < 0) {
fprintf(stderr, "Unable to start subprocess\n");
fflush(stderr);
exit(5);
}
//Parent process
else if(g->player[player].id>0) {
g->player[player].send = fdopen(send[1], "w");
g->player[player].receive = fdopen(receive[0], "r");
close(send[0]);
close(receive[1]);
//Child process
} else {
close(send[1]);
close(receive[0]);
dup2(send[0], 0);
dup2(receive[1], 1);
close(send[0]);
close(receive[1]);
file = open("/dev/null", O_WRONLY);
for(i =0; i < player; i++) {
fclose(g->player[i].send);
fclose(g->player[i].receive);
}
char width[1024];
char playerId[26];
char numOfPlayers[26];
char seed[1024];
char numOfCarriages[1024];
sprintf(seed, "%d", g->seed);
sprintf(width, "%d", g->numOfCarriages);
sprintf(playerId, "%d", player);
sprintf(numOfPlayers, "%d", g->numOfPlayers);
char* args[] = {command, numOfPlayers, playerId, width, seed, NULL};
execlp(command, command, numOfPlayers, playerId, width, seed, (char*) 0);
_exit(99);
}
When I run the code block to make the child processes, none of the messages are sent through (Parent stdout -> child stdin). I've added some examples to show how I'm handling the messaging. Any help would be greatly appreciated.
In the parent process: Parent Process sends message to child process
//Send message to start the game
void send_startGame(Game *g) {
fprintf(g->player[g->currentPlayer].send, "startGame\n");
fflush(g->player[g->currentPlayer].send);
}
In the child process: child message receives message from parent
void read_message(Game *g) {
char message[2048];
if(fgets(message, 2048, stdin) == NULL) {
fprintf(stderr, "Error in message\n");
}
//Receive start game message
if(strncmp("startGame\n", message, 9)==0){
start_game(g);
}
}
Try to reduce your code, there is wrong close in the child code :
close(send[1]);
dup2(send[0], 0);
close(send[0]);
After the dup2, the filedescriptor send[0] is no more related to the send pipe input, and it could close unexpectly an other filedescriptor in the child process.
Your code doesnot make communication from stdout of parent to stdin of child.
Hereafter a small sample that redirect stdout of parent to input of pipe and output of pipe to stdin of child.
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv) {
int send[2];
if(pipe(send)!=0) {
perror("pipe");
} else {
int pid = fork();
if(pid < 0) {
perror("fork");
}
else if(pid>0) {
//Parent process
close(send[0]); // close pipe input
dup2(send[1], 1); // replace stdout with pipe output
// send message to child
fprintf(stdout, "send to child\n");
fflush(stdout);
} else {
//Child process
close(send[1]); // close pipe output
dup2(send[0], 0); // replace stdin with pipe input
char message[2048];
if(fgets(message, 2048, stdin) != NULL) {
fprintf(stderr, "message from parent:%s", message);
}
}
}
}
Ideone link
So I have this program where the initial process sends numbers to the child, then the child performs certain operations with numbers and sends them to the next child...
My problem is that where the program has to send the numbers starting from two (2), the first number the child gets is 3. What could the problem be?
Here is my code:
void start(int num_of_nums){
if (num_of_nums <= 0) return;
int pipefd[2];
pid_t cpid;
int pipe_res = pipe(pipefd);
if (pipe_res == -1) {
printf("pipe error in start\n");
perror("pipe error");
exit(EXIT_FAILURE);
}
//create a new process
cpid = fork();
if (cpid == -1) {
printf("fork error in start\n");
perror("fork error");
exit(EXIT_FAILURE);
}
if (cpid == 0) { // child
printf("child in start\n");
close(pipefd[1]); // close write end
int num_from_parent = pipefd[0]; //where the number is read
printf("num from parent is %d\n", num_from_parent); //prints out 3...
filter(num_from_parent);
} else { // parent
printf("parent in start\n");
close(pipefd[0]); // close read end
for (int i = 2; i <= num_of_nums + 1; i++){
write(pipefd[1], &i, sizeof(int)); //WHERE THE NUMBERS ARE SENT
}
close(pipefd[1]);
}
}
Help would be much appreciated...
Title may be a little confusing, so let me explain. I am trying to write a simple shell to practice my programming. I have got the get a command, fork, exec loop working. However, when I press CTRL-C while child process is still executing, my shell terminates, instead of child process (but child process would keep running). Here is the main function:
int main()
{
dynarray *args; /* pointer to a dynamic array */
int bytes_read;
size_t nbytes = 0;
char *command;
pid_t pid;
printf("Enter command: ");
while ((bytes_read = getline(&command, &nbytes, stdin)) != -1) {
if (bytes_read == -1) {
perror("getline");
exit(EXIT_FAILURE);
}
pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
else if (pid == 0) { /* child process */
args = newdynarray();
char *arg = strtok(command, " \n");
while (arg != NULL) {
addstring(args, arg);
arg = strtok(NULL, " \n");
}
if (args->nval == 0) {
freedynarray(args);
continue;
}
addstring(args, NULL);
char *fullpath = find_executable(args->strings[0]);
if (fullpath == NULL) {
fprintf(stderr, "Couldn't find executable: %s\n", command);
exit(EXIT_FAILURE);
}
if (execv(fullpath, args->strings) == -1) {
perror("execv");
exit(EXIT_FAILURE);
}
} else {
int status;
waitpid(pid, &status, 0);
}
printf("Enter command: ");
}
return 0;
}
I didn't include other parts, because I don't think they are relevant. How can I make my child process catch all the input from stdin until it terminates?
You can register a signal handler for SIGINT in your parent process, and therein use kill(2) to send a signal to the child process, whose PID you should store somewhere.
How can I make my child process catch all the input from stdin until it terminates? Signals generated from stdin keys (such as control C) will be sent to the last process to use stdin, so there's nothing you can do unless you can force your child to use the path.
Instead, you need to create a signal handler in your shell process to catch SIGINT (and others), and resend the signal (using the kill() function) to the process you want to receive it.