The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problems installing a program through Terminal in Mac OS X Sixmax UNIX for Dummies Questions & Answers 1 05-12-2008 12:52 PM
Problems installing a program in Terminal Sixmax Shell Programming and Scripting 4 04-10-2008 01:05 PM
Control process from different terminal (over SSH) rakeshou UNIX for Advanced & Expert Users 3 12-19-2007 08:48 PM
Terminal control from rsh zam Shell Programming and Scripting 2 09-22-2005 08:07 PM
Terminal Control Database Entry kayode SCO 2 10-23-2003 06:58 AM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 05-23-2007
ku@ntum ku@ntum is offline
Registered User
  
 

Join Date: May 2007
Posts: 10
terminal device control through a C/C++ program

Hi,

I want to fork a new process from a daemon that needs terminal attachment to some ttyN or pts/N device. Here is the code


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


int main(int argc, char **args)
{
char *con = args[1];

printf("\nStart demonizing ...\n");

int i = fork();
if (i == 0)
{
close (0);
close (1);
close (2);
setsid();

int j = fork();
if (j == 0)
{
setsid();
//int in = open(con, O_RDWR);
int in = open(con, O_RDONLY);
int ou = open(con, O_WRONLY);
dup2(in, fileno(stdin));
dup2(ou, fileno(stdout));
dup2(ou, fileno(stderr));

printf("\n input :");

stdin = fdopen(0, "r");
stdout = fdopen(1, "w");
stderr = fdopen(2, "w");

char tmp[100];
strcpy(tmp, "");
//gets(tmp);
fread(tmp, sizeof(tmp), 1, stdin);
printf("\n output : %s\n", tmp);
}
exit(0);
}
}


The program takes a single command line argument - the name of the terminal device special file, in my case that will be /dev/"pts/1". The problem is that, the inner most spawned child does the output on the pts/1 terminal successfully, but it fails to take input from it.

Anyone can help here.

Last edited by ku@ntum; 05-23-2007 at 08:28 AM..
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0