Only the stronger will survive.( Charles Darwin)

As i posted my first fifo error problem which was a practice thing .Now i am doing what i m supposed to do.

I m making this code first which is running is background.whenever any one will write on
the fifo it creates the control will come out of the select.

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

int main(void)
 { 
    int fds;
    fd_set fd;
    struct timeval tv;
    int retval,age;
    char data1[100];


    /*Creating A FIFO*/
    if (mkfifo("/home/ishan/fifo3", S_IRWXU|S_IROTH) != 0)
    perror("mkfifo() error");
    else
    puts("success");


    /* Opening A FIFO*/
        if ((fds=open("/home/ishan/fifo3",O_RDONLY)) < 0)
        {
        perror("open()error");
        }
        else
        {
        puts("!success");

        }
   /*Watch stdin (fd 0) to see when it has input. */
    FD_ZERO(&fd);
    FD_SET(fds, &fd);

   /* Wait up to five seconds. */
    tv.tv_sec = 10;
    tv.tv_usec = 0;
while(1)
{
   retval = select(fds+1, &fd, NULL, NULL, &tv);
   if(retval==-1)
   {
           printf("\n !!ERROR!!");
   }
   else
   {
           printf("\n Problem is solved!");
   }
	
	printf("\n enter your age plz");
	scanf("%d",&age);
	printf("\n skipped scanf");

   /* Don't rely on the value of tv now! */
   if(FD_ISSET(fds,&fd))
    printf("............ data received \n");
   else
    printf("............ data not received \n");
   if (retval == -1)
        perror("select()");
    else if (retval)
            printf("Data is available now.\n");
    /* FD_ISSET(0, &fd) will be true. */
    else
        printf("No data within ten seconds.\n");
       #if 0
	if(read(fds,data1,strlen(data1))>0)
	puts(data1);
	else
	printf("error .............. ");
      #endif

}
   return 0;
}

code 2:
via this code i m writing on fifo3

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include<fcntl.h>

int main()
{
char data[100];
int rc,fd;

/* Opening A FIFO*/
if ((fd=open("/home/ishan/fifo3",O_RDWR)) < 0)
{
perror("open()error");
}
else
{
puts("success");
}
/* Writing A FIFO*/
puts("\n enter data to be written");
gets(data);
if (-1 == (rc=write(fd, data, strlen(data))))
  {
    perror("write failed");
    return(0);
  }
else
{
puts("\n*\n");
puts(data);
}
return 0;
}

Output:
!success
success

enter data to be written
ishaan\

*

ishaan\

Problem is solved!
enter your age plz
it is not letting me scanf in first code but when i manually take the first process in foreground then its allowing to scanf.
Continued Output after using fg:
23

skipped scanf............ data received
Data is available now.

Is there any alternate way that it allows me to scanf without using fg commansd to take process 1 in fore ground.

Recommended Answers

All 2 Replies

>>gets(data);
Click here

>>it is not letting me scanf in first code but when i manually take
Now how do you expect a program to have keyboard access when its running in the background? Keyboard hits only go to the topmost forground program.

Dear Dragon;
when control is going to first process via select call and it is printing some messages.
I already know that it it will not allow access to a background process but i want to ask that is there any alternative such as system call by which we can take the bg process to foreground and allow the scanf(). And thanks for ur suggestion regarding gets().
I will never use that again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.