码迷,mamicode.com
首页 > 其他好文 > 详细

代码中创建新终端 以及的把新终端的输出打印到文件,用于调式。

时间:2015-08-17 17:37:24      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

转自:http://blog.chinaunix.net/uid-20682147-id-4981769.html
cat term.c
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<string.h>
#include<sys/prctl.h>
#include<unistd.h>
#include<utmp.h>
#include<signal.h>
#include<pty.h>
#include<fcntl.h>

static void on_signal(int signo)
{
	printf("[%d] signal: %s \n", getpid(), strsignal(signo) );
}

int main()
{
	int amaster = 0;
	char name[100];
	struct termios termp;    //termios.h (bits/termios.h)
	struct winsize winp;     //term.h (bits/ioctl-types.h)
	pid_t pid = forkpty(&amaster, name, &termp, &winp);
	if (pid < 0) {
		perror("forkpty");
		exit(1);
	} 
	else if (0 == pid) {
		//子进程属于新终端
		//子进程的printf()在父进程的终端上看不见
		//父进程被中断或挂掉会向子进程发送sighup
		signal(SIGHUP, on_signal);
		printf("child: %d \n", getpid());
		int save_fd = dup(STDOUT_FILENO);
		int fd = open("1.txt", (O_RDWR |O_CREAT), 0644);
		dup2(fd,STDOUT_FILENO);
		while (true) {
			sleep(1);
			printf("sub process printf!!!\n");
			
		}
		exit(0);
	}
	else if (pid > 0) {
		//父进程使用之前终端
		//如果中断会向进程发送SIGHUP
		printf("pid: %d/%d\n", getpid(), pid);
		printf("name: %s\n", name);
		printf("amaster: %d\n", amaster);
		printf("win.row: %d\n", winp.ws_row);
		printf("win.col: %d\n", winp.ws_col);
		printf("win.xpixel: %d\n", winp.ws_xpixel);
		printf("win.ypixel: %d\n", winp.ws_ypixel);
		getchar();
	}

	return 0;
}


 gcc term.c -o term -lutil


tail -f  1.txt
sub process printf!!!
sub process printf!!!
sub process printf!!!
sub process printf!!!
sub process printf!!!
sub process printf!!!
sub process printf!!!
sub process printf!!!
sub process printf!!!
[6598] signal: Hangup 
sub process printf!!!
sub process printf!!!
sub process printf!!!


转自:http://blog.csdn.net/wangxvfeng101/article/details/11558697

方法1:  
     #include <stdlib.h>  
     #include <stdio.h>  
     #include <unistd.h>  
     #include <sys/stat.h>  
     #include <fcntl.h>  
       
       
     int main()  
     {  
         fflush(stdout);  
         setvbuf(stdout,NULL,_IONBF,0);  
         printf("test stdout\n");  
         int save_fd = dup(STDOUT_FILENO); // 保存标准输出 文件描述符 注:这里一定要用 dup 复制一个文件描述符. 不要用 = 就像是Winodws下的句柄.  
         int fd = open("test1.txt",(O_RDWR | O_CREAT), 0644);  
         dup2(fd,STDOUT_FILENO); // 用我们新打开的文件描述符替换掉 标准输出  
         printf("test file\n");  
           
         //再恢复回来标准输出. 两种方式  
         //方法1 有保存 标准输出的情况  
         //dup2(save_fd,STDOUT_FILENO);  
           
         //方法2 没有保存 标准输出的情况  
          int ttyfd = open("/dev/tty",(O_RDWR), 0644);  
         dup2(ttyfd,STDOUT_FILENO);  
         printf("test tty\n");  
     }  
      
     方法2:  
    #include <stdlib.h>   
    #include <stdio.h>   
    #include <unistd.h>   
      
     int main()  
     {  
         fflush(stdout);  
         setvbuf(stdout,NULL,_IONBF,0);  
         printf("test stdout\n");  
         freopen("test1.txt","w",stdout); //注: 不要使用这类的代码 stdout = fopen("test1.txt","w");   这样的话输出很诡异的. 最好使用  freopen 这类的函数来替换它.  
         printf("test file\n");  
         freopen("/dev/tty","w",stdout);  
         printf("test tty\n");  
     }



  1. 到这里我们就可以轻松解决 prinft  不输出到屏幕的问题了.  
  2.   
  3.   
  4. 不外乎两种情况.    
  5. 第1 标准输出被重定向了.  
  6. 第2 输出缓冲区的问题. 就是 我们上两例中 开头的两行代码了.  
  7.  fflush(stdout); //  刷新一下缓冲区 让它马上输出.  在printf 之后调用它,就会马上输出了.  
  8.  setvbuf(stdout,NULL,_IONBF,0); //如果你嫌上个方法麻烦, 就使用这个函数. 直接将缓冲区禁止了. 它就直接输出了.  
  9. 这两个函数都是有关流缓冲区的. 具体使用和说明网上有很多.   我只说一下什么是流缓冲区, 是做什么用的.  操作系统为减少 IO操作 所以设置了缓冲区.  等缓冲区满了再去操作IO. 这样是为了提高效率.  



代码中创建新终端 以及的把新终端的输出打印到文件,用于调式。

标签:

原文地址:http://my.oschina.net/lenglingx/blog/493534

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!