LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-02-2013, 08:57 PM   #1
iw1210
LQ Newbie
 
Registered: Apr 2013
Posts: 1

Rep: Reputation: Disabled
a simple example, Strange problem running in the background, about shell program.


a simple example, Strange problem running in the background, about shell program.

the "test.c" file is:

#include <stdio.h>

int main()
{
printf("\nstart test\n");

while(1)
{
printf("type 'X' to exit\n");

if('X' == fgetc(stdin))
break;

}

printf("\nend test\n");

return 0;
}

the "test.sh" file is:

#!/bin/bash
# test.sh
./test &

Compile test.c generates executable file test:

$ gcc -o test test.c

on the command line Executed directly test :
$ ./test &
[1] 10602

start test
type 'X' to exit


[1]+ Stopped ./test


But, execute the test.sh,why does the terminal constantly display “type 'X' to exit” ???

$ sh test.sh
type 'X' to exit
type 'X' to exit
type 'X' to exit
type 'X' to exit
............
 
Old 04-02-2013, 11:00 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I'd say its because you've got an infinite loop in the C code and stdin is disconnected (from the terminal) if you background a program, but stdout+stderr are not ...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem running a program/script in the background from a script newbie01.linux Programming 5 03-28-2011 07:28 AM
Problem when running simple c program ryellamr Linux - Newbie 3 06-06-2009 02:38 PM
Running program in the background HarryBoy Programming 2 06-12-2008 11:42 AM
Running C Program in Background help ibshar Linux - Newbie 7 10-04-2007 02:41 PM
Problem running C++ program in Linux Shell Alkibiades Programming 17 10-28-2006 06:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration