Question

Below given code(process1) is similar to actaul scanrio. Im updating the global_data from another application using process id of the process1.

Because of getchar() in process1, when I run this process like,

$ ./process1 &

following message is displayed.

[1]+ Stopped (tty input) ./process1

I can't remove getchar(). so how to run both applications.

Note: telnet/ssh options tried. If I connect though telnet, then only that telnet window is active. At max I can work through only one terminal.

#include <stdio.h>    
volatile int global_data = 0;

int main()
{

FILE *fp = NULL;
int data = 0;    
printf("\n Address of global_data:%x \n", &global_data);

        while(1)
        {
              if(global_data == 0)
                {
                getchar();
                continue;
                }
                else if(global_data == 2)
                {
                        fp = fopen("JeyTest.txt", "w+");
                        if(fp == NULL)
                        {
                           printf("\n Error in file creation..... \n");
                                break;
                        }

                        for(data = 0; data < 1000; data++)
                        {
                            fprintf(fp, "%d\n", data);
                        }

                        fclose(fp);
                        break;
                }
        }    

return 0;
}
Était-ce utile?

La solution

Create a text file with the data you want to introduce to the process, and redirect the input:

$ ./process1 < file_with_data &

This mode avoid stopping the process because the stdin will be taken from file. The input file must have enough data to the process requeriments.

Autres conseils

A background process which attempts to read from stdin gets suspended (obviously). So first of all, figure out why is it that you want to read from stdin - can this be avoided - by say taking command line arguments - if so, then you should do that. In this case from you code, it looks like you want to wait till the volatile variable is updated - you don't want to check continuously. If this is the case, then use sleep.

Otherwise, you have to work around this by

  • redirecting stdin from a file.

or

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top