LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-02-2009, 04:22 AM   #1
sklitzz
Member
 
Registered: Feb 2006
Location: Croatia, Split
Distribution: Ubuntu 7.10
Posts: 93

Rep: Reputation: 15
Redirecting Input and Output in C/C++


Hi,

I need to run a program that runs under certain limitations( like
memory and execution time) and also I need to redirect its input and
output streams

I have this code below which limits time and memmory but I dont know
how to fit in the redirection. I know you can use "<" and ">" operators
in shell but how do I do it in C/C++?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    struct rlimit lim;
    int status;

    if ( argc < 4 ) {
    fprintf(stderr, "Usage: %s <time> <memory> <exe>\n",
        argv[0]);
    exit(EXIT_FAILURE);
    }

    if (fork() == 0) {
    lim.rlim_cur = lim.rlim_max = atoi(argv[1]);
    setrlimit(RLIMIT_CPU, &lim);

    lim.rlim_cur = lim.rlim_max = atoi(argv[2]) * 1024 * 1024;
    setrlimit(RLIMIT_AS, &lim);

    if (execl(argv[3], NULL) == -1)
        fprintf(stderr, "I can't run the program.\n");
    } else {
    wait(&status);

    if (WIFEXITED(status))
		fprintf(stderr, "Program enden successfully with exit code: %d\n",
           WEXITSTATUS(status));
    else if (WIFSIGNALED(status))
		fprintf(stderr, "Program ended with signal #%d\n",
            WTERMSIG(status));
    }

    return EXIT_SUCCESS;
}

TIA,
sklitzz
 
Old 10-02-2009, 04:34 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Yes. That is not as easy as it first looks.

You will need to connect a pipe (see: man 2 pipe).

Or run the program with popen() (see: man 3 popen) which is easier but may not fit the code you posted very well.

Here you can read about pipes in C (PDF: look for paragraph 5.4): http://www.advancedlinuxprogramming....p-ch05-ipc.pdf
 
Old 10-02-2009, 06:17 AM   #3
yeye_olive
LQ Newbie
 
Registered: May 2009
Posts: 10

Rep: Reputation: 1
Where exactly do you want to redirect the input and output to? You might find the dup2() system call useful (see man dup2). It allows you to redefine what a file descriptor refers to. In you example you might want to:

1. open file descriptors to e.g. the files you want the i/o of the child redirected to,
2. then use them to overwrite STDIN_FILENO, STDOUT_FILENO and STDERR_FILENO with dup2(),
3. close the file descriptors opened in 1 as they are useless now,
4. and finally call execl().
 
Old 10-02-2009, 06:18 AM   #4
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Does freopen() not do what you want?

i.e.:

Code:
freopen("carbonfiber.out", "w+", stdout);
freopen("carbonfiber.err", "w+", stderr);

Last edited by carbonfiber; 10-02-2009 at 06:19 AM.
 
  


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
redirecting input from two files zhjim Linux - Newbie 2 08-07-2009 03:33 AM
Bash scripting: redirecting input to a command hal8000b Programming 2 12-10-2007 07:29 AM
Redirecting input for ssh... Lenux78 Linux - General 3 10-24-2006 01:00 PM
Input redirecting Thomas Ene Linux - Newbie 4 03-30-2006 12:16 AM
Automating tasks by redirecting input lsgko Linux - Software 4 02-26-2006 11:47 PM

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

All times are GMT -5. The time now is 05:10 PM.

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